Insufficient one-time receipt of socket receive data

Source: Internet
Author: User

Posted on staid reading (150) Comments (2) EDIT favorites

At the sender end, 4092 bytes are sent at a time,

At the receiving end, 4092 bytes are received at a time,

However, at the receiving end, socket. Receive may occasionally receive incomplete data,

Ret = socktemp. Receive (bbuffer, ibufferlen, 0); // you may not be able to receive all the data!
0 <RET <ibufferlen must be considered: continue to receive ibufferlen-ret bytes, and then merge

The length of send and Recv of socket:

A packet has no fixed length, and the Ethernet is limited to 1500 bytes. Is the MTU of the Ethernet. If the packet exceeds this value, TCP sets an offset for IP datagram for multipart transmission, currently, the application layer is generally allowed to set a buffer of 8 K (NTFS system). 8 K of data is split from the underlying layer, and the application layer seems to be sending only one time.
Windows Buffer experience is 4 kb.
The socket itself is divided into two types: stream (TCP) and datagram (UDP). Your problem is different for these two types of use. It is even related to whether you use blocking or non-blocking socket for programming.
1. The communication length is determined by yourself. The system does not force you to send a large package. The actual length should be determined based on your needs and network conditions. For TCP, the length can be larger, but you must know that the default size of the buffer for sending and receiving in the socket is about 8 K. You can use setsockopt to change the length. But for UDP, it should not be too large, generally between 1024 and 10 K. Note that, no matter how large a package is, the IP layer and the link layer will send your package in parts. Generally, the LAN is about 1500, And the Wan is only several dozen bytes. The fragmented package will arrive at the receiver through different routes. For UDP, if one of the parts is lost, the IP layer of the receiver will discard the entire packet, which leads to packet loss. Obviously, if a UDP packet is too large to be sent, the link layer will have a high chance of losing parts after it is split. If you have UDP packet, it will be easy to lose, but it is too small and will affect the efficiency. It is best to configure this value to adjust to the optimal state according to different environments.
The send () function returns the actual sending length. When the network keeps, it will never return an (failed to send) error. A maximum of 0 is returned. For TCP, you can write a circular message. When the send function returns socket_error, it indicates an error. But for UDP, do not write loop transmission, otherwise it will bring great trouble to your reception. Therefore, UDP needs to use setsockopt to change the buffer size inside the socket to accommodate your sending packets. Specifically, as a stream, the packet sending process does not reach the entire package, but is continuously sent, so the receiver must group packets. UDP, as a message or datagram, must arrive at the receiver in the whole package.
2. Generally, packet sending has packet boundary. The first thing you need to do is to let the receiver know the length of the packet, so there is a packet header. For TCP, the receiver first receives the packet header information and then receives the packet data. You can collect the entire package at a time. You can verify whether the result is collected. This completes the group package process. UDP, you can only receive the entire package. If the Received Buffer you provide is too small, TCP returns the actual length of the received buffer, and the rest can be received. The difference between UDP is that the remaining data is discarded and the wsaemsgsize error is returned. Note TCP: If the buffer buffers you provide are large, you may receive multiple sending packets, and you must separate them. When the buffer is too small, you cannot close the data in the socket at a time, therefore, the socket receives the event (onreceive) and may not be triggered again. pay close attention to this when receiving the event. These features reflect the difference between a stream and a data packet.
In addition, receiving buffsize> = Sending buffsize> = actual sending size applies to both internal and external buffers. The above section describes the buffer size relationship inside the socket.
3. The number of TCP requests is equal to the number of requests received. If the socket Recv is not blocked of course, the system waits until there is data or the socket is not blocked, but wsaewouldblock is returned. UDP. If no data exists, blocking the socket will wait. If the socket is not blocked, wsaewouldblock will be returned. If there is data, it will wait until the entire package arrives, and receive the entire package before returning.

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.