TCP/UDP detailed

Source: Internet
Author: User
Tags ack rfc

Reprint: Http://www.cnblogs.com/visily/archive/2013/03/15/2961190.html, fancies

Related: HTTP protocol detailed

In-depth understanding of the HTTP protocol

1. The necessity of transmission layer existence

Because the packet transport of the network layer is unreliable, it is not possible to understand the time at which the data arrives at the end, and to understand the state of the data not reaching its end. Therefore, it is necessary to enhance the quality of service provided by the network layer.

2, the cause of the introduction of transport layer

The connection-oriented transport service is similar to the connection-oriented network service, which is divided into three stages of establishing connection, data transmission and releasing connection, and the address, addressing and flow control are similar. A non-connected transport service is also very similar to a non-connected network service. One obvious question: since the service of the transport layer is so similar to the service of the network layer, why do we need two separate tiers?

The reason for this is that the code for the Transport layer runs completely on the user's machine, but the network layer is primarily running on a router controlled by the carrier. What are some of the things you think?

The service provided by ① Network layer is not enough;

② frequent loss of grouping;

③ routers often crash.

Users do not have real control over the network layer, so they cannot use the best routers or a better error handling mechanism on the data link layer to solve poor service problems. The only possibility is to improve the quality of service in another layer above the network layer. This is the need for the transport layer to exist.

The importance of the transport layer: not just another layer, it is the core of the entire protocol hierarchy. Without a transport layer, the entire concept of a layered protocol becomes meaningless.

Transport Layer Tasks: Provides reliable, cost-effective data transfer services between the source and target machines and is completely independent of the physical network currently in use.

3, the function of the transport layer

Data transfer, do not care about the meaning of data, interprocess communication.

Make up for high level (top 3) Requirements and network layer (based on the next 3 layers) Data transfer service quality difference (Error rate, error recovery ability, throughput rate, delay, cost, etc.), to the high-level shielding network layer service differences, provide a stable and consistent interface.

4. The main difference between the Transport layer protocol and the Network layer protocol

The network layer (the IP layer) provides a point-and-spot connection that provides logical communication between hosts, and the transport layer provides an end-to-end connection that provides logical communication between processes.

5, the use of the transport layer

The transport layer fragments the data and makes the necessary controls to reorganize the fragments into a variety of communication flows. In this process, the transport layer is primarily responsible for:

① tracks each communication between the source host and the application on the destination host;

② segment the data and manage each fragment;

③ to reorganize segmented data into application data streams;

The ④ identifies different applications.

6, the concept of port number

Processes running on the computer are marked with the process identifier. The various application processes running at the application level should not let the computer operating system assign its process identifier. This is because computers that are used on the Internet have a wide variety of operating systems, and different operating systems use process identifiers in different formats. 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.

7, the transport layer of the main protocol

The two main protocols of the TCP/IP transport layer are important standards for the Internet, TCP (Transmission Control Protocol) [RFC 768], User Datagram Protocol UDP (subscriber Datagram Protocol) [RFC 793].

The data flow of the transport layer is to establish a logical connection between the network endpoints. If you use UDP, the primary task of the transport layer is to transfer data from the source device to the destination device. With TCP, the transport layer provides end-to-end control primarily through sliding windows, as well as providing reliability with confirmation sequence numbers and acknowledgement information. The transport layer defines an end-to-end connection between host applications.

8. Comparison of TCP&UDP

TCP (transmission Control Protocol) Reliable, connection-oriented protocol (eg: call), low transmission efficiency full duplex communication (send cache & receive cache), byte stream oriented. Applications that use TCP: Web browsers, e-mail, file transfer programs.

UDP (User Datagram Protocol) unreliable, non-connected services, high transmission efficiency (before sending are chosen adequately), a pair of one or one-to-many, many-to-one, many-to-many, message-oriented, to do their best to serve, no congestion control. Applications that use UDP: Domain Name System (DNS), video streaming, voice over IP (VoIP).

Question 1: What is connection-oriented, non-connected?

Connection-oriented example: Two people communicate over the phone.

For no connection example: Postal Service, the user put the letter in the mail to look forward to the postal processing process to pass postal parcels. Obviously, an unreachable representative is unreliable.

