无法分配请求的地址,C++ UDP套接字

发布于 2024-12-04 02:45:54 字数 1130 浏览 2 评论 0原文

以下代码来自 http://digitalpbk.blogspot。 com/2007/10/unix-networking-sockets-udp-transmitter.html,它在本地主机上运行良好,但是当我将其更改为我的ip时它给出了错误

bind(): Can't allocate requested address

我一直在寻找解决方案几个小时,所以任何帮助都会很棒

int main(void)
{

 struct sockaddr_in sin;
 char msg[10000];
 int ret;
 int sin_length;


 int s;

 s = socket(AF_INET, SOCK_DGRAM, 0);
 if(!s)
 {
  perror("socket()");
  return 0;
 }
 sin.sin_family = AF_INET;
 sin.sin_port = htons(65000);
 sin.sin_addr.s_addr = inet_addr("24.212.11.211"); // ---------------- This line ----------------
 if(bind(s, (struct sockaddr *)&sin, sizeof(sin)))
 {
  perror("bind()");
  return 1;
 }


 do  // I think the following might be a problem
 {
  sin_length = sizeof(sin);
  ret = recvfrom(s, msg, 10000, 0, (struct sockaddr *)&sin, (socklen_t*) &sin_length);
//Waits until a message is recieved...
  printf("Message[%s:%d] : %s\n",
  inet_ntoa(sin.sin_addr), sin.sin_port,msg);
 }
 while(msg[0]!='0');

 close(s);
 return 0;
}

The following code is from http://digitalpbk.blogspot.com/2007/10/unix-networking-sockets-udp-transmitter.html, It runs fine on localhost but when I change it to my ip it gives the error

bind(): Can't assign requested address

I have been searching for a solution for a few hours so any help would be great

int main(void)
{

 struct sockaddr_in sin;
 char msg[10000];
 int ret;
 int sin_length;


 int s;

 s = socket(AF_INET, SOCK_DGRAM, 0);
 if(!s)
 {
  perror("socket()");
  return 0;
 }
 sin.sin_family = AF_INET;
 sin.sin_port = htons(65000);
 sin.sin_addr.s_addr = inet_addr("24.212.11.211"); // ---------------- This line ----------------
 if(bind(s, (struct sockaddr *)&sin, sizeof(sin)))
 {
  perror("bind()");
  return 1;
 }


 do  // I think the following might be a problem
 {
  sin_length = sizeof(sin);
  ret = recvfrom(s, msg, 10000, 0, (struct sockaddr *)&sin, (socklen_t*) &sin_length);
//Waits until a message is recieved...
  printf("Message[%s:%d] : %s\n",
  inet_ntoa(sin.sin_addr), sin.sin_port,msg);
 }
 while(msg[0]!='0');

 close(s);
 return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花心好男孩 2024-12-11 02:45:54

最有可能的是,您将其更改为人们认为您的 IP,而不是您认为自己的 IP。 (假设函数采用的参数为窗口)转到开始 ->控制面板-> (网络和互联网 ->)网络连接/更改网络适配器设置,然后右键单击您的网络适配器并选择状态,查看 IP 地址,这就是您应该在代码中使用的地址。如果您不想担心这一点,您可以随时使用 BIND TO ALL THE THINGS 地址 0.0.0.0。

Most likely you're changing it to the IP that people see you as, not the IP you see yourself as. (Assuming windows from the parameters the functions take) Go to start -> control panel -> (Network & Internet ->) network connection/change network adapter settings and then right click on your network adapter and select status, look at IP address, that is the one you should be using in your code. If you don't want to worry about that, you can always use the BIND TO ALL THE THINGS address, 0.0.0.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文