TCP handshake waved three times

1, TCP connection

The phone is able to use the phone because networking is the underlying implementation of the TCP / IP protocol, TCP connection is established the mobile terminals via a wireless network. TCP protocol can provide an interface to the upper network, the network data transmission upper layer built on top of "non-discriminatory" network.

Establish a TCP connection to go through, "three-way handshake":

The first handshake: a client sends syn packets (syn = j) to the server, and enters SYN_SEND state, waiting for the server to confirm;

Second handshake: server receives syn packets, must confirm the customer SYN (ack = j + 1), while themselves sends a SYN packet (syn = k), i.e., SYN + ACK packet, then the server enters a state SYN_RECV;

Third handshake: the client receives the SYN + ACK packet to the server, the server sends an acknowledgment packet ACK (ack = k + 1), this packet is sent, the client and server into the ESTABLISHED state, complete the three-way handshake.

Bag handshake does not include the transfer of data, after three-way handshake is completed, the client and server before the official start transferring data. Ideally, TCP connection, once established, before any party in the communication of the two parties take the initiative to close the connection, TCP connection will be kept down. When disconnected from the server and client sides can initiate a request to disconnect the TCP connection, disconnection process to go through "handshake" (writing process is not fine, that is, the server and client interaction, and ultimately determine the disconnection)


2, HTTP connection

HTTP protocol Hypertext Transfer Protocol (Hypertext Transfer Protocol), is the basis for Web networking, mobile phone network is one of the commonly used protocol, HTTP protocol is an application built on top of TCP protocol.

HTTP connection most notable feature is the client sends the server needs to send back a response for each request, after the request, will take the initiative to release the connection. From the establishment of the connection to close the connection process is called "a connection."

1) In the HTTP 1.0 client requests each time required to establish a separate connection, after processing this request, releases the connection automatically.

2) it is possible to handle multiple requests in a single connection in HTTP 1.1, and may overlap the plurality of requests, without waiting for the next request after the end of a transmission request.

Because at the end of each HTTP request will take the initiative to release the connection, thus HTTP connection is a "short connection" to remain online client program, the need to constantly initiates a connection request to the server. The usual practice is not necessary to obtain any data for real time, the client also maintains a fixed period of time to send a request to "stay connected" to the server every server to the client after receiving the reply request, indicates that the client knows " online. " If the server can not long received the client's request, the client is considered "offline", if the client can not receive a response time of the server, the network is considered to have been disconnected.


3, SOCKET principle

3.1 socket (socket) concept

Socket (Socket) is the cornerstone of communication, support TCP / IP protocol communication network basic operation unit. It is a network communication process endpoint abstract representation contains five kinds of information network communication must be: Protocol port, remote host's IP address, the remote connection protocol used in the process of protocol, IP address of the local host, the local process port.

When the application-layer data communication via the transport layer, TCP will encounter problems while providing services for multiple concurrent application processes. Multiple TCP connections or multiple applications through the same process may require a TCP protocol port to transfer data. In order to distinguish between different applications and process connections, many computer operating system applications TCP / IP protocol to interact with the provided socket (Socket) interface. The application layer and the transport layer through the Socket interface, a process of distinguishing a communication network from different applications or connections, concurrent data transmission service.


3.2 establishing the socket connection
established Socket Connection socket requires at least one pair, one running on the client, called the ClientSocket, another run on the server, called ServerSocket.

Socket connection process between the three steps: monitor server, client requests, connection confirmation.

Server monitoring: server-side socket does not target specific client socket, but in a state of waiting for a connection, real-time monitoring of network status, waiting for a connection request from the client.

Client requests: Refers to the client socket connection request is made, the goal is to connect to the server socket. To this end, the client socket must describe the server socket to connect to it, pointing out the address and port number of the server-side socket, then made a connection request to the server socket on.

Connection confirmation: When the server side socket listening to or receiving the connection request from the client socket, the client socket to respond to the request to establish a new thread, the server side socket described hair to the client, once the client to confirm this description, the two sides formally establish a connection. And the server socket continues in listening state, continues to receive requests from other clients connected to the socket.


4, SOCKET connection with TCP

When you create a Socket connection, you can specify the transport protocol used, Socket can support different transport protocols (TCP or UDP), when using the TCP protocol to connect the Socket connection is a TCP connection.


5, Socket connection to the HTTP

As is usually the case Socket connection is a TCP connection, therefore Socket Once connected, communicating parties can start sending data content to each other, until both disconnected. But in the real network applications, the communication between the client to the server often need to traverse multiple intermediate nodes, such as routers, gateways, firewalls and so on, most of the default firewall closes the connection is inactive for a long time which led to Socket connection is broken even, you need to tell the network by polling, the connection is active.

The HTTP connection using the "request - response" approach, not only in the need to establish a connection request, and the need for client makes a request to the server, the server can respond to data.

In many cases, the server needs to take the initiative to push data to the client, in real time and synchronize client and server data. At this point if the two sides to establish a Socket connection, the server can transmit data directly to the client; if the two sides to establish a HTTP connection, the server need to wait until after the client sends a request to the data back to the client, therefore, regular client sends a connection request to the server, not only can remain online, but also in the "ask" whether the server has new data, if the data will be passed to the client. TCP (Transmission Control Protocol) transport control protocol

 

TCP is the host to host transmission control protocol layer, to provide reliable connectivity services, three-way handshake to establish a connection confirmation:

I.e. tcp flag bit code, there are six kinds Flag: SYN (synchronous connection establishment) ACK (acknowledgement acknowledgment) PSH (push transfer) FIN (finish end) RST (reset Reset) URG (urgent emergency)

Sequence number (sequence number) Acknowledge number (confirmation number)

TCP / IP three-way handshake basis --TCP

The first handshake: Host A sends bit code syn = 1, randomly generated seq number = 1234567 packet data to the server, by the host B knows SYN = 1, A requirement to establish a connection;

Second handshake: Host B after receiving the request for a connection acknowledgment message, to send ack number A = (host A seq + 1), syn = 1, ack = 1, randomly generated packet seq = 7654321

Third handshake: Host A receives the ack Number check is correct, i.e., the first transmission seq number + 1, and the ack bit code is 1, if correct, then the host A transmits ack number = (host B seq + 1), ack = 1, the host B receives the acknowledgment seq value ack = 1 the connection is successfully established.

Three-way handshake is completed, the host A and the host B starts transmitting data.

 

 


In the TCP / IP protocol, TCP protocol provides reliable connectivity services, three-way handshake to establish a connection. 
The first handshake: a connection is established, the client sends syn packets (syn = j) to the server, and enters SYN_SEND state, waiting for the server to confirm; 
second handshake: server receives syn packets, must confirm the customer SYN (ack = j + 1), while themselves sends a SYN packet (syn = k), i.e., SYN + ACK packet, then the server enters a state SYN_RECV; third handshake: the client receives the SYN + ACK packet to the server, the server sends a confirmation packet ACK (ack = k + 1) , this packet is sent, the client and server into the ESTABLISHED state, complete the three-way handshake. Complete three-way handshake, the client and the server begins transmitting data.

 

 

Example:

IP 192.168.1.116.3337 > 192.168.1.123.7788: S 3626544836:3626544836
IP 192.168.1.123.7788 > 192.168.1.116.3337: S 1739326486:1739326486 ack 3626544837
IP 192.168.1.116.3337 > 192.168.1.123.7788: ack 1739326487,ack 1

The first handshake: 192.168.1.116 transmit bit code syn = 1, randomly generated data packet seq number = 3626544836 192.168.1.123,192.168.1.123 to know the SYN = 1 192.168.1.116 claim establish a connection;

Second handshake: 192.168.1.123 after receiving a request for a connection acknowledgment message, to send ack number = 3626544837 192.168.1.116, syn = 1, ack = 1, randomly generated seq = 1739326486 of packets;

Third handshake: 192.168.1.116 after receiving the inspection ack number is correct, that is, for the first time sent seq number + 1, and the ack-bit code is 1, if correct, 192.168.1.116 will then send ack number = 1739326487, after receiving confirmation 1,192.168.1.123 = ack seq = seq + 1, ack = 1 the connection is successfully established.

Guess you like

Origin www.cnblogs.com/royalluren/p/11020593.html