Socket basic knowledge sharing

Socket  Programming Considerations

Socket write programs need to pay attention to set the following timeout, avoid each other when there is no response, causing the program to hang or Socket wait too long

 

1.1         Accept Timeout

Accept timeout only ServerSocket helpful. Use ServerSocket accept () method to listen for the Socket connection client. By default, ServerSocket.accept () method will remain blocked until the client to connect. Normally, we do not need to set accept timeout.

 

But sometimes special circumstances, or to consider setting accept timeout.

For example: A Program to Program B issued a JMS message, and then starts a procedure A Socket Server, like waiting to receive program B returns the message through socket. If you do not set accept timeout, and the program B for some reason has been unable to connect Socket Server, will eventually lead to the program A hang.

 

Accept Timeout can be set up:

ServerSocket serverSocket = new ServerSocket(5555);

serverSocket.setSoTimeout(5000); // in milliseconds

while (true) {

    Socket socket = serverSocket.accept();

        …

}

 

1.2         Connect Timeout

When Client-Server connection terminal can be specified Connect Timeout

If not specified, will use the operating system's default:

THE

Default TCP timeout

BSD

75 seconds

Linux

189 seconds

Solaris

225 seconds

Windows XP

21 seconds

 

Connect Timeout can be set up:

SocketAddress socketAddress = new InetSocketAddress(host, port);

socket = new Socket();

socket.connect(socketAddress, connectTimeout);

 

1.3         Receive Timeout

When receiving data from the other socket can be provided Receive Timeout

The default is no timeout, socket will block until the data can be read.

Receive Timeout can be set up:

Socket socket = new Socket(host, port);

socket.setSoTimeout(timeout);

 

1.4         Send Timeout

Send Timeout socket is used for transmitting data to another party.

But there is no way to set up Java Send Timeout.

Of course, when the transmission data socket, will first be sent to a buffer within the native OS. As long as the data is generally sent at one time is not great, even if the other party hangs or is temporarily unable to receive data, it will not cause the sender to hang.

 

2.1       Socket ack (acknowledgement)

Then the Socket ack When the socket means receiving the data, sends an ack string (for example, $ the ACK) to the sender socket. Thus, socket according to whether the sender can determine whether the other received ack received data.

Socket ack is a communication protocol display added in the application. If you do not use ack, in socket communications, data may be lost.

 

For example, socket client to continuously send the message 100 to the socket server. If we receive the message of Article 50 of the server when forced to kill the server. So log client sends a query, the client may end successfully sent 51. Only when the client sends a message of Article 52 only exception is encountered. Article 51 of this message is lost.

Therefore, to ensure accuracy of data transmission, we can introduce ack protocol. Sometimes we must not only ensure server not only received the data, but also to ensure the successful processing of server data. At this time, after the server can successfully process the data and the like, to give the client send ack.

 

2.2       Socket Keep Alive

Socket connection like the same database connection, the weight type resource belongs. If we frequently create socket, send / receive data, close the socket, then there will be a large part of the time wasted on creating and close the socket.

So, if we often need to send / receive data with the same socket address, it should be considered only create a socket, and then has been using this socket object to send / receive data.

 

2.3       Heartbeat

Usually, we will set the receive timeout socket. Thus, if we left open socket (keep alive), but for a long time and no data communication, socket recipient will timeout, resulting in an open connection is broken.

If there is no data communication for a long time, a firewall or proxy server may also close the open socket connection.

Therefore, in order to ensure an open socket connection has been available for some time if there is no data communication (or a specified time interval), we can show send a heartbeat message (for example: $ HRT) to each other, so as to ensure that the connection is not abnormal shutdown .

 

2.4       Socket Close

Each socket object will hold a socket descriptor (actually a file descriptor), the operating system has a maximum limit for the socket descriptor. Therefore, when the socket is no longer used, be sure to remember to turn off, even if the socket connection failure or abnormal, as long as the socket object is not null, we must remember to shut down.

The following figure shows, when the socket is closed, the state change of the socket (socket netstat command status can be viewed). A more detailed explanation can google it.

 

When the calling party to take the initiative to close (first call close) state changes:

ESTABLISHED -> FIN_WAIT_1-> FIN_WAIT_2 -> TIME_WAIT -> CLOSED

When the passive party to call the close (after calling close) state changes:

ESTABLISHED -> CLOSE_WAIT -> LAST_ACK -> CLOSED

Typically, the TIME_WAIT state is normal, a period of time (2MSL, 1 to 4 minutes) will automatically disappear.

We need to pay special attention to CLOSE_WAIT state:

1. If a long time to disappear, indicating that the socket server process is too slow, a lot of client already connected to the server, send the data and close the finish.

2. If you have been does not disappear, indicating that there is no normal close socket (the other party has close)

 

2.5   SO_REUSEADDR Option

When the socket automatically call close, they can know from the above, it will eventually enter TIME_WAIT state needs over 1-4 minutes, to completely close.

 

When the socket in the TIME_WAIT state, it still occupies IP / PORT being used. Thus, if our program (such as socket server) using a fixed IP / PORT, when the socket is in TIME_WAIT state, the program will not be restarted immediately, the port occupies error.

 

Socket provides a setReuseAddress () method, you can set when the socket is in TIME_WAIT state, whether to allow other processes to bind to this port.

 

If we are developing socket server, be sure to remember to call ServerSocket.setReuseAddress (true).

Client socket also has this method, and sometimes may need to specify a local IP / PORT client connects to the server when used (usually not specified, the system will randomly select a PORT). But the real test, set on the client socket This method does not work under Windows and Solaris. When the socket in the TIME_WAIT state, restart the client port is still occupied by error. Search the Internet for a long time, many people have encountered this problem may be the underlying operating system socket implementation issues. Because the test using a socket client C language development, there are also the error. Some people say that under LINUX easy to use, there is that you can try to modify tcp_time_wait_interval to reduce the waiting time TIME_WAIT

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/11/04/2236169.html

Guess you like

Origin blog.csdn.net/weixin_33711641/article/details/93361104