O'Reilly logo

Java RMI by William Grosso

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Customizing Socket Behavior

In addition to the basic methods for creating connections and sending data, the Socket class defines a number of methods that enable you to set some fairly standard socket parameters. Setting these standard socket parameters won’t change how the rest your code interacts with the socket. However, it will change the socket’s network behavior. The methods, paired along get( )/set( ) lines, are:

public boolean getKeepAlive(  ) 
public void setKeepAlive(boolean on) 

public int getReceiveBufferSize(  ) 
public void setReceiveBufferSize(int size) 
public int getSendBufferSize(  ) 
public void setSendBufferSize(int size) 

public int getSoLinger(  ) 
public void setSoLinger(boolean on, int linger) 

public int getSoTimeout(  ) 
public void setSoTimeout(int timeout) 

public boolean getTcpNoDelay(  ) 
public void setTcpNoDelay(boolean on)

In the rest of this section, we discuss these parameters in more detail:

public boolean getKeepAlive( )
public void setKeepAlive(boolean on)

One problem with distributed applications is that if no data arrives over a long period of time, you need to wonder why. On one hand, it could be that the other program just hasn’t had any information to send recently. On the other hand, the other program could have crashed. TCP handles this problem by allowing you to send an “Are you still alive?” message every so often to quiet connections. The way to do this is to call setKeepAlive( ) with a value of true. Note that you don’t need to worry about ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required