0

I have a problem binding the socket. There is no error as such, not sure why if fails on if condition.

int result = WSAStartup(MAKEWORD(2,2), &wsadata);
if(result != NO_ERROR)
{
    printf("\nThere is a problem at WSAStartup");
}
else
{
    printf("\nWSAStartup was ok");
}
list_sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if(list_sock == -1)
{
    printf("\n Socket not created %d\n", list_sock);
}
else
{
    printf("\nSocket was created Succesfully");
}
HOSTENT *thishost;
char *ip;
u_short port;
port = 55555;
thishost = gethostbyname("localhost");
printf("\n");
printf("\nHost name is:%s ",thishost->h_name);
ip = inet_ntoa(*(struct in_addr*)*thishost->h_addr_list);
printf("\nip address is %s\n ", ip);
printf("Address type is:%i",thishost->h_addrtype);
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(ip);
service.sin_port = htons(port);
if(bind(list_sock,(SOCKADDR *)&service,sizeof(service))== SOCKET_ERROR)
{
    printf("Error during bind %s", strerror(errno));
}
else
{
    printf("All done");
}
WSACleanup();

return 0;

I get error at line

if(bind(list_sock,(SOCKADDR *)&service,sizeof(service))== SOCKET_ERROR)

But strerror(errno)) gives me: "No error". But it still fails on if condition and does not bind.

Any help?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1411601
  • 23
  • 2
  • 8
  • 6
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms737550(v=vs.85).aspx : "If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, and **a specific error code can be retrieved by calling WSAGetLastError**." – Mat Jun 20 '13 at 14:12
  • ok, I got it. It fails with 10014... and I am on 64 bit system..will it make a difference? – user1411601 Jun 20 '13 at 16:25
  • Well, did you look up what that error means? What investigation did you do then? – Mat Jun 20 '13 at 16:47
  • Well! I looked at http://stackoverflow.com/questions/861154/winsock-error-code-10014. I guess, i has something to do with 64bit and 32bit,we it looked more probable for me..you suggest anything? – user1411601 Jun 20 '13 at 17:54
  • Your error has nothing to do with 32bit vs 64bit at all. It has to do with a logic bug in your code. See my answer for details. – Remy Lebeau Jun 20 '13 at 18:34

1 Answers1

1

10014 is WSAEFAULT, which bind() returns when:

The system detected an invalid pointer address in attempting to use a pointer argument in a call.

This error is returned if the name parameter is NULL, the name or namelen parameter is not a valid part of the user address space, the namelen parameter is too small, the name parameter contains an incorrect address format for the associated address family, or the first two bytes of the memory block specified by name do not match the address family associated with the socket descriptor s.

You are creating an IPv6 socket (AF_INET6), but you are trying to bind it to an IPv4 (AF_INET) address. That will not work. You need to either create an IPv4 socket, or bind to an IPv6 address, depending on what you are trying to accomplish.

Lastly, don't use gethostbyname("localhost"). First, it only supports IPv4. Second, if you want to bind to the loopback IP, explicitly bind to 127.0.0.1 (IPv4) or ::1 (IPv6), which you can hard-code or at least use the INETADDR_SETLOOPBACK() macro in mstcpip.h. If you want to bind to all available IPs, bind to INADDR_ANY (IPv4) or IN6ADDR_ANY (IPv6), which you can hard-code or at least use the INETADDR_SETANY() macro in mstcpip.h. If you really want to use a function call, use getaddrinfo() instead, which supports both IPv4 and IPv6.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770