Docsity

Sockets and URLs - Advance Java Web Technology - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Sockets and URLs - Advance Java Web Technology - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Sockets and URLs

Sockets

  • A socket is a low-level software device for

connecting two computers together

  • Sockets can also be used to connect two programs running on the same computer
  • There are two kinds of sockets:
  • A Socket is a client socket, and initiates the communication
  • A ServerSocket waits for a request; it then
  • May perform the requested action
  • May return a result to the requester

Some Socket methods

  • getInputStream()
    • Returns an input stream for this socket
  • getOutputStream()
    • Returns an output stream for this socket
  • close()
    • Closes this socket

Some ServerSocket methods

  • accept()
    • Listens for a connection to be made to this socket and accepts it
    • Returns a Socket as its result
  • For input, use the getInputStream() and

getOutputStream() methods on the

returned Socket

  • close()
    • Closes this socket

URLs

  • A URL is a Uniform Resource Locator, typically

a Web address

  • Some constructors:
    • URL(String spec )
      • Creates a URL object from the information in the String
    • URL(String protocol , String host , int port , String file ) - Creates a URL object from the specified protocol, host, port number, and file
    • URL(String protocol , String host , String file )
      • Same as above, but uses the default port for this Docsity.com

Some URL methods

  • openStream()
    • Opens a connection to this URL and returns an InputStream for reading from that connection
  • openConnection()
    • Returns a URLConnection object that represents a connection to the remote object referred to by the URL
  • getProtocol(), getHost(), getPort(),

getFile(), getQuery()

Some URLConnection methods

  • getHeaderField(int n)
    • Returns the value for the nth header field
  • getHeaderField(String name)
    • Returns the value of the named header field
  • getHeaderFieldKey(int n)
    • Returns the key for the nth header field
  • getInputStream()
    • Returns an input stream that reads from this open connection
  • getOutputStream() Docsity.com

The End