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){
int sockd, gai_status, bind_status;
struct addrinfo hints;
struct addrinfo *res;
mvprintw(line++,0," - Server_new function initiated");
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if (gai_status = (getaddrinfo("164.11.116.39", "2554", &hints, &res))!=0){
mvprintw(line++,0," - getaddrinfo() error:%s",gai_strerror(gai_status));
}
sockd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
mvprintw(line++,0," - Socket descriptor set up");
bind_status = bind(sockd, res->ai_addr, res->ai_addrlen);
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;
}