user3563852
user3563852

Reputation: 23

Error in binding a UDP socket

I have a program which goes this way.

{
              memset(&hints, 0, sizeof(struct addrinfo));
               /* fill the hints struct */
                hints.ai_flags = AI_PASSIVE;
                 hints.ai_socktype = SOCK_STREAM;
                //hints.ai_socktype = SOCK_DGRAM;
                hints.ai_protocol = 0;
                //hints.ai_protocol = IPPROTO_UDP;
                hints.ai_canonname = NULL;
                hints.ai_addr = NULL;
                hints.ai_next = NULL;


                if(iFamily == AF_INET)
                    hints.ai_family = AF_INET;


                else if(iFamily == AF_INET6)
                        hints.ai_family = AF_INET6;

         /* Code for getting gettaddressinfo */

                if(iFamily == AF_INET)
                {

                iRet = bind(SockIPC, res->ai_addr, sizeof(struct sockaddr_in));
                char sBuff[1024];
         sprintf(sBuff, "errno [%d] ", errno);
               fp=fopen("debug.log","a+");
               fprintf(fp,"IPv4 bind error\n ");
               fprintf(fp,"bind error = %s\n",sBuff);
               fclose(fp);
               fflush(stdout);
               }
}

In the above piece of code I am trying to bind a UDP socket created.

I am getting the following bind error

bind error = 266

Which is address already in use.

Can anyone let me know where I am going wrong.

Upvotes: 0

Views: 1469

Answers (1)

user207421
user207421

Reputation: 311039

There's no evidence of a problem here. It's only valid to evaluate errno if the immediately prior system call has returned -1, and perror("bind"); would have been a lot simpler:

if (bind(...) == -1)
{
    perror("bind");
}
else // continue with execution

However there almost certainly was an error, if not 266: EADDRINUSE is 98, not 266. SOCK_STREAM and IPPROTO_UDP don't go together. You need SOCK_DGRAM and IPPROTO_UDP.

Upvotes: 1

Related Questions