Java Network communication: TCP protocol

Source: Internet
Author: User
Tags ack

During the interview, the interviewer, because of the need to examine an interviewer's familiarity with network programming, often examines students ' understanding of the protocols in TCP, HTTP, UDP, these common network programming, and the TCP protocol is the first, as a common protocol for communication between processes, As long as the post involves the communication between the process, it will be asked to the TCP protocol, then this article to summarize the TCP protocol bar ~

TCP protocol in the OSI seven layer model, is a transport layer of the Protocol, it is a reliable transmission of the protocol, this reliable, refers to TCP can ensure that the communication information is not caused by network factors to drop packets, to ensure that the recipient will be able to accept the full data. In the TCP reliable transmission aspect, often compares with the other protocol--UDP protocol, we through the comparison, first roughly has the understanding to the TCP protocol.

Similarities and differences between 1.TCP and UDP

1) TCP is connection-oriented, and UDP does not need to establish a connection.

2) TCP is byte stream oriented, while UDP is for datagram. It is understood that the length of the message sent by UDP is given by the process, and the length of the message sent by TCP is determined by the receiver's window and the current network situation. It can be imagined that TCP is transmitted by stream, and UDP is transmitted by a single message.

3) TCP is reliable transmission, and UDP is the best possible delivery protocol, TCP reliable transmission is mainly through the sliding window and send-confirm-window sliding mechanism to ensure that UDP is as far as possible to send, do not ensure that the data sent to arrive.

4) TCP is one-to-one full-duplex communication, while UDP can be single-to-many, multi-pair, many-to-many communication.

5) TCP has flow control, congestion control mechanism, and UDP does not.

From the above comparison, you can roughly grasp what TCP is a protocol, then, we look at the interview involved in the TCP protocol, the necessary test of a testing center, TCP three handshake and four waves.

2.TCP three-time handshake and four waves

TCP is a connection-oriented protocol, then two processes need to interact through TCP, the need to first establish a connection, the process of establishing a connection, need to go back and forth three times, so as the name implies, was more than three handshake, then the three handshake is a what kind of process? First, let's take a look.

In the diagram, focus on several points, the state of the client and server changes, the flag bit 1, the number of the sending sequence number.

First we call the process that initiates the connection request called the client, and the passive receive connection request called Server,server and the client creates the TBC (transport control block) before initiating the connection request, and the server enters the listening (Listen) state. After the client establishes the TCB, the connection request is sent.

The SYN flag position in the TCP message that the client will send is 1, and the send sequence number is set to X, which is sent to the server, when the client enters the Syn_sent (synchronous sent) state.

After the server receives, reply to the client A SYN flag bit also set 1,ack flag position 1, send the sequence number is: Y, and confirm the sequence number is: x+1 TCP message, this time the server will enter the SYN_RCVD (synchronous receive) state

After the client receives the reply message of the server, it replies to an ACK flag position 1, sends the sequence number to x+1, confirms the serial number is: Y+1 TCP message, this time the client and the server all simultaneously enters establish state, the connection establishes.

After the interviewer has explained three handshakes, the interviewer may ask a general extension question, why is it three handshakes, not two handshakes ?

This is to prevent the client from failing the connection request message suddenly to the server, resulting in an error. For example, in detail, the client sends a connection request, but because the message of the connection request is missing and the acknowledgement is not received, then the client again re-transmits the connection request, then receives the confirmation, then establishes the connection, after the data transfer completes, releases the connection. When the first connection request arrives at this time Server,server receives the message, mistakenly thinks that the client needs to establish the connection again, will reply to this connection request, but this time because the client does not actually request at this time, so the client ignores this reply, And if the use is two times handshake, this time the server has entered the state of establishing a connection, wasted resources in vain. With a three-time handshake, the server will not receive a response, indicating that it is an invalid connection and will be interrupted.

Java Network communication: TCP protocol

Related Article

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.