sockets - Bind failed with Invalid argument error -


i'm trying write simple program uses bind. code compiles on solaris, bind() failed invalid argument error.

i have tried many times getting same error, i'm unable figure out error.

here code :

int my_func () {     struct sockaddr_storage name;     struct addrinfo hints, *res;     int status;      char hostnamestr[256];     gethostname (hostnamestr, 256);      int irc;      memset(&hints, 0, sizeof(hints));      hints.ai_family = af_unspec;     hints.ai_socktype = sock_stream;      if ((status = getaddrinfo(hostnamestr, null, &hints, &res) != 0))     {         printf("getaddrinfo: %s\n", gai_strerror(status));         return 1;     }      memcpy(&name, res->ai_addr, res->ai_addrlen);     freeaddrinfo(res);     monsocket = socket (name.ss_family, sock_stream, ipproto_tcp);     int ilasterror = wsagetlasterror ();      if (monsocket == -1)     {         fprintf (stderr, "cannot open socket monitors [%s]\n", wsaerror (ilasterror));         return 1;     }      sockopts(monsocket);       int flags;     flags = fcntl (monsocket, f_getfl);     ilasterror = wsagetlasterror ();      if (flags < 0)     {         fprintf (stderr, "cannot flags socket [%s]\n", wsaerror (ilasterror));         return 1;     }      irc = fcntl (monsocket, f_setfl, flags | o_nonblock);     ilasterror = wsagetlasterror ();      if (irc == -1)     {         fprintf (stderr, "cannot set flags socket [%s]\n", wsaerror (ilasterror));         return 1;     }      int iport;     if(name.ss_family == af_inet)     {         ((struct sockaddr_in *) &name)->sin_port = htons (8091);     }         else if(name.ss_family == af_inet6)     {         ((struct sockaddr_in6 *) &name)->sin6_port = htons (8091);     }      struct  sockaddr_in* ipv = (struct sockaddr_in *) &name;     printf("connecting addr: %s\n", inet_ntoa(ipv->sin_addr));      irc = bind (monsocket, (struct sockaddr *) &name, sizeof (name));     ilasterror = wsagetlasterror ();      if (irc == -1)     {         close(monsocket);         monsocket=-1;         fprintf (stderr, "could not bind port %d\n", iport);         printf("error %s",wsaerror (ilasterror));         return 1;     }     else     {         fprintf (sched_show_msg, "bound on port %i\n", ntohs (iport));     }     irc = listen (monsocket, somaxconn);     ilasterror = wsagetlasterror ();      if (irc == -1)     {         fprintf (stderr, "could not listen socket [%s]\n", wsaerror (ilasterror));         return 1;     }      return 0; } 

bind (monsocket, (struct sockaddr *) &name, sizeof (name)) 

your sizeof(name) yields size of generic sockaddr_storage size. need size of sockaddr_in or sockaddr_in6.

in case, you'd better of by using stuff getaddrinfo directly:

monsocket = socket(res->ai_family, res->ai_socktype, res->ai_protocol); bind(monsocket, res->ai_addr, res->ai_addrlen); 

in order you'll have move freeaddrinfo(res) call.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -