TCP protocol and UDP protocol of computer network

Source: Internet
Author: User

The Transport layer provides communication services to the application layer above it, which belongs to the highest level of the communication section and is also the bottom of the user's function.

Two hosts are actually communicating with each other in a two host application process . Communication between application processes is also known as end-to-end communication .

The messages of different processes in the application layer are delivered down to the transport layer through different ports , and then the services provided by the shared network layer are down.

The transport layer provides end-to-end logical communication between application processes (but the network layer provides logical communication between hosts). The transport layer also carries out error detection of incoming messages.

The transport layer requires two different transport protocols:

(1) Subscriber Datagram Protocol UDP (user Datagram Protocol)

UDP does not need to establish a connection before transmitting data. The transport layer of the other party does not need to give any confirmation after receiving the UDP message. Although UDP does not provide reliable delivery, in some cases UDP is the most efficient way to work.

(2) Transmission Control Protocol TCP (transmission-Protocol)

TCP provides connection-oriented services. TCP does not provide broadcast or multicast services. Because TCP provides reliable, connection-oriented transport services , it inevitably adds a lot of overhead.

Port number

In order to enable the application process of computers running different operating systems to communicate with each other, it is necessary to use a unified method to flag the application process of TCP/IP system.

The solution to this problem is to use the protocol port number (Protocol port numbers) in the transport layer, or generally referred to as the Port .

Although the end of the communication is the application process, we can think of the port as the end of the communication, because we just have to send the message to the destination host for a suitable destination port, the remaining work (that is, the final delivery of the destination process) is done by TCP.

The port is marked with a 16-bit port number.

The port number only has local meaning , that is, the port number is only to mark each process in the computer application layer. The same port number is not associated with different computers in the Internet.

The port number is divided into two categories:

(1) port number used by the server.

This is divided into two categories, the most important one is known as the Port number (well-known), the value is generally 0~1023. The regulatory authority IANA assigns these ports to some of the most important applications of TCP/IP, which are known to all users.


Another class called the registration port number ,

The value is 1024~49151, which is used for applications that do not have a well-known port number. The port number used for this range must be registered in the IANA to prevent duplication.

(2) port number used by the client

The value is 49152~65535, left to the customer process to choose temporary use, only in the customer process run-time color dynamic selection, so called ephemeral port number . When the server process receives a message from the client process, it knows the dynamic port number used by the client process. After the communication is over, this port number can be used later by other client processes.


User Datagram Protocol UDP

UDP only adds a little bit of functionality on top of the IP datagram service, which is the function of the port and error detection.

UDP is not connected, that is, you do not need to establish a connection before sending the data .

UDP uses its best effort to deliver , that is, no guarantee of reliable delivery, and congestion control is not used.

UDP is message-oriented. UDP does not have congestion control, it is suitable for multimedia communication requirements.

UDP supports a pair of one or one-to-many, many-to-one, and many-to-many interactive communications.

UDP has a small header overhead and only 8 bytes .


Sender UDP sends the message to the application, and then delivers the IP layer down after the header is added. UDP does not merge or split the messages that are delivered by the application layer, but retains the boundaries of these messages. When the application layer is handed over to UDP, UDP sends a message at a time. The UDP user datagram that the receiver UDP has delivered to the IP layer, after removing the header, delivers the upper application process intact and delivers a complete message at a time. So the application must choose the appropriate size of the message.

The UPD header format is as follows:



Transmission Control Protocol TCP

TCP is a connection-oriented transport layer protocol, meaning that the application must establish a TCP connection before using the TCP protocol, and must release the connection after the data has been transferred.

Each TCP connection can have only two endpoints (endpoint), and each TCP connection can only be point-to-point ( one-to-one).

TCP provides reliable delivery of services to ensure that data is error-free, not lost, not duplicated, and arrives sequentially.

TCP provides full-duplex communication.

TCP is a byte stream-oriented, showing the sending and receiving process of byte stream:


A TCP connection is a virtual connection and not a real physical connection. TCP is not concerned about how long messages are sent to the TCP cache by the application process at a time. TCP determines how many bytes a message segment should contain based on the window value given by the other and the current level of network congestion (the length of the message sent by UDP is given by the application process). TCP can divide too long chunks of data into shorter transmissions. TCP can also wait to accumulate enough word sections to send out the message segment.

The endpoint of the TCP connection is not the host, not the IP address of the host, not the application process, nor the protocol port of the transport layer. The endpoint of a TCP connection is called a socket (socket) or socket.

The port number is stitched to the IP address to form the socket.

Socket sockets = (IP address: port number)

