Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hey Guys,
Excuse the newbie but I was working on a simple sockets program today (from Beej's excellent (book). Everything was working fine but then suddenly the socket refused to bind. Now the bind command constantly returns a -1.

I've tried waiting for the socket to free up (as per Beej's advice) and have set up the socket again and closed it but this doesn't seem to help either. I've also tried a few different ports (from 2000-5500) and restarting the system. It binds fine if I use IP address "127.0.0.1".

The program is running on a remote system (a PC 104) running Ubuntu. bind() seemed to stop binding at the same time as a colleague of mine SSH'd into the same machine without realising I was working on it. Is this likely to be the cause of the probelm?

Here is my code and here is the output:

- Server_new function initiated
- Socket descriptor set up
- Bind error -1
- errno: Cannot assign requested address
- Socket closed

Any ideas why I can't assign the requested address? Or what else might be up...

Thanks,

Ad

Many Thanks,

Ad


<pre lang="midl">int Server_new(int line){
// from Beej's guide
    int sockd, gai_status, bind_status;
    struct addrinfo hints;
    struct addrinfo *res;   // points to results
    mvprintw(line++,0," - Server_new function initiated");
    
// set up hints (addrinfo structure)
    memset(&hints, 0, sizeof hints);    // make sure the struct is empty
    hints.ai_family = AF_UNSPEC;            // set AF_INET
    hints.ai_socktype = SOCK_STREAM;    // TCP stream sockets
    hints.ai_flags = AI_PASSIVE;        // fill in my IP for me
    
// get values from this call and put into res linked list 
    if (gai_status = (getaddrinfo("164.11.116.39", "2554", &hints, &res))!=0){
        // print error if it didn't works
        mvprintw(line++,0," - getaddrinfo() error:%s",gai_strerror(gai_status));
    }

    // use values to set up socket descriptor
    sockd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
 
   // confirm that it worked
    mvprintw(line++,0," - Socket descriptor set up");
 
   // need to bind to port before listening
    bind_status = bind(sockd, res->ai_addr, res->ai_addrlen);
 
   // bind to the port passed in getaddrinfo
    if ( bind_status != 0){;
        mvprintw(line++,0," - Bind error %i", bind_status);
	mvprintw(line++,0," - errno: %s\n", strerror(errno));
    }
    else
    {
    mvprintw(line++,0," - Bound to port");
    }

    close(sockd);
    mvprintw(line++,0," - Socket closed");

    return 0;
}

Posted
Updated 14-Jun-11 7:23am
v2

1 solution

Hi,
you cannot bind your socket to a foreign IP address. If 164.11.116.39 is not the ip from a network interface of a computer on which your program is running, you won't be able to bind a socket to it.
Regards
 
Share this answer
 
Comments
ad_robot 15-Jun-11 8:28am    
Ah! That's so obvious I can't believe I missed it. Clearly I need to read the book in more detail.

Thank you for your prompt answer :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900