当我分配这个地址时,它说无法分配请求的地址
。但是当我输入本地地址 (127.0.0.1) 时,它会接受它。为什么???
char* hostname = "192.168.1.8";
int sockfd;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
char buf[MAXBUFLEN];
int port =5000;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
try
{
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_addr.s_addr = inet_addr(hostname);
printf("Accepted/n");
// automatically fill with my IP
my_addr.sin_port = htons(5000); // short, network byte order
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
while (1)
{
addr_len = sizeof(struct sockaddr);
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
//printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
//printf("packet is %d bytes long\n",numbytes);
buf[numbytes] = '\0';
//printf("packet contains \"%s\"\n",buf);
}
close(sockfd);
}
catch(...)
{
最佳答案
如果错误发生在 bind
上(根据您的问题内容,由于您陈述的错误消息未出现在代码中,因此错误并不明显),很可能是因为地址是不可用。
这通常是因为它已经在使用中,或者在当前主机上不可用。
除了少数异常(exception),您通常只能绑定(bind)到分配给本地接口(interface)的 IP 地址。您应该检查 192.168.1.8
是否在该类中。假设 127.0.0.1
将是一个本地接口(interface)(因此它可以工作),并且 INADDR_ANY
也可以工作 - 这可能是您应该使用的“地址”除非您确实有特定需要将自己限制在一个界面上。
您应该检查失败函数后的 errno
并将其与 possibilities 匹配.
顺便说一句,这可能与您的问题无关,您初始化 sockaddr_in
结构(设置字段然后清除其余部分)的方式对我来说似乎不太便携。
我认为清除该批处理然后简单地设置您想要的内容会更安全,例如:
memset (&my_addr, 0, sizeof (my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = inet_addr (hostname);
my_addr.sin_port = htons (5000);
至少这样,结构中字段的顺序不会影响您的代码。
您可以通过以下代码看到问题。首先,必要的 header :
#define __USE_GNU
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
然后是参数检查和套接字创建。
int main (int argc, char *argv[]) {
int sockfd;
struct sockaddr_in me;
if (argc < 2) {
printf ("Need argument with IP address\n");
return 1;
}
if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
return 1;
}
然后是绑定(bind)本身:
memset (&me, 0, sizeof (me));
me.sin_family = AF_INET;
me.sin_addr.s_addr = inet_addr (argv[1]);
me.sin_port = htons (5000);
if (bind (sockfd, (struct sockaddr *)&me, sizeof(struct sockaddr)) == -1)
{
fprintf (stderr, "errno = %d ", errno);
perror("bind");
exit(1);
}
close(sockfd);
return 0;
}
当您使用某些参数运行它时,您可以看到它适用于 IP 地址属于本地接口(interface)(127.0.0.1
和 192.168.0.101
)的那些) 但不适合那些不这样做的,例如 192.168.0.102
:
pax> ifconfig | grep 'inet addr'
inet addr:192.168.0.101 Bcast:192.168.0.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0
inet addr:192.168.99.1 Bcast:192.168.99.255 Mask:255.255.255.0
inet addr:192.168.72.1 Bcast:192.168.72.255 Mask:255.255.255.0
pax> ./testprog 127.0.0.1
pax> ./testprog 192.168.0.101
pax> ./testprog 192.168.0.102
errno = 99 bind: Cannot assign requested address
pax> grep '#define.*99' /usr/include/asm-generic/errno.h
#define EADDRNOTAVAIL 99 /* Cannot assign requested address */
并且,从上面的 bind
手册页的链接,我们看到:
EADDRNOTAVAIL
A nonexistent interface was requested or the requested address was not local.
关于c - 我在 Linux (Centos) 下的 C 中运行时出现错误 "Cannot assign requested address",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8799216/