Transport connection management for TCP

Source: Internet
Author: User
Tags ack

TCP Management of transport connections

TCP is a connection-oriented protocol with three phases: connection establishment, data transfer, and connection release. The management of the transport connection is to make the resume and release of the transport connection normal.

There are three issues to resolve during TCP connection setup:

1, to enable each Party to know the other side of the existence: so need three times handshake.

2. To allow the parties to negotiate some parameters (such as the maximum window value, whether to use the window expansion options and timestamp options, and quality of service, etc.).

3. Be able to allocate the transport entity resources (such as cache size, items in the connection table, etc.): Establish TCB.

The establishment of TCP connections is followed by client-server mode. An application process that initiates a connection is called a client, while an application process that passively waits for a connection to establish is called a server.

*tcp three-time handshake Wave four times

http://blog.csdn.net/whuslei/article/details/6667471

When establishing a connection: first, the server is passive open in the listen state, after the client starts to send the SYN message to the server, the server sends the Syn+ack message, then the client sends an ACK message to the server, the connection is established, and the data can be sent;

When the client has no data to be sent to the server, the client wants the server to send fin messages, the server received after the ACK acknowledgement received, the client can not send data to the server, but the server can still send data to the client, when the server sent out the data, The server sends a FIN message to tell the client that I have sent out the data, the client replies with an ACK acknowledgement message, and then the client waits for 2MSL time, because the acknowledgment message may be lost in the middle, and if the server does not send any messages during this 2MSL wait time, the server has received the message. This is the client can be closed, the server can also be closed when receiving an ACK message;

The MSL(Maximum Segment Lifetime) is the longest message segment life, with RFC recommendations of 2 minutes, which can be implemented with a smaller value.

keepalive Timer (keepalive timer):

When the client fails, the server can no longer receive the client's data, so there should be a measure that the servers do not wait in vain. Each time the server receives the customer's data, it re-sets the keepalive timer, which is usually set to two hours. If the customer's data is not received in two hours, the server sends a probe segment, which is sent every 75 seconds later. If there is no customer response after sending 10 probe segments, the server thinks the client has failed and then shuts down the connection. During this time, if the client restarts receiving the probe message, the client sends a reset message to let the server shut down the connection.

State transfer diagram, abnormal transfer, after-class exercises.

Why do I need three handshakes and four waves?

three-time handshake: strictly speaking, even if n-times handshake can not guarantee the two sides to establish a successful connection, because as long as the last confirmation is lost, the two sides are in an asymmetric state of information (this asymmetry is the asymmetry of the current point in time, the information before this point of symmetry). The sign of a successful connection should be knowing that each other is ready to transfer data, which requires at least three handshakes. take a for the client, b for the server as an example, if only one handshake: A to B sent a message, and then both sides think the connection is established, this situation is actually equivalent to UDP no connection, no meaning; If you use two handshake: A sends a SYN message to B, B sends an ACK message to a (possibly also the message SYN field), then B already knew that a wants to establish the connection to him, the information of both sides is basically symmetrical, however at this time the message segment of B to a may be lost, then a can not judge whether B received its own connection request, a state is unknown, B also know that this situation So a third handshake is needed, that is, a wants B to send an ACK message, when both parties know that the other side is ready to transmit the data (before the time point is ready, the current state is still asymmetrical).

The above only consider the case of packet loss, if the packet delay is reached, there will be "failed connection request message segment", such as A to B sent the connection request message delay reached B,b mistaken for a new connection request, and then accept the issue of the ACK message, If it is two times handshake B is now in the establishing state, but this is a wrong state, because a has already abandoned this connection.

In short, how many times a handshake can not guarantee the successful establishment of a connection, because the last message may be lost, delay to achieve a variety of situations. Three-time handshake success just means that both sides now have a fairly high probability that they can communicate normally.

Four waves: Four waves are actually two fin messages and two ACK messages, which are essential for four messages. A no data to send is bound to send a fin message to B, B must reply to an ACK message. Why can't b learn three handshakes to combine fin and ack? , since B is notified to the upper-level application after the FIN message, the upper-level application may not send the data, then the fin cannot be sent, even if it is sent, B should not be a combination of the two (notify the upper application may take a lot of time, these are uncertain), The best way to do this is to send an ACK to the other person and then send your fin at the right time.

In TCP/C mode, when the TCP client wants to disconnect, it cannot be disconnected with the TCP server with shutdown and closesocket, and the TCP client's port can be released immediately only if the TCP server is actively disconnected (the TCP client is passive). http://blog.csdn.net/HackerJLY/article/details/6116857

Can the server side actively disconnect?

Yes, but the active disconnect side waits for 2MSL, during which time the kernel does not release resources, which is detrimental to the server.

What do I need to do when I disconnect?

Recycle resources: Socket, memory, port number.

TCP long connections and short connections

Reference Documents: http://www.cnblogs.com/liuyong/archive/2011/07/01/2095487.html

Http://www.cnblogs.com/cswuyg/p/3653263.html

Http://blog.chinaunix.net/uid-26000296-id-3758651.html

Http://blog.sina.com.cn/s/blog_9720724f0101feg4.html

Short connection: Refers to the communication between the two parties have data interaction, the establishment of a TCP connection, the data is sent after the completion of the disconnection of this TCP connection;

General banks, HTTP servers use short connections. Short connections are generally one-to-many.

Long connection: Refers to a TCP connection can be continuously sent multiple packets, during the TCP connection remains, if no packet is sent, the two sides need to send a detection packet to maintain this connection;

The usual short connection operation steps are: connection → data transmission → close connection;

The long connection is usually: Connect → data transfer → keep connected (heartbeat) → data transfer → keep connected (heartbeat) → ... → Close connection;

KeepAlive

Reference documents:

Http://www.bubuko.com/infodetail-260176.html

Http://www.cnblogs.com/cswuyg/p/3653263.html

KeepAlive have different meanings in TCP and HTTP;

The keepalive in TCP is described in the keepalive timer above;

The meaning of the keepalive in HTTP is actually the use of persistent connections (long connections), which previously established a connection for each HTTP request. In this way, only one HTTP request and response can be transmitted per connection, and in order to be more efficient, the connection option is added in the header domain of HTTP. When set to Connection:keep-alive indicates on, set to Connection:close indicates off. It often takes dozens of requests to display a page, and only one connection is required to connect in a persistent way.

TCP for Network programming and UDP

Transport connection management for TCP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.