Java Networking
Sockets
There are several socket options that are barely documented:
- SoLinger (get/set) - Sun's API docs tell you that you can use this
to set the "linger" time but don't explain what linger time is.
The Linger time is the amount of time undelivered data will be held before
it is discarded after a socket is closed. Adding some linger time will allow
unsent data to be transmitted.
- KeepAlive (get/set) - if this is enabled the socket will occasionally
probe to see if the connection is still active. The interval is undefined.
This is probably worthless and will just generate excess network traffic.
There are better ways to monitor if you've lost a connection.
- OOBInline (get/set) - by default this option is disabled and "urgent"
TCP data is silently discarded!
- SoTimeout (get/set) - normally the input stream will block on a read.
If this is enabled then a SocketTimeoutException is thrown if data isn't read
within the specified time.
- TcpNoDelay (get/set)- enables or disables "Nagle's algorithm".
In Java 1.5 the default appears to be false (use Nagle). Conflicting documentation
makes it unclear which setting turns on or off Nagle. Searching Sun's documentation
indicates that "false" in Java means use Nagle. Nagle's Algorithm
essentially says to buffer up data output until an entire packet is full.
A problem with Nagle is that it works badly with "TCP delayed acknowledgements".
Setting this to true (disabling Nagle) will decrease latency
but will increase bandwidth consumption.
A good resource is:
Java
Network Programming FAQ