Sockets and URLs Sockets n A socket is

  • Slides: 11
Download presentation
Sockets and URLs

Sockets and URLs

Sockets n A socket is a low-level software device for connecting two computers together

Sockets n A socket is a low-level software device for connecting two computers together n n Sockets can also be used to connect two programs running on the same computer There are two kinds of sockets: n n A Socket is a client socket, and initiates the communication A Server. Socket waits for a request; it then n n May perform the requested action May return a result to the requester 2

Socket constructors n import java. net. *; n Socket(Inet. Address address, int port) n

Socket constructors n import java. net. *; n Socket(Inet. Address address, int port) n n Server. Socket(int port) n n Creates a client socket and connects it to the specified port number at the specified IP address Creates a server socket on the specified port, with a default queue length of 50 The actual work, for both kinds of sockets, is done by an instance of the Socket. Impl class 3

Some Socket methods n get. Input. Stream() n n get. Output. Stream() n n

Some Socket methods n get. Input. Stream() n n get. Output. Stream() n n Returns an input stream for this socket Returns an output stream for this socket close() n Closes this socket 4

Some Server. Socket methods n accept() n n n Listens for a connection to

Some Server. Socket methods n accept() n n n Listens for a connection to be made to this socket and accepts it Returns a Socket as its result Once you have a Socket , you can use the same methods on both the client and the server sides For input, use the get. Input. Stream() and get. Output. Stream() methods on the returned Socket close() n Closes this socket 5

Socket fields n protected Inet. Address address n n protected File. Descriptor fd n

Socket fields n protected Inet. Address address n n protected File. Descriptor fd n n The file descriptor object for this socket protected int localport n n The IP address of the remote end of this socket The local port number to which this socket is connected protected int port n n The port number on the remote host to which this socket is connected All of these except File. Descriptor have associated getter methods 6

URLs n n A URL is a Uniform Resource Locator, typically a Web address

URLs n n A URL is a Uniform Resource Locator, typically a Web address Some constructors: n URL(String spec) n n URL(String protocol, String host, int port, String file) n n n Creates a URL object from the information in the String Creates a URL object from the specified protocol, host, port number, and file The host may be localhost, to indicate “this machine” URL(String protocol, String host, String file) n Same as above, but uses the default port for this protocol 7

Some URL methods n open. Stream() n n open. Connection() n n Opens a

Some URL methods n open. Stream() n n open. Connection() n n Opens a connection to this URL and returns an Input. Stream for reading from that connection Returns a URLConnection object that represents a connection to the remote object referred to by the URL get. Protocol(), get. Host(), get. Port(), get. File(), get. Query() 8

Using a URLConnection n The steps in using a URLConnection are: n n Use

Using a URLConnection n The steps in using a URLConnection are: n n Use open. Connection() to create the URLConnection object Set the parameters as desired: n n n set. Allow. User. Interaction, set. Do. Input, set. Do. Output, set. If. Modified. Since, set. Use. Caches, set. Request. Property Use the connect method to make the actual connection Use the remote object 9

Some URLConnection methods n get. Header. Field(int n) n n get. Header. Field(String name)

Some URLConnection methods n get. Header. Field(int n) n n get. Header. Field(String name) n n Returns the key for the nth header field get. Input. Stream() n n Returns the value of the named header field get. Header. Field. Key(int n) n n Returns the value for the nth header field Returns an input stream that reads from this open connection get. Output. Stream() n Returns an output stream that writes to this connection 10

The End 11

The End 11