Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make TCP connection and its Optimization in Java

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to do TCP connection and its optimization in Java? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Preface

As a back-end programmer, the network connection is an inevitable cut, when you are doing server optimization, network optimization is also one of the links, so as the most basic part of the network connection-TCP connection do you know? Today, let's take a closer look at this part.

TCP establish connection-three-way handshake

Detailed explanation

The client and the server have not yet established a connection, but the server is generally in the listen state. The client initiatively establishes a connection and sends a SYN message to the server. The client changes to the SYN_SENT status server to receive the message sent by the client, and also responds to a SYN message, including an ack. At this point, the server becomes SYN_RCVD and the client receives the SYN message sent by the server, acknowledges the ack, and it will send an ACK message to the server. At this point, the client changes to the ESTABLISHED server to receive the ACK message from the client and confirms the ack. At this point, the server becomes the ESTABLISHED server and the client can communicate normally.

Step 2-4 is a three-way handshake, so why do you need a three-way handshake? Why not shake hands once or twice?

First of all, we need to know that this network communication is successful only if both the server and the client can ensure that they can send and receive messages.

The purpose of step 2 is to let the server know that it can receive messages.

The function of step 3 is to let the client know that its function of sending and receiving messages is OK, and the ability to send messages is confirmed by the ack=x+1 returned by the server, because this value is based on the message seq=x sent by the client. The ability to receive messages is due to receipt of a return from the server.

The purpose of step 4 is to let the server know that its ability to send messages is OK (similar to step 3).

Linux View

The linux server can use the netstat-anp | grep tcp command to view the connection status of various ports and applications on the server.

You can also adjust the number of states by modifying the linux configuration file / etc/sysctl.conf

SYN_SENT status dependent

The number of retries sent to SYN (step 2) when actively establishing a connection

Nct.ipv4.tcp_syn_rctries = 6

Range of local port availability when establishing a connection

Net.ipv4.ip_local_port_range = 32768 60999

SYN_RCVD status dependent

Maximum number of SYN_RCVD stateful connections

Net.ipv4.tcp_max_syn_backlog

When passively establishing a connection, send SYN/ACK (step 3) number of retries

Net.ipv4.tcp_synack_retries

After talking about establishing a connection with TCP, let's take a look at the normal disconnection process of TCP.

TCP disconnect-four waves

Detailed explanation

The client actively disconnects from the server and sends the FIN message to the server. After the client becomes FIN_WAIT1 status server receives the client's FIN, it sends the ACK message to the client. After the server becomes the CLOSE_WAIT state client receives the server's ACK message, the client changes to the FIN_WAIT2 status server to send the FIN message to the client. After the server becomes LAST_ACK state, the client receives the FIN message sent by the server, sends the ACK message to the server, and the client becomes TIME_WAIT state. After the server receives the ACK message from the client, the server changes to CLOSED state. After 2MSL (max segment lifetime, maximum message lifetime) time, the client also changes to CLOSED state.

Among them, steps 2, 3, 5 and 6 are 4 waves.

TIME_WAIT State and its Optimization

After reading it, you must have a question: why does the TIME_WAIT state need to be kept 2MSL? Because this ensures that the port is unreusable within the round-trip time of at least one message.

Assuming that the duration of the TIME_WAIT state is very short, let's simulate the following scenario:

The client sends three messages to the server, of which the third message is stuck in the network, the server only receives the first two messages, sends ACK=2 to the client, and the client sends the third message again. The server actively sends FIN messages, the client sends FIN and ACK after receiving it, and the server sends ACK after receiving it and enters the TIME_WAIT state (assuming this state is very short). Now the server establishes a connection with the client again and begins to send normal data after a three-way handshake. as a result, the third message that was stuck before is finally sent to the server, but the server does not know how to deal with this message.

Therefore, this is also the reason why the TIME_WAIT state needs to be kept 2MSL. If the message is not received for such a long time, even if the correct message is sent from the client, it has expired, so it will not affect the subsequent communication.

But this also brings a problem. The TIME_WAIT state is maintained for a long time. Assuming that the server has a large number of TCP connections in TIME_WAIT state, it is equivalent to wasting a lot of server resources (ports). At this point, we can tune the server by modifying the following configuration:

Net.ipv4.tcp_tw_reuse = 1

When enabled, the new connection as a client can use the port that is still in TIME_WAIT state. Due to the existence of timestamp, the operating system can reject late messages (such as the third message mentioned above) and make use of the following configuration:

Net.ipv4.tcp_timestamps = 1

Optimization of other states

CLOSE_WAIT statu

If there are a large number of CLOSE_WAIT connections on the server side, it is very likely that the application process appears bug and does not close the connection in time.

FIN_WAIT1 statu

Adjust the number of retries to send FIN messages. 0 equals 8.

Net.ipv4.tcp_orphan_retries = 0

FIN_WAIT2 statu

Adjust the time spent in the FIN_WAIT2 state

Net.ipv4.tcp_fin_timeout = 60

Seeing this, you must have a general understanding of TCP connections. Now most of the servers use nginx for load balancing, so we may need to understand some nginx-related configuration principles on this basis, which should be more helpful to our server performance tuning.

This is the answer to the question about how to make a TCP connection in Java and its optimization. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 265

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report