Options
Public Final Static intTcp_nodelay = 0x0001; Public Final Static intSO_REUSEADDR = 0x04; Public Final Static intSo_linger = 0x0080; Public Final Static intSo_timeout = 0x1006; Public Final Static intSO_SNDBUF = 0x1001; Public Final Static intSo_rcvbuf = 0x1002; Public Final Static intSo_keepalive = 0x0008; Public Final Static intSo_oobinline = 0x1003;
One. Tcp_nodelay
By default false, when a client sends data to the server, it determines whether to send it immediately, depending on the size of the packet. When there is little data in the packet, such as only 1 bytes, and the packet header has dozens of words
Section (IP header +tcp header), the system merges the smaller packages into a soft package before sending, sending the data together. When the next packet is sent, the system waits for the server to the previous packet
Response, when the response from the server is received, the next packet is sent, which is called the Nagle algorithm
Two. So_reuseaddr
If the port is busy, but the TCP state is in Time_wait, the port can be reused. If the port is busy and the TCP state is in a different state, reusing the port still gets an error message that throws "Address
Already in Use:jvm_bind ". The SO_REUSEADDR option is useful when your service program stops and wants to restart immediately, for 60 seconds, while the new socket still uses the same port.
On the Windows platform, multiple socket new objects can be bound to the same port, and these new connections are non-time_wait state. This does not make much sense.
On the Linux platform, ports can be reused only if the TCP state is in time_wait. This is the right behavior.
publicclass Test { Public Static voidMain (string[] args) {Try{serversocket Socket1=NewServerSocket (); ServerSocket Socket2=NewServerSocket (); Socket1.setreuseaddress (true); Socket1.bind (NewInetsocketaddress ("127.0.0.1", 8899)); System.out.println ("Socket1.getreuseaddress ():" +socket1.getreuseaddress ()); Socket2.setreuseaddress (true); Socket2.bind (NewInetsocketaddress ("127.0.0.1", 8899)); System.out.println ("Socket2.getreuseaddress ():" +socket1.getreuseaddress ()); } Catch(Exception e) {e.printstacktrace (); } }}
There are two points to note when using the SO_REUSEADDR option:
1. You must use the Setreuseaddress method to open the SO_REUSEADDR option before calling the Bind method. Therefore, to use the SO_REUSEADDR option, you cannot go through the socket
Class is constructed to bind the port.
2. The SO_REUSEADDR option for all socket objects that are bound to the same port must be turned on to work. Both Socket1 and Socket2 use the Setreuseaddress method to open
The respective SO_REUSEADDR options.
The features of the Windows platform performance are not correct:
Socket1.getreuseaddress ():truesocket2.getreuseaddress ():true
The features on the Linux platform are correct: Because the first connection is not a time_wait state, the second connection cannot use port 8899
Socket1.getreuseaddress ():truejava.net.BindException:Address already in Java.net.PlainSocketImpl.socketBind (Native Method) at java.net.PlainSocketImpl.bind (Plainsocketimpl.java: 383) at java.net.ServerSocket.bind (serversocket.java:328) at Java.net.ServerSocket.bind (Serversocket.java:286) at com. Test.main (Test.java:15)
Three. So_linger
This socket option can affect the behavior of the Close method. By default, when the Close method is called, it returns immediately, and if there are still packets that are not sent, the packets are
Be discarded.
If the linger parameter is set to a positive integer n (the maximum value of n is 65,535), the Close method will be blocked for up to n seconds after it is called. Within these n seconds, the system will try to keep the number of
The packets is sent out, and if there are packets that are not sent, the packets are discarded, and the Close method returns immediately if it exceeds n seconds.
If you set linger to 0, the effect of the So_linger option is the same.
The socketexception exception will be thrown if the underlying socket implementation does not support So_linger.
Setsolinger also throws an IllegalArgumentException exception when a negative value is passed to the linger parameter. The Getsolinger method can be used to get the delay time off, if
Returns-1, indicating that the So_linger is closed. For example, the following code sets the delay-off time to 1 minutes:
if (Socket.getsolinger () = =-1) socket.setsolinger (true, 60);
Four. So_timeout
This option allows you to set the read data timeout. When the Read method of the input stream is blocked, if timeout is set, the system throws after a timeout millisecond
Interruptedioexception. After the throw, the input stream is not closed and you can continue reading the data through the Read method.
If you set timeout to 0, it means that read will wait indefinitely until the server program closes the socket. This is also the default value for timeout.
Five. So_sndbuf
By default, the send buffer for the output stream is 8,096 bytes (8K). This value is the size of the output buffer recommended by Java. If this default value does not meet the requirements, you can use the
The Setsendbuffersize method to reset the size of the buffer. However, it is best not to set the output buffer too small, or it will cause the transmission of data too frequently, thereby reducing the efficiency of network transmission.
Six. So_rcvbuf
The value of SO_RCVBUF is also used to set the TCP receive window which is advertized to the remote peer. Generally, the window size
Can be modified at any time if a socket is connected. However, if a receive window larger than 64K is required then the must be
Requested before the socket is connected to the remote peer. There is cases to be aware of:
- For sockets accepted from a serversocket, this must is done by calling
ServerSocket.setReceiveBufferSize(int) before the
ServerSocket is bound to a local address.
2. For client sockets, Setreceivebuffersize () must is called before connecting the socket to its remote peer.
Seven. So_keepalive
If this option is turned on, the client socket will send a packet to the server with an idle connection at every interval. This packet has no other effect, just to check
Check to see if the server is still active. If the server does not respond to this packet, the client socket will be closed.
If this option is turned off, the client socket may not shut down for a long time if the server is invalid.
Java Socket Option