From the perspective of the implementation of the program to resolve the connection-oriented, non-connected as follows:

TCP for connection, UDP for no connection (in default blocking mode):

In the TCP protocol, when the client exits the program or disconnects, the RECV function of the TCP protocol immediately returns to no longer block because the server itself knows that the client has exited or disconnected, proving that it is connection oriented;

In the UDP protocol, recvfrom this receive function will always remain blocked, because the server itself does not know that the client has exited or disconnected, proving that it is facing a non-connected.

It can also be clearly seen that TCP communication requires the server to listen for listen, receive client connection request accept, wait for the client connect to establish a connection before the packet send and receive (recv/send) work.

However, the concept of UDP server and client is not obvious, the server side is the receiving end need to bind the port, waiting for the arrival of the client's data. Subsequent data can be sent and received (RECVFROM/SENDTO) work.

Question 2: What is byte stream oriented, message oriented?

Byte stream oriented: Although the interaction between the application and TCP is one block of data at a time (varying in size), TCP sees the application as a series of unstructured byte streams. TCP has a buffer, and when the data block that the application transmits is too long, TCP can divide it short and then transmit it. If the application sends only one byte at a time, TCP can also wait to accumulate enough word sections to send out the message segment.

Message-oriented: message-oriented transmission is the application layer to the UDP long message, UDP is sent, that is, one message sent at a time. Therefore, the application must select the appropriate size of the message. If the message is too long, the IP layer needs to be fragmented, reducing efficiency. If it is too short, the IP will be too small. (Shehiren, Fifth edition.) UDP does not merge or split the messages that are delivered by the application layer, but retains the boundaries of these messages. That is to say, the application layer to the UDP long message, UDP will send the same, that is, send a message at a time).

Issue 3: In the default blocking mode, why does TCP have no boundaries and UDP has a boundary?

For the TCP protocol, the client continuously sends the data, as long as the server side of the buffer is large enough to receive a one-time, that is, the client is divided several times to send over, there is a boundary, and the server is a one-time reception, so the proof is borderless;

And for the UDP protocol, the client continuously sends the data, even if the server side of the function of the buffer is large enough to receive only one time, the number of times sent to receive, that is, the client sent over several times, the service side must be received several times, thus proving that the UDP communication pattern has boundaries.

Question 4:udp How to send large amounts of data? How to deal with subcontracting?

Use updclient to send too much data at a time, or you will get an error: A message sent on a datagram socket is larger than the internal message buffer or some other network restriction, or the buffer used by the user to receive datagrams is smaller than the datagram. But I don't know how many bytes can be sent at a time? If a large byte array is split into several byte arrays, how will it be judged and processed when it is received?

Answer: The method is simple:

1, in the client will you want to send the content (file anything can) chunked, each block of content is numbered, and then sent;

2, the service side after receiving your chunked data, according to your client data content number re-assemble;

3, in general, when we send data, try to use a smaller block of the way (I have not more than 1024), the data block is too large to send and receive data for a long time, matching problems.

Question 5. Problems with UDP packets

Q: UDP sends two data, first 100 bytes, second 200 bytes, then outsourcing once recvfrom (1000), received 100, or 200, or 300?

A: UDP is the data packet protocol, is in packet mode, so each can receive 100,200, ideally, the first time is no matter how much recvfrom is received to 100. Of course, perhaps due to network reasons, the second package first arrived, it may be 200. May be due to the network cause of disorderly order, so you may receive 200, so the custom UDP packet header to add a serial number, identity send and receive packets corresponding.

Question 6. TCP's packet problem

Q: Similarly, if you switch to TCP, send 100 bytes for the first time, 200 bytes for the second time, and recv (1000) What will be received?

A: TCP is a streaming protocol, so recv (1000), will receive a TCP self-processed retransmission, to ensure the integrity of the packet

Question 7: If there are shards, the following processing

Q: If the MTU is 1500, use UDP to send 2000, then Recvfrom (2000) is received 1500, or 2000?

