setsockopt错误WSAEADDRNOTAVAIL

时间:2012-11-25 20:26:03

标签: c++ c winapi sockets setsockopt

来源:

WSADATA WSAData; 
SOCKET sock;
if (WSAStartup(MAKEWORD(2,2), &WSAData)!=0) {
    printf("\nProblem with WSAStartup\n\n");
    return FALSE; 
}
if ((sock = WSASocket(AF_INET,SOCK_RAW,IPPROTO_TCP, 0, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) {
    WSACleanup();
    printf("\nProblem with WSASocket\n\n");
    return FALSE;
}
int flag = 1;
if (setsockopt(sock,IPPROTO_IP, IP_HDRINCL,(char *)&flag,sizeof(flag)) == SOCKET_ERROR) {
    printf("getsockopt failed with error: %u\n\n", WSAGetLastError());
    closesocket(sock);
    WSACleanup();
    return FALSE; 
}

每次我在我的程序中运行时都会得到:

getsockopt failed with error: 10049

10049是WSAEADDRNOTAVAIL的错误代码,表示“请求的地址在其上下文中无效。”

请帮忙,我不知道为什么我一直收到这个错误。

1 个答案:

答案 0 :(得分:0)

您在致电WSASocket()时指定了无效的协议,请尝试将IPPROTO_TCP指定为:

WSASocket(AF_INET,SOCK_RAW,IPPROTO_TCP, 0, 0, WSA_FLAG_OVERLAPPED)