0

Source:

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; 
}

Everytime I run this in my program I get:

getsockopt failed with error: 10049

10049 is the error code for WSAEADDRNOTAVAIL which means "The requested address is not valid in its context."

Please help, I have no clue why I keep getting this error.

Ian
  • 218
  • 3
  • 11

1 Answers1

0

You have specified an invalid protocol in your call to WSASocket() try specifying IPPROTO_TCP as:

WSASocket(AF_INET,SOCK_RAW,IPPROTO_TCP, 0, 0, WSA_FLAG_OVERLAPPED)
Michael Shmalko
  • 710
  • 4
  • 16
  • 1
    The code you have posted does not trigger an error for me with VS 2010 debug mode... So parameters and use of API is correct. Can you post full code? – Michael Shmalko Nov 26 '12 at 00:15
  • I can't post the rest of the code for security issues (not to sound shady lol), but wouldn't the rest of the code not make a difference because there aren't any variables or anything used in this context that I haven't addressed here. The only thing I can think of is that this code is taken from a thread, that was launched off the main program. But all that threading is done correctly because there haven't been any errors from it logically or syntactically. – Ian Nov 26 '12 at 01:26
  • I was reading a little bit more into error code 10049 from msdn and they say "Cannot assign requested address. The requested address is not valid in its context. This normally results from an attempt to bind to an address that is not valid for the local computer." So could it be a firewall issue or just an issue with my computer? But I should also note that I have connected and used other sockets perfectly in other parts of my main program. – Ian Nov 26 '12 at 01:28
  • 1
    yes, looks like an issue with your test box. Can you built a test harness to test the code in isolation? And you would be able to test it on different machines as well... – Michael Shmalko Nov 26 '12 at 02:34