`

解决 Socket API错误代码:WSAECONNRESET (10054)

阅读更多

http://support.microsoft.com/kb/263823/en-us

http://hi.baidu.com/jetqu2003/blog/item/397700031435e9703812bbcc.html

 

用Socket API 的 UDP做了一个c/s结构的通信程序,client启动后会向server发送一个请求,而server启动后也会向每个记录的client发送一个请 求,当server和client在同一台机器上时,server端发送给client端(实际是自己的地址)请求后,server自己会收到该请求,并 报错:recvfrom error 10054。(但实际上server 在sendto时,指定的是client的port,而非server的port)。解决办法:

1.头文件中加入下面代码:
#include <Winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#define IOC_VENDOR 0x18000000
#define _WSAIOW(x,y) (IOC_IN|(x)|(y))
#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)

2.在创建socket之后加入下面代码:
DWORD dwBytesReturned = 0;
BOOL bNewBehavior = FALSE;
DWORD status;

status = WSAIoctl(m_hSock, SIO_UDP_CONNRESET,
&bNewBehavior,
sizeof (bNewBehavior),
NULL, 0, &dwBytesReturned,
NULL, NULL);

----------------------------------------------
具体原理参见下面这篇MSDN的文章  
Knowledge Base Articles
-------------------------------------------------------
    
WinSock Recvfrom() Now Returns WSAECONNRESET Instead of Blocking or Timing Out
Q263823


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Windows 2000 Professional
Microsoft Windows 2000 Server
Microsoft Windows 2000 Advanced Server

--------------------------------------------------------------------------------


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.



RESOLUTION
To resolve this problem, obtain the latest service pack for Windows 2000. For additional information, please see the following article in the Microsoft Knowledge Base:

Q260910 How to Obtain the Latest Windows 2000 Service Pack
A new sockets IOCTL called "SIO_UDP_CONNRESET" has been introduced in Windows 2000. When this IOCTL is used, the program must be rewritten specifically for Windows 2000 to obtain the original Windows NT 4.0 behavior. Windows NT 4.0, Microsoft Windows 95, and Microsoft Windows 98 have no support for this new IOCTL. In addition to rewriting your application, you will need the hotfix referenced further down in this article.

The following code snippet demonstrates a technique that can be used to call WSAIoctl with the SIO_UDP_CONNRESET control code to obtain the original Windows NT 4.0 behavior:

DWORDdwBytesReturned = 0;
BOOLbNewBehavior = FALSE;
DWORDstatus;// disable new behavior using
// IOCTL: SIO_UDP_CONNRESET
status = WSAIoctl(sd, SIO_UDP_CONNRESET,
&bNewBehavior, sizeof(bNewBehavior),
NULL, 0, &dwBytesReturned,
NULL, NULL);if (SOCKET_ERROR == status)
{
DWORD dwErr = WSAGetLastError();
if (WSAEWOULDBLOCK == dwErr)
{
// nothing to do
return(FALSE);
}
else
{
printf("WSAIoctl(SIO_UDP_CONNRESET) Error: %d\n", dwErr);
return(FALSE);
}
}
To be able to compile this code, you need either to have the latest Mswsock.h which includes the definition of SIO_UDP_CONNRESET or to insert below definition of it directly into your code:

// MS Transport Provider IOCTL to control
// reporting PORT_UNREACHABLE messages
// on UDP sockets via recv/WSARecv/etc.
// Path TRUE in input buffer to enable (default if supported),
// FALSE to disable.
#defineSIO_UDP_CONNRESET_WSAIOW(IOC_VENDOR,12)
NOTE: The hotfix that is described in this article will not resolve the problem unless the program is rewritten to use the new SIO_UDP_CONNRESET IOCTL.

In order to compile this code, it is necessary to have the latest Platform SDK installed on your computer. This is available from the following MSDN Web site:
http:\\msdn.microsoft.com


For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
Q263823 WinSock Recvfrom() Now Returns WSAECONNRESET Instead of Blocking or Timing Out



STATUS
Microsoft has confirmed this to be a problem in the Microsoft products that are listed at the beginning of this article. This problem was first corrected in Windows 2000 Service Pack 2.



MORE INFORMATION
For additional information about how to install Windows 2000 and Windows 2000 hotfixes at the same time, click the article number below to view the article in the Microsoft Knowledge Base:

Q249149 Installing Microsoft Windows 2000 and Windows 2000 Hotfixes

Additional query words:

Keywords : kbWin2000PreSP2Fix kbWin2000SP2Fix
Issue type : kbbug
Technology : kbwin2000AdvServ kbwin2000AdvServSearch kbwin2000Serv kbwin2000ServSearch kbwin2000Search kbwin2000ProSearch kbwin2000Pro kbWinAdvServSearch

分享到:
评论

相关推荐

    windows socket错误码及出错原因

    26. WSAECONNRESET (10054):该错误码表示连接被重置。 27. WSAENOBUFS (10055):该错误码表示缓冲区不足。 28. WSAEISCONN (10056):该错误码表示已经连接。 了解这些错误码可以帮助开发者更好地处理网络错误和...

    Vc.rar_VC socket_VC Socket_Vc_socket_socket vc

    8. **错误处理**:Socket编程中常见的错误如WSAECONNRESET、WSAEWOULDBLOCK等,需要正确处理这些错误以确保程序的健壮性。 9. **多线程与异步编程**:在VC++中,可以利用多线程或者异步I/O模型(如WSAAsyncSelect或...

    VC下SOCKET编程实例(客户端)源代码

    在Windows系统中,Winsock是SOCKET API的实现,它遵循BSD套接字模型并为开发者提供了创建TCP/IP网络应用的能力。 在VC下进行SOCKET编程,首先需要初始化Winsock。这通常通过调用`WSAStartup`函数完成,该函数会加载...

    VC下SOCKET编程实例源代码

    错误检查是必不可少的,常见的错误如WSAECONNREFUSED(连接被拒绝)、WSAECONNRESET(连接被重置)等,都需要进行适当的处理。 在源代码实例中,可能会包括以下部分: 1. 客户端程序:创建SOCKET,连接到服务器,...

    异步socket通信例程

    8. **错误处理**:在整个过程中,要时刻关注并处理可能出现的错误,如WSAEWOULDBLOCK、WSAECONNRESET等,这些错误信息可以帮助我们定位和解决问题。 异步Socket通信例程的代码通常包含以下几个部分:初始化Winsock...

    GetLastError & WSAGetLastError & WSAGETSELECTERROR 返回值大全

    这些函数在遇到问题时返回一个错误代码,该代码可以被用来识别并解决程序中的错误。下面我们将详细介绍这三个函数以及它们返回的错误值。 1. **GetLastError**: `GetLastError` 是 Windows API 的一个函数,用于...

    wince版socket 通信

    例如,`WSAGetLastError()`可以获取最近的错误代码,`WSACleanup()`用于在程序结束时清理Winsock环境。常见的错误如WSAECONNRESET表示连接被远程主机重置,WSAEWOULDBLOCK表示当前操作不允许,需要重试。 **七、...

    精通windows socket网络编程VC++

    6. **错误处理**:了解常见的网络错误,如WSAEWOULDBLOCK(操作将阻塞)、WSAECONNRESET(连接被重置)等,以及如何通过`WSAGetLastError()`获取错误信息。 7. **套接字选项和控制消息**:使用`setsockopt()`和`...

    winsock2网络编程及源码

    7. **错误处理**:Winsock2提供了丰富的错误代码,如WSAECONNRESET、WSAEWOULDBLOCK等,可以帮助开发者诊断并解决网络编程中遇到的问题。 8. **套接字选项**:`setsockopt`和`getsockopt`函数允许设置或获取套接字...

    计算机网络高级编程技术

    Winsock错误代码(如WSAEINVAL、WSAECONNRESET等)提供了关于网络通信失败原因的详细信息,帮助开发者调试程序,解决网络连接问题。 6. **《WINDOWS网络编程技术》**:这本书可能深入介绍了上述概念,并提供了详细...

    windows_socket编程实战

    Winsock提供了一套错误代码,如WSAECONNRESET、WSAEWOULDBLOCK等,开发者需要适当地捕获和处理这些错误。 9. **多线程与异步编程**:对于高性能服务器,通常会使用多线程或多进程处理并发连接,或者利用异步I/O模型...

    BFT.rar_BFT

    - 在Winsock编程中,错误检查至关重要,常见的错误代码如WSAECONNRESET、WSAEWOULDBLOCK等需要适当地处理。 - VC6的调试器可以用来追踪代码执行,查看内存状态,以及设置断点,帮助找出程序中的问题。 通过深入...

    Windows Socket进阶

    本节将深入探讨Windows Socket(Winsock)API的使用,以及在实际开发中可能遇到的各种挑战和解决方案。 首先,我们了解Socket的基础概念。Socket是进程间通信(IPC)的一种方式,特别是在网络环境下,它允许不同...

    WINDOWS网络编程技术

    4. **错误处理**:Windows Socket API提供了丰富的错误代码,如WSAECONNRESET表示连接被远程主机重置,WSAEWOULDBLOCK表示操作将阻塞等。开发者需要正确处理这些错误,确保程序的健壮性。 **高级特性** 1. **多...

    Windows网络编程(第二版) 中英文 附源代码

    5. **错误处理与调试**:Windows网络编程中,理解并正确处理各种网络错误是必不可少的,例如WSAEWOULDBLOCK、WSAECONNRESET等。同时,学会使用调试工具如Visual Studio Debugger,可以帮助定位和解决问题。 6. **...

    mfc socket 客户端/服务器 实例

    CAsyncSocket类提供了各种错误代码,例如WSAECONNRESET表示连接被远程主机重置,WSAENETDOWN表示网络子系统失效等。在编程过程中,我们需要捕获并适当地处理这些错误。 五、关闭与清理 在完成通信后,记得关闭套接...

    WinSocket 网络高级实现教程

    WinSocket提供了丰富的错误代码,如WSAECONNRESET表示连接被重置等,需要根据这些错误进行适当的处理。 通过学习本教程,你将掌握WinSocket高级实现技巧,能够独立构建一个功能完备的网络聊天室,从而进一步提升你...

    Windows网络与通信程序设计

    理解并正确处理Winsock API的错误代码,如WSAECONNRESET、WSAEWOULDBLOCK等,可以帮助定位和解决问题。同时,使用调试工具如Visual Studio的调试器或第三方网络监控工具,可以辅助分析网络通信中的问题。 8. **网络...

    WinSocket.rar

    Winsock提供了一系列的错误代码,如WSAECONNREFUSED(连接被拒绝),WSAECONNRESET(连接被重置)等,开发者需要根据这些错误码进行适当的错误处理。 9. **多线程与异步编程** 在实际应用中,可能需要处理多个并发...

Global site tag (gtag.js) - Google Analytics