Answer: Or receive 2000, the data shard is processed by the IP layer, put to UDP or a complete package. Packets received are fragmented by the least MTU on the routing path, and note that the transfer to UDP is already assembled (the package with the error is discarded by the CRC checksum error) and is a complete packet.

Issue 8: Post-shard processing

Q: What if the 500 piece is lost? UDP is not re-transmitted.

A: UDP has a CRC test, if the package is incomplete will be discarded, and will not notify whether the reception is successful, so UDP is unreliable transport protocol, and TCP does not exist this problem, has its own retransmission mechanism. In the intranet, UDP basically does not have packet loss, reliability or protection. Of course, if it is required to have time series and high reliability, or to go TCP, or to provide self-retransmission and disorderly processing (UDP network packet processing capacity of up to 7m~10m/s)

Issue 9: Different packets connected to the same port are processed

Q: TCP

A-C Hair 100

B, C 200

AB simultaneously with one end port

C recv (1000), how much will you receive?

A: A and C is a TCP connection, B and C is another TCP connection, so different sockets, so separate processing. Each socket has its own receive buffer and send buffer

Question 10: What is a TCP sticky packet

During application development, applications that are based on TCP network transport sometimes appear sticky packets (that is, packets sent by the sender are stuck in a packet when received by the receiver). The detailed explanation is as follows: Because TCP is a stream protocol, for a socket packet, such as Send 10AAAAABBBBB two times, because the network reason is divided into two times for the first time, 10AAAAAB and BBBB, if the packet is read 10 (packet length) and then read into the subsequent data, when the reception is fast, Sent slowly, will appear first received 10AAAAAB, will explain the error, and then received BBBB10AAAAABBBBB, also explain the error situation. This is the sticky packet of TCP.

In network transmission applications, it is usually necessary to customize a protocol on the network protocol encapsulation, the simple way is to send the data before adding a custom header, the packet header can contain data length and some other information, receive the first packet header, and then according to the length of the data described in the packet header to receive the data behind. The detailed method is: first receives the Baotou, in Baotou the designation package body length to receive. Set the check bit at the end of the packet header (such as group Space 0x2 start, 0x3 end to check whether a package is complete). For TCP: 1) There is no packet loss, the wrong packet, so there is no data error 2) If the Baotou detection error, that is illegal or request, directly reset can

In order to avoid sticking, the following measures can be taken.

One is caused by the sender of the sticky packet phenomenon, the user can be programmed to avoid, TCP provides a mandatory data transfer immediately after the operation instruction Push,tcp software received the operation instruction, the data is sent out immediately, without waiting for the transmission buffer full;

Second, for the receiver caused by the adhesive package, you can optimize the program design, reduce the workload of the receiving process, improve the priority of the receiving process and other measures, so that they receive data in a timely manner, so as to avoid the occurrence of sticky packet phenomenon;

Three is controlled by the receiver, a packet of data by the structure of the field, the human control sub-multiple received, and then merged, through this means to avoid sticky packets.

11. Diagram The 3-time handshake of TCP to establish a connection, 4 handshake release connection?

TCP is a connection-oriented protocol. A transport connection is used to transmit a TCP message. The establishment and release of TCP transport connections is an essential process in every connection-oriented communication. Therefore, the transport connection is made up of three phases: connection establishment, data transfer, and connection release.

here the syn=synchronization,syn=1,ack=0, which represents the connection request message segment, agrees to establish the connection then syn=1,ack=1, connecting all the ack=1.

three-time handshake (Three-way handshake) solution solves the problem caused by the loss, storage, and duplication of the network layer. Imagine a problem that might occur without a three-time handshake?

As shown, if the handshake is only 2 times, the following problems may occur:

Host a sent packets due to network reasons, there has been stranded, that is, the delay reached the HOSTB. At this point, B thought Hosta sent a request, so he sent a confirmation message to Hosta to establish a connection. The hosta receives the message, because it does not initiate a connection request to HOSTB, and therefore does not respond to HOSTB confirmation, nor sends data to HOSTB. And at this time B think has been established connected, waiting for hosta sent data, resulting in HOSTB resources wasted!

Connection release:

Part of the content of the reference to the "computer network", online search content.

TCP/UDP detailed

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.