Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: All
Aug 13, 1999

Your Options With Sockets

JDK 1.1 offers enhanced control over a socket's behavior through some socket options. These options are:

SO_TIMEOUT, which you can access through accessor methods:

 
	public synchronized int getSoTimeout() throws SocketException
	public synchronized void setSoTimeout(int timeout) throws
SocketException
of java.net.Socket class.

The specified timeout is in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only the specified amount of time. If the timeout expires, a java.io.InterruptedIOException is raised, but the Socket remains valid. In order to have effect, the option must be enabled prior to entering. The timeout must be greater than 0, because a timeout of zero is interpreted as an infinite timeout.

SO_LINGER, accessed through accessor methods:

 
	public int getSoLinger() throws SocketException
	public void setSoLinger(boolean on, int linger) throws
SocketException
of java.net.Socket class.

With this option, you can indicate whether you want the socket to linger on after its close() method is called in order to send its queued data to its peer. The linger variable of the setter method tells the socket how long(seconds) it should linger on after the invocation of its close() method in order to try to send queued data before it closes itself. The getter returns to you the length of time (seconds) the socket would linger on after its close() method is called. A return of -1 implies that the linger option is disabled.

TCP_NODELAY, accessed through accessor methods:

 
	public boolean getTcpNoDelay() throws SocketException
	public void setTcpNoDelay(boolean on) throws SocketException
of java.net.Socket class.

This one concerns Nagle's algorithm, which stops a machine on a network flooding an Internet by forcing the system to throttle back the output dependent on the replies from the far end. When a broadband link between the two points exist the TCP connection increases its bandwidth, but will slow down if things get congested.

As of Java 2 (a.k.a. Java 1.2), two more options have been added to sockets:

SO_RCVBUF, can be accessed through accessor methods:

 
public void setReceiveBufferSize(int size) throws SocketException
public int getReceiveBufferSize()throws SocketException
of java.net.Socket class.

This option indicates the size of the underlying network I/O buffer for input on the socket.

SO_SNDBUF, can be accessed through accessor methods:

 
public int getSendBufferSize() throws SocketException
public void setSendBufferSize(int size) throws SocketException
of java.net.Socket class.

This option indicates the size of the underlying network I/O buffer for output on the socket.

For both SO_SNDBUF and SO_RCVBUF, increasing the buffer size may increase the performance. Reducing it may help reduce backlog of in-bound data. For UDP packets, these options set the maximum size of a packet that may be sent on the socket.

Behrouz Fallahi
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap