Академический Документы
Профессиональный Документы
Культура Документы
1-Mar-13
Sockets
Sockets can also be used to connect two programs running on the same computer 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
Socket constructors
Creates a stream 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
ServerSocket(int port)
The actual work, for both kinds of sockets, is done by an instance of the SocketImpl class
getInputStream()
Returns an input stream for this socket Returns an output stream for this socket Closes this socket
getOutputStream()
close()
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()
Socket fields
The IP address of the remote end of this socket The file descriptor object for this socket The local port number to which this socket is connected
protected FileDescriptor fd
URLs
URL(String spec)
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 Same as above, but uses the default port for this protocol
openStream()
Opens a connection to this URL and returns an InputStream for reading from that connection Returns a URLConnection object that represents a connection to the remote object referred to by the URL
openConnection()
Using a URLConnection
Use openConnection() to create the URLConnection object Set the parameters as desired:
Use the connect method to make the actual connection Use the remote object
getHeaderField(int n)
Returns the value for the nth header field Returns the value of the named header field Returns the key for the nth header field Returns an input stream that reads from this open connection Returns an output stream that writes to this connection
10
getHeaderField(String name)
getHeaderFieldKey(int n)
getInputStream()
getOutputStream()
The End
11