The returned values of TCP sending interfaces (such as send () and write () are not directly related to the data volume successfully sent to the receiver.

Source: Internet
Author: User
Link: http://blog.csdn.net/ordeder/article/details/17240221
1. TCP sending interface: Send ()

The interfaces for sending data over TCP include send, write, and sendmsg. In the system kernel, these functions have a unified entry, namely sock_sendmsg (). TCP is reliable, so it is easy to misunderstand the TCP sending interface, such as Sn = Send (...); it is incorrectly believed that the value of Sn indicates that data with Sn bytes has been sent to the receiving end. In fact, this is not the case.

We know that TCP sending and receiving are buffered in the kernel (Linux:

  1. Struct sock {
  2. ...
  3. Struct sk_buff_head receive_queue; // The received datagram queue
  4. Struct sk_buff_head write_queue; // The datagram queue to be sent
  5. ...
  6. }

For the sender, the user space calls send (data) and other sending interfaces to send data. The kernel copies the data to the buffer corresponding to the socket in the kernel space, that is, Sock. write_queue. The Return Value of the send () function is only the number of bytes successfully copied in this send () call (user space-> sock Buffer Queue corresponding to the kernel space ). The specific sending and receiving ends are completed by the TCP protocol. Although TCP is reliable, the premise is that the transmission end is connected to the receiving end's network, otherwise you will understand. In this way, if you want to determine whether the receiver has successfully received data by calling send (), you have to look for other methods.

2. Send () blocking and non-blocking

As mentioned above, Sn = Send (data) is responsible for copying data to the kernel space, and the specific sending is completed after TCP (including traffic control ). Blocking and non-blocking are only for the transmission buffer period from empty space copying to kernel space.

1. if the remaining space of the sending buffer is greater than the number of bytes to send data, the data can be successfully copied to the kernel buffer for blocking and non-blocking purposes. The returned Sn is equal to the number of bytes of the data to be sent.

2. If the remaining space of the sending buffer is smaller than the number of bytes to send data,

A. in blocking mode, blocking will be performed. Wait for the idle space in the kernel buffer, and then copy the data to the kernel buffer until all the data is copied. Returns the amount of data in RN.

B. In non-blocking mode, when the kernel sending buffer is filled up, return immediately. RN is equal to the amount of data that has been copied to the sending buffer.

3. How can I determine whether data is successfully received?

1. the receiving end replies to the response message.

This method is more practical, but it is also more practical.

2. calculate the amount of data sent by the sending client sock

Reference: how to determine whether TCP packets are successfully sent

4. Reference:

Research on the send function of TCP

Recv send blocking and non-blocking

How to determine whether TCP packets are successfully sent

The returned values of TCP sending interfaces (such as send () and write () are not directly related to the data volume successfully sent to the receiver.

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.