Java Reference
In-Depth Information
SO_REUSEADDR
When a socket is closed, it may not immediately release the local port, especially if a
connection was open when the socket was closed. It can sometimes wait for a small
amount of time to make sure it receives any lingering packets that were addressed to
the port that were still crossing the network when the socket was closed. The system
won't do anything with any of the late packets it receives. It just wants to make sure they
don't accidentally get fed into a new process that has bound to the same port.
This isn't a big problem on a random port, but it can be an issue if the socket has bound
to a well-known port because it prevents any other socket from using that port in the
meantime. If the SO_REUSEADDR is turned on (it's turned off by default), another
socket is allowed to bind to the port even while data may be outstanding for the previous
socket.
In Java this option is controlled by these two methods:
public void setReuseAddress ( boolean on ) throws SocketException
public boolean getReuseAddress () throws SocketException
For this to work, setReuseAddress() must be called before the new socket binds to the
port. This means the socket must be created in an unconnected state using the noargs
constructor; then setReuseAddress(true) is called, and the socket is connected using
the connect() method. Both the socket that was previously connected and the new
socket reusing the old address must set SO_REUSEADDR to true for it to take effect.
IP_TOS Class of Service
Different types of Internet service have different performance needs. For instance, video
chat needs relatively high bandwidth and low latency for good performance, whereas
email can be passed over low-bandwidth connections and even held up for several hours
without major harm. VOIP needs less bandwidth than video but minimum jitter. It
might be wise to price the different classes of service differentially so that people won't
ask for the highest class of service automatically. After all, if sending an overnight letter
cost the same as sending a package via media mail, we'd all just use FedEx overnight,
which would quickly become congested and overwhelmed. The Internet is no different.
The class of service is stored in an eight-bit field called IP_TOS in the IP header. Java
lets you inspect and set the value a socket places in this field using these two methods:
public int getTrafficClass () throws SocketException
public void setTrafficClass ( int trafficClass ) throws SocketException
The traffic class is given as an int between 0 and 255. Because this value is copied to an
eight-bit field in the TCP header, only the low order byte of this int is used; and values
outside this range cause IllegalArgumentException s.
Search WWH ::




Custom Search