套接字突然无法绑定?! [英] Socket suddenly not binding?!
问题描述
大家好,
打扰一下,但是我今天正在开发一个简单的套接字程序(来自Beej的出色著作( book ).一切正常很好,但随后套接字突然拒绝绑定,现在bind命令不断返回-1.
我尝试过等待套接字释放(按照Beej的建议),并再次设置了套接字并关闭了它,但这似乎也无济于事.我还尝试了一些不同的端口(从2000-5500开始)并重新启动系统.如果我使用IP地址"127.0.0.1",它可以很好地绑定.
该程序在运行Ubuntu的远程系统(PC 104)上运行. bind()似乎在我的SSH同事进入同一台计算机的同时停止绑定,却没有意识到我正在使用它.这可能是引起问题的原因吗?
这是我的代码,这是输出:
-Server_new函数已启动
-套接字描述符设置
-绑定错误-1
-errno:无法分配请求的地址
-插座关闭
有什么想法为什么我不能分配所请求的地址?否则还有什么可能...
谢谢,
广告
非常感谢,
广告
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;
}
推荐答案
您不能将套接字绑定到外部IP地址.如果164.11.116.39
不是正在运行程序的计算机的网络接口中的IP,则将无法将套接字绑定到该IP.
问候
Hi,
you cannot bind your socket to a foreign IP address. If164.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
这篇关于套接字突然无法绑定?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!