Java Reference
In-Depth Information
A number of constants are defined in the interface SocketOptions . These constants are
used as settings to customize a client socket. We do not implement the SocketOptions inter-
face directly, but we have access to its constants through getter and setter methods in the
Socket class. In the sections that follow, we discuss a few of the more useful options.
Using SO_TIMEOUT
When reading data from a socket through the use of the input stream's read method , the call
blocks other requests as determined by this setting. The SO_TIMEOUT option determines the
amount of time in milliseconds that blocking operations can block until they time out. If the
operation does not complete in the allotted time, then a java.net.SocketTimeoutException is
thrown, as shown in Figure 7-1. If this option has not been set or is set to 0, then the call blocks
requests until complete and will not time out.
Figure 7-1. A SocketTimeoutException
The Socket methods used for setting and getting this option are as follows:
public void setSoTimeout (int timeout) throws SocketException
public int getSoTimeout () throws SocketException
We use this option in DVDSocketServer.java , although we could just as easily have used
this option in our socket client, DVDSocketClient.java . The following line will shut down the
socket server after 1 minute, or 60,0000 milliseconds, of inactivity:
serverSocket.setSoTimeout(60000);
After the specified time elapses, a java.net.SocketTimeoutException is thrown. Figure 7.1
illustrates the DVDSocketServer console when the exception occurs.
Using SO_SNDBUF
This option sets the buffer size for data transmissions from this socket. When setting this
option, it is merely a suggestion, or a hint, to the platform about the size of buffer the applica-
tion requires for output operations over the socket. The getter indicates the actual size of the
output buffer. These two methods can be used together to determine whether the platform
Search WWH ::




Custom Search