5.2 Connection-Oriented Protocol ClassesThe second basic mechanism, or protocol, for transfering data between a client and server, the connection-oriented protocol, establishes a connection between the client and server socket before data is transmitted. The logical device used to transfer packets of data between machines is called a socket. A socket has an associated port number that is either assigned by the operating system or supplied by the program. Data is usually sent between two sockets, one associated with the client machine and one associated with the server. When the client socket sends a packet of data, the packet is accompanied with information about the client machine's network address and the port number of the client socket. The server socket specifies only the port number it is monitoring to receive data. When one of the sockets sends a packet to the other, it waits to receive confirmation that the packet was successfully received. If the socket does not receive confirmation, it re-sends the packet. Packets are read from the receiving socket in the order that they were sent. The connection-oriented class hierarchy is shown in the figure below.
A schematic showing the mechanism for transferring data between a client and server using the connectionless protocol is shown below. The client program creates a Socket object that opens a logical connection with the server. The server program creates a ServerSocket object that will accept the connection. When the connection is made, the ServerSocket object creates a Socket object that in turn creates InputStream and OutputStream objects. The InputStream and OutputStream objects can be used to read and write byte stream data to and from the connection. It is possible for the calling application to spin off each new connection as a separate Thread, each with its own Socket object, allowing the ServerSocket to continue to listen for new connections. 5.2.1 ServerSocket Classpublic class ServerSocket extends Object Object ServerSocket The ServerSocket class is used to implement the connection-oriented communication protocol. A ServerSocket object is used to listen for a connection request from a client machine. When a connection is requested, the ServerSocket object can create a Socket object to receive and transmit data through the socket. ServerSocket() Constructor
Creates a ServerSocket object. The port that the ServerSocket object will listen to is specified. If the port is set to 0, the ServerSocket object will use any available port. The backlog is the number of connection requests that can be placed in the ServerSocket queue. The default number is 50. Once the queue is full, additional connection requests are refused and clients will receive an error. If the server machine has multiple IP addresses, the InetAddress argument can be specify which IP address will be used. accept() Method
accept() listens for a connection request and accepts the connection. When the connection is accepted, a Socket object is returned that can be used to send and receive data through the connection. Other activity is blocked until a connection is made. close() method
close() closes the socket used by the invoking ServerSocket object and releases any system resources associated with it. Methods to Return ServerSocket Properties
getInetAddress() returns an InetAddress object representing the IP address used by the invoking ServerSocket object. getLocalPort() returns the port number that the invoking ServerSocket object is listening to. getSoTimeout() returns the time in milliseconds that the invoking ServerSocket object will wait for a connection. A return value of 0 means that the ServerSocket object will wait indefinitely. setSoTimeout() method
setSoTimeout() is used to change the time in milliseconds that the ServerSocket will wait for a connection. toString() Method
toString() overrides the toString() method from the Object class and returns a String representation of the invoking Socket object.
5.2.2 Socket Classpublic class Socket extends Object Object Socket A Socket object is used to implement stream-based, connection-oriented data communication. The Socket class encapsulates a two-way communication mechanism that is connected at both ends to an IP address and port number. An InputStream and an OutputStream are associated with the Socket, to allow data to be sent to and read from the Socket using the standard methods of those classes, defined in the java.io package. (See InputStream Class in Section 4.1.7 on page and OutputStream Class in Section 4.2.9 on page .) Socket() Constructor
Creates a Socket object. The remote port number, and remote host name or an InetAddress object, must be specified. In addition the local port number and local host name or IP address can be provided. Buffer Size Methods
getReceiveBufferSize() and getSendBufferSize() return the size of the input and output buffers for the invoking Socket object. setReceiveBufferSize() and setSendBufferSize() suggest changes to the size of the input and output buffers. The system may or may not permit these changes. close() Method
close() closes the socket associated with the invoking Socket object and releases any system resources used by the socket. Methods that Return Socket Properties
getInetAddress() returns an InetAddress object containing the IP address of the remote host to which the invoking Socket object is connected. getKeepAlive() returns true if the 'keep-alive' property of the Socket is enabled. getLocalAddress() returns an InetAddress object containing the local IP address of the invoking Socket object. getLocalPort() returns the local port number for the invoking Socket object. getPort() returns the port number of the remote host to which the invoking Socket object is connected. getSoLinger() returns the delay in milliseconds between when the close() method is invoked and the socket is closed. getSoTimeout() returns the time in milliseconds the invoking Socket object will wait for an incoming packet. getTcpNoDelay() returns true if Nagle's algorithm is disabled for this socket. Nagle's algorithm combines smaller packets into larger packets for increased efficiency. Methods that Set Socket Properties
setKeepAlive() specifies the value of the invoking Socket object's 'keep-alive' property. setSoLinger() sets the delay in milliseconds between when the close() method is invoked and the socket is closed. If the boolean on is set to false or the int delay is set to 0, the socket closes immediately. setSoTimeout() specifies the time in milliseconds the invoking Socket object will wait for an incoming packet. setTcpNoDelay() specifies if Nagle's algorithm is to be used for this socket. Nagle's algorithm combines smaller packets into larger packets for increased efficiency. Stream Methods
getInputStream() returns an InputStream object that can be used to read data from the invoking Socket object. getOutputStream() returns an OutputStream object that can be used to send data through the invoking Socket object. shutDownInput() shuts down the InputStream of the invoking Socket object by placing the stream at "end of stream". Any data then received is silently discarded. shutDownOutput() disables the OutputStream of the invoking Socket object. toString() Method
toString() overrides the toString() method from the Object class and returns a String representation of the invoking Socket object. Deprecated Constructors
These two constructors were deprecated as of Java 1.1. If the boolean stream is set to false, a datagram socket is created. DatagramSocket objects should be used instead to create this type of socket.
|