WSAGetLastError()返回码

WSAGetLastError()返回码

Socket error 0 - Directly send error

Socket error 10004 - Interrupted function call

Socket error 10013 - Permission denied
Socket error 10014 - Bad address
Socket error 10022 - Invalid argument
Socket error 10024 - Too many open files
Socket error 10035 - Resource temporarily unavailable
Socket error 10036 - Operation now in progress
Socket error 10037 - Operation already in progress
Socket error 10038 - Socket operation on non-socket
Socket error 10039 - Destination address required
Socket error 10040 - Message too long
Socket error 10041 - Protocol wrong type for socket
Socket error 10042 - Bad protocol option
Socket error 10043 - Protocol not supported
Socket error 10044 - Socket type not supported
Socket error 10045 - Operation not supported
Socket error 10046 - Protocol family not supported
Socket error 10047 - Address family not supported by protocol family
Socket error 10048 - Address already in use
Socket error 10049 - Cannot assign requested address
Socket error 10050 - Network is down
Socket error 10051 - Network is unreachable
Socket error 10052 - Network dropped connection on reset
Socket error 10053 - Software caused connection abort
Socket error 10054 - Connection reset by peer
Socket error 10055 - No buffer space available
Socket error 10056 - Socket is already connected
Socket error 10057 - Socket is not connected
Socket error 10058 - Cannot send after socket shutdown
Socket error 10060 - Connection timed out
Socket error 10061 - Connection refused
Socket error 10064 - Host is down
Socket error 10065 - No route to host
Socket error 10067 - Too many processes
Socket error 10091 - Network subsystem is unavailable
Socket error 10092 - WINSOCK.DLL version out of range
Socket error 10093 - Successful WSAStartup not yet performed
Socket error 10094 - Graceful shutdown in progress
Socket error 11001 - Host not found
Socket error 11002 - Non-authoritative host not found
Socket error 11003 - This is a non-recoverable error
Socket error 11004 - Valid name, no data record of requested type
WSAEADDRINUSE (10048) Address already in use
WSAECONNABORTED (10053) Software caused connection abort
WSAECONNREFUSED (10061) Connection refused
WSAECONNRESET (10054) Connection reset by peer
WSAEDESTADDRREQ (10039) Destination address required
WSAEHOSTUNREACH (10065) No route to host
WSAEMFILE (10024) Too many open files
WSAENETDOWN (10050) Network is down
WSAENETRESET (10052) Network dropped connection
WSAENOBUFS (10055) No buffer space available
WSAENETUNREACH (10051) Network is unreachable
WSAETIMEDOUT (10060) Connection timed out
WSAHOST_NOT_FOUND (11001) Host not found
WSASYSNOTREADY (10091) Network sub-system is unavailable
WSANOTINITIALISED (10093) WSAStartup() not performed
WSANO_DATA (11004) Valid name, no data of that type
WSANO_RECOVERY (11003) Non-recoverable query error
WSATRY_AGAIN (11002) Non-authoritative host found
WSAVERNOTSUPPORTED (10092) Wrong WinSock DLL version
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Windows 平台上,`recvfrom` 函数在发生错误时会返回 `-1`。此时,可以使用 `WSAGetLastError` 函数获取错误,然后根据错误进行调试和处理。 以下是一个简单的示例代,可以用来测试 `recvfrom` 函数的返回值: ```c++ #include <winsock2.h> #include <iostream> #define BUF_SIZE 1024 int main() { // 初始化 Winsock 库 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { std::cerr << "Failed to initialize Winsock." << std::endl; return 1; } // 创建套接字 SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock == INVALID_SOCKET) { std::cerr << "Failed to create socket." << std::endl; WSACleanup(); return 1; } // 绑定端口 sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(1234); addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sock, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) { std::cerr << "Failed to bind socket." << std::endl; closesocket(sock); WSACleanup(); return 1; } // 接收数据 char buf[BUF_SIZE]; sockaddr_in clientAddr; int clientAddrLen = sizeof(clientAddr); int recvLen = recvfrom(sock, buf, BUF_SIZE, 0, (sockaddr*)&clientAddr, &clientAddrLen); if (recvLen == SOCKET_ERROR) { int errCode = WSAGetLastError(); std::cerr << "Failed to receive data: " << errCode << std::endl; closesocket(sock); WSACleanup(); return 1; } // 输出接收到的数据 buf[recvLen] = '\0'; std::cout << "Received data: " << buf << std::endl; // 关闭套接字 closesocket(sock); WSACleanup(); return 0; } ``` 如果 `recvfrom` 函数返回 `-1`,则可以根据错误进行调试和处理。常见的错误包括: - `WSAECONNRESET`:连接被重置,通常是对方已经关闭了连接。 - `WSAEINTR`:函数被信号中断。 - `WSAEINVAL`:参数无效。 - `WSAEMSGSIZE`:接收到的消息太大,无法放入缓冲区。 - `WSAEWOULDBLOCK`:套接字处于非阻塞模式,没有数据可用。 - `WSANOTINITIALISED`:未初始化 Winsock 库。 根据具体的错误,可以采取不同的处理方式,例如重新尝试接收数据、关闭套接字等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值