Each TCP connection is uniquely determined by the two endpoints (that is, two sockets) on both ends of the communication. That

TCP Connection:: = {socket1, socket2}

={(Ip1:port1), (IP2:PORT2)}

TCP is a byte-stream-oriented, but the data unit transmitted by TCP is a message segment. A TCP message segment is divided into the header and the data two parts.

The first 20 bytes of the header of the TCP packet are fixed, and the subsequent bytes are incremented as needed. The header format is as follows:



Connection control for TCP

There are three stages of a transport connection: connection Establishment , data transfer , and connection release . The management of the transportation connection is to make the transportation connection's establishment and the release to carry on normally.

The following three issues are resolved during connection setup:

(1) To enable each Party to ascertain the existence of each other.

(2) To allow the parties to negotiate some parameters (such as the maximum message segment length, maximum window size, quality of service, etc.).

(3) Ability to allocate transport entity resources (such as cache size, items in the join table, etc.).

The TCP connection is established using the client server approach . The application process established by initiating a connection is called a customer (client). The application process that the passive waits for a connection to establish is called the server.


TCP connection Establishment (three-time handshake)


, B's server process first creates the transport control block TCB, ready to accept connection requests from the client process. The server process is then in LISTEN(listening) state, waiting for the client's connection request.

A TCP client process is also the first to create a transport control block TCB, then send a connection request packet to B, then the synchronization bit in the header syn=1, and select an initial sequence number seq=x. TCP Specifies that the SYN message segment cannot carry data, but consumes an ordinal number. At this point, the TCP client process enters the syn-sent(synchronous sent) state.

b After receiving the connection request message segment, if you agree to establish a connection, send a confirmation to a. In the confirmation message section should be the SYN bit and ACK for both 1, confirm good ack=x+1, but also for their own choice of an initial sequence number seq=y. This message segment also cannot carry data, but it also consumes a serial number. At this point the TCP server process enters the SYN-RCVD(synchronously received) state.

After the TCP client process receives B's acknowledgement, it also needs to confirm to B. Confirm the ACK of the message Segment 1. Confirmation number ack=y+1, and its own serial number seq=x+1. The TCP standard specifies that the ACK segment can carry data, but does not consume the serial number if the data is not carried. At this point, the TCP connection is established and a enters the establishen(established connection) state.

When B receives the confirmation of a, it also enters the establishen state.

The above procedure is called a three-time handshake .


TCP connection release (four waves)


After the data transfer is complete, A and B are in the establishen state. A's application process first sends a connection release segment to its TCP and stops sending the data, actively shutting down the TCP connection. A connect the end of the packet to release the header of the FIN 1, its ordinal seq=u, equal to the last byte of the data that has been transmitted to add 1. At this point a enters the fin-wait-1(stop waiting 1) state, waiting for the confirmation of B.

b After receiving the connection release segment, a confirmation is issued, the confirmation number is ack=u+1, and this segment's own serial number is v. equals the ordinal of the last byte of the data that has been transmitted before B plus 1. Then B enters the close-wait(off wait) state. The TCP server process should then notify the high-level application process, so the connection from A to B is released, and the TCP connection is in a semi-closed state . That is, a has no data to send, but B if it sends the data, a will still be accepted.

A after receiving confirmation from B, enter fin-wait-2(terminating wait 2) status, waiting for the connection release message segment from B.

If B has no data to send to a, the application process notifies the TCP release link. At this point the message segment sent by B must make fin=1. It is assumed that the ordinal of B is w (some data may have been sent by the semi-closed State B). b You must also repeat the confirmation number ack=u+1 that you sent last time. At this point B enters the last-ack(final confirmation) state and waits for a confirmation.

A confirmation must be issued after receiving the connection release message section of B. In the confirmation message section, the ACK is set to 1, the confirmation number ack=w+1, and its serial number is seq=u+1. Then enter into the time-wait (time Wait) state. Note that the TCP connection is not released yet. The time required to wait for the timer to be set after 2MSL, a before entering into the CLOSED state. The time MSL is called the longest message segment life (Maximum Segment Lifetime).


Finite state machine for TCP

Each box in a TCP finite state machine diagram is a state that TCP may have. The uppercase English string in each box is the TCP connection state name used by the TCP standard. Arrows between states indicate changes in state that may occur. The words next to the arrows indicate the cause of this change, or indicate what happens after the state changes.

There are three different kinds of arrows in the diagram:

The thick solid line arrows indicate normal changes to the customer process.

A thick dashed arrow indicates normal changes to the server process.

Another thin line arrow indicates an abnormal change.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

TCP protocol and UDP protocol of computer network

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.