Java Reference
In-Depth Information
could live more or less forever without noticing that the server had crashed. These
methods turn SO_KEEPALIVE on and off and determine its current state:
public void setKeepAlive ( boolean on ) throws SocketException
public boolean getKeepAlive () throws SocketException
The default for SO_KEEPALIVE is false. This code fragment turns SO_KEEPALIVE
off, if it's turned on:
if ( s . getKeepAlive ()) s . setKeepAlive ( false );
OOBINLINE
TCP includes a feature that sends a single byte of “urgent” data out of band. This data
is sent immediately. Furthermore, the receiver is notified when the urgent data is re‐
ceived and may elect to process the urgent data before it processes any other data that
has already been received. Java supports both sending and receiving such urgent data.
The sending method is named, obviously enough, sendUrgentData() :
public void sendUrgentData ( int data ) throws IOException
This method sends the lowest-order byte of its argument almost immediately. If nec‐
essary, any currently cached data is flushed first.
How the receiving end responds to urgent data is a little confused, and varies from one
platform and API to the next. Some systems receive the urgent data separately from the
regular data. However, the more common, more modern approach is to place the urgent
data in the regular received data queue in its proper order, tell the application that urgent
data is available, and let it hunt through the queue to find it.
By default, Java ignores urgent data received from a socket. However, if you want to
receive urgent data inline with regular data, you need to set the OOBINLINE option to
true using these methods:
public void setOOBInline ( boolean on ) throws SocketException
public boolean getOOBInline () throws SocketException
The default for OOBINLINE is false. This code fragment turns OOBINLINE on, if it's
turned off:
if (! s . getOOBInline ()) s . setOOBInline ( true );
Once OOBINLINE is turned on, any urgent data that arrives will be placed on the
socket's input stream to be read in the usual way. Java does not distinguish it from
nonurgent data. That makes it less than ideally useful, but if you have a particular byte
(e.g., a Ctrl-C) that has special meaning to your program and never shows up in the
regular data stream, then this would enable you to send it more quickly.
Search WWH ::




Custom Search