2. getter and setter methods used to obtain and set Socket options
The Socket option specifies how the Socket class sends and accepts data. In JDK1.4, a total of eight sockets can be set. These eight options are defined in the java.net. SocketOptions interface. Definition:
Public final static int TCP_NODELAY = 0x0001;
Public final static int SO_REUSEADDR = 0x04;
Public final static int SO_LINGER = 0x0080;
Public final static int SO_TIMEOUT = 0x1006;
Public final static int SO_SNDBUF = 0x1001;
Public final static int SO_RCVBUF = 0x1002;
Public final static int SO_KEEPALIVE = 0x0008;
Public final static int SO_OOBINLINE = 0x1003;
Interestingly, except for the first one that does not have the SO prefix, the other seven options use the SO prefix. In fact, this SO is the abbreviation of Socket Option. Therefore, all constants prefixed with SO in Java indicate Socket options. Of course, there are exceptions, such as TCP_NODELAY. A pair of get and set methods are provided for each option in the Socket class to obtain and set these options respectively.
1. TCP_NODELAY
Public boolean getTcpNoDelay () throws SocketException
Public void setTcpNoDelay (boolean on) throws SocketException
By default, when the client sends data to the server, it determines whether to send the data packet immediately based on the data packet size. When there is very little data in a packet, such as only one byte, but the packet header has dozens of bytes (IP header + TCP Header, the system combines small packages into soft and large packages before sending them, and then sends the data together. When sending the next packet, the system will wait for the server to respond to the previous packet. After receiving the response from the server, the system will send the next packet. This is called the Nagle algorithm; by default, the Nagle algorithm is enabled.
Although this algorithm can effectively improve the efficiency of network transmission, it also has low network speed and high requirements on implementation (such as games and Telnet ), using this method to transmit data will cause a pause on the client. Therefore, the best solution is to use the Nagle algorithm when it is needed and disable it when it is not needed. The use of setTcpToDelay can meet this requirement. When the Nagle algorithm is disabled using setTcpNoDelay (true), the client sends the data no matter the size of the data packet.
2. SO_REUSEADDR
Public boolean getReuseAddress () throws SocketException
Public void setReuseAddress (boolean on) throws SocketException
With this option, you can bind multiple Socket objects to the same port. In fact, this does not make much sense, but when the close method is used to close the Socket connection, the port bound to the Socket object may not be released immediately; sometimes, when the Socket connection is closed, the system checks whether there are data packets that have not arrived because of the delay surface. This is completely processed at the underlying layer, that is, it is transparent to users. Therefore, I don't feel it when using the Socket class.
This processing mechanism has no impact on the Socket objects bound to random ports, but the "Address already in use: JVM_Bind" exception may be thrown for the Socket objects bound to fixed ports. Therefore, this option can avoid exceptions.
Package mynet;
Import java.net .*;
Import java. io .*;
Public class Test
{
Public static void main (String [] args)
{
Socket socket1 = new Socket ();
Socket socket2 = new Socket ();
Try
{
Socket1.setReuseAddress (true );
Socket1.bind (new InetSocketAddress ("127.0.0.1", 88 ));
System. out. println ("socket1.getReuseAddress ():"
+ Socket1.getReuseAddress ());
Socket2.bind (new InetSocketAddress ("127.0.0.1", 88 ));
}
Catch (Exception e)
{
System. out. println ("error:" + e. getMessage ());
Try
{
Socket2.setReuseAddress (true );
Socket2.bind (new InetSocketAddress ("127.0.0.1", 88 ));
&