UDP的recvfrom产生10054的错误

这个是Windows的问题。

当UDP Socket在某次发送后收到一个不可到达的ICMP包时,这个错误将在下一个接收中返回。所以上面的套接字在下一次的接收中返回了SOCKET_ERROR,错误是10054

解决方法:

简单的忽略这个就可以了,对后续的调用没有影响。

//防止sendto不能到达后recvfrom返回-1并且获得10054错误.
/*
SYMPTOMS:
In Windows 2000, a User Datagram Protocol (UDP) program may not work and may generate a
WSAECONNRESET response.
CAUSE:
If sending a datagram using the sendto function results in an "ICMP port unreachable"
response and the select function is set for readfds, the program returns 1 and the subsequent
call to the recvfrom function does not work with a WSAECONNRESET (10054) error response.
In Microsoft Windows NT 4.0, this situation causes the select function to block or time out.
//用来防止q263823(见MSDN)(UDP中sendto发送的消息到不了指定的IP&PORT后使用recvfrom的话会返回-1,报错10054)
*/
DWORD ? dwBytesReturned ? = ? 0;
BOOL ? ? bNewBehavior ? = ? FALSE;
DWORD ? status;
status = WSAIoctl(mainSocket, SIO_UDP_CONNRESET,&bNewBehavior,sizeof(bNewBehavior),NULL,0,&dwBytesReturned,NULL,NULL);

Comments are closed.