The returned values of recv () received data in the FD_READ event of the WSAAsynSelect () Model

Source: Internet
Author: User

When using the WSAAsynSelect () model, is the recv () return value discussed during the FD_READ event?

C/C ++ codedo {int nRecvStat = recv (m_sockID, pchBuf, nBufLen, 0); int nErrorCode =: WSAGetLastError () ;} while (nRecvStat & gt; 0 );
NRecvStat & gt; 0 is naturally the data actually read from the SOCKET buffer. Now we will discuss nRecvStat
1. Under what circumstances does nRecvStat = 0?
2. Under what circumstances does nRecvStat & lt; 0;
3. In the case of nRecvStat & lt; 0, what is the error of nErrorCode = WSAEWOULDBLOCK (10035? Let's explain it to you.
4. In the case of nRecvStat & lt; 0, what is the error of nErrorCode = WSAEMSGSIZE (10040?
5. What do you want to add!

PS: Remember to use the WSAAsynSelect () model.

 

 

The connection has been disconnected.

2. Under what circumstances does nRecvStat & lt; 0;
No data is received.

3. In the case of nRecvStat & lt; 0, what is the error of nErrorCode = WSAEWOULDBLOCK (10035? Let's explain it to you.
The socket is set to non-blocking, but there is no data currently.

4. In the case of nRecvStat & lt; 0, what is the error of nErrorCode = WSAEMSGSIZE (10040?
When UDP is used, the buffer is too small and the received data is incomplete.

5. What do you want to add!
Add them downstairs.

 

 

1. when the WSAAsynSelect () model receives data with FD_READ, the system will send the FD_READ message to your program as long as the data can be read. It is "Superfluous" to use loops to receive data ", in addition, messages sent by many systems to your program may be lost or blocked. only the blocking socket model will use your data receiving method. The socket called by WSAAsynSelect () is not blocked.

2. in the WSAAsynSelect () model, when recv () or WSARecv () is used to receive data (when an FD_READ message is triggered), recv () or WSARecv () is impossible to return 0 values. when FD_READ exists, the data is already in the buffer of your system. Even if the remote socket is disconnected, you can still receive the data, unless your system is too busy or has no paging memory usage, or you disconnect the socket with closesocket (), you can call recv () After receiving the FD_READ message () or WSARecv (), it is impossible to return the value of & lt; = 0.

 

 

1. After the data reaches the socket, and never triggers FD_READ (that is, the initial stage)
2. After the data arrives at the socket and the previous recv () is called
3. After calling recv (), the buffer zone still has unfinished data

The 3rd process is as follows:
1.100 bytes data arrives, and winsock2 sends FD_READ
2. The program uses recv () to read-only 50 bytes, with 50 bytes left.
3. winsock2 continues to send the FD_READ message

Recv () returns WSAEWOULDBLOCK:
1. When data arrives, it is triggered by FD_READ. The message is added to the Message Queue of the program.
2. Before processing the message, the program will recv () the data.
3. When the FD_READ message is processed, the program calls recv () and returns WSAEWOULDBLOCK (because the data is recv () before this)

Note:
1. After winsock2 issues an FD_READ, if the program does not use recv (), even if there is still data that does not receive FD_READ, it will not trigger another FD_READ. It will not be sent until the recv () is called.
2. for a FD_READ recv () multiple times: if the program attempts to recv () multiple times for one FD_READ, multiple blank FD_READ will be triggered, so the program recv () for 2nd times () disable FD_READ (you can use WSAAsynSelect to disable FD_READ) and recv () multiple times ().
3. The recv () returns messages such as WSAECONNABORTED, WSAECONNRESET..., which can be processed by the FD_CLOSE event.

 

Reference the reply from jiefangtw on the 5th floor:
FD_READ event trigger conditions:
1. After the data reaches the socket, and never triggers FD_READ (that is, the initial stage)
2. After the data arrives at the socket and the previous recv () is called
3. After calling recv (), the buffer zone still has unfinished data

The 3rd process is as follows:
1.100 bytes data arrives, and winsock2 sends FD_READ
2. The program uses recv () to read-only 50 bytes, with 50 bytes left.
3. winsock2 continues to send the FD_READ message

Recv () returns WSAEWOULDBLOCK:
1. When data arrives, it is triggered by FD_READ. The message is added to the process...

Since you know the above rules, your code will still use loops to receive data?

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.