Workaround for Windows UDP socket recvfrom return 10054 error

Source: Internet
Author: User



Phenomenon:



On a Windows 7 system, a uses a UDP socket, which calls the SendTo function to send data to a destination address B, but the destination address B does not receive data, and if a immediately calls Recvfrom to attempt to receive the data sent back by the destination address B, Recvfrom will return immediately to -1,wsagetlasterror () return 10045.



Reason:



The above phenomenon is a bug in Windows Sockets, when the UDP socket receives an unreachable ICMP packet after a send, this error will be returned in the next receive, so the above socket returns SOCKET_ERROR in the next receive. The error is 10054.



Workaround:



Use WSAIoctl to set the operating mode of the UDP socket to ignore this error. The following are the specific practices:


#include <Winsock2.h>
#include <Mstcpip.h>
#include <stdio.h>

#pragma comment(lib, "ws2_32.lib")
#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR, 12)

BOOL bNewBehavior = FALSE;
DWORD dwBytesReturned = 0;
WSAIoctl(iSock, SIO_UDP_CONNRESET, &bNewBehavior, sizeof bNewBehavior, NULL, 0, &dwBytesReturned, NULL, NULL);


Sio_udp_connrest options: Controls whether UDP port_unreachable messages is reported. Set to TRUE to enable reporting. Set to FALSE to disable reporting.



Note:


    1. SetSockOpt is to modify the properties of the socket interface, but the set of interfaces in the process of the need to use some of the parameters, WSAIoctl is to modify the working mode of the socket interface, more define how this set of interfaces to work in the form of, there are essential differences.


Reference documents:



[1]. http://blog.csdn.net/wpullo/article/details/5905616
[2]. http://msdn.microsoft.com/zh-cn/ms741621
[3]. http://blog.sina.com.cn/s/blog_536e955201009xqp.html
[4]. http://blog.csdn.net/threewall/article/details/5089817



Workaround for Windows UDP socket recvfrom return 10054 error


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.