什么ip地址接受退货
请参阅以下代码:
accept(sockfd, (struct sockaddr*)&cliaddr, &slen);
cout << inet_ntop(AF_INET, cliaddr.sin_addr, ipv4addr, 100);
我的客户端从本地主机连接。 我在输出中得到一个荒谬的地址。这不是我的 IP 地址。每次我运行代码时我都会得到不同的IP地址。当我 ping 该 IP 地址时,没有得到任何响应。
原因是什么。
我在 Windows Vista 的虚拟机上运行 suse linux。
更新:
bzero(&cliaddr, sizeof(cliaddr));
int connfd = accept(sockfd, (struct sockaddr*)&cliaddr, &slen);
if (sem_wait(&mutex) < 0)
err_sys("sem_init error");
char ipv4addr[100];
cout << inet_ntop(AF_INET, &cliaddr.sin_addr, ipv4addr, 100) << endl;
//const char* p = inet_ntop(AF_INET, &cliaddr.sin_addr, ipv4addr, 100);
//cout << p << endl;
//cout << (void*)p << " " << (void*)ipv4addr << endl;
如果我取消注释这些行,这将返回地址 0.0.0.0
,我会在所有行中得到正确的地址,127.0.0.1
see the following code:
accept(sockfd, (struct sockaddr*)&cliaddr, &slen);
cout << inet_ntop(AF_INET, cliaddr.sin_addr, ipv4addr, 100);
my client connects from localhost.
i get an absurd address in the output. this is not my ip address. everytime i run the code i get a different ip address. when i ping that ip address i don't get any response.
what is the reason.
i am running suse linux on a virtual machine in windows vista.
Update:
bzero(&cliaddr, sizeof(cliaddr));
int connfd = accept(sockfd, (struct sockaddr*)&cliaddr, &slen);
if (sem_wait(&mutex) < 0)
err_sys("sem_init error");
char ipv4addr[100];
cout << inet_ntop(AF_INET, &cliaddr.sin_addr, ipv4addr, 100) << endl;
//const char* p = inet_ntop(AF_INET, &cliaddr.sin_addr, ipv4addr, 100);
//cout << p << endl;
//cout << (void*)p << " " << (void*)ipv4addr << endl;
this returns address as 0.0.0.0
if i uncomment the lines, i get the correct address in all the lines, 127.0.0.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在调用
inet_ntop()
时缺少第四个参数。这是一个有效的示例:与网络通信时始终检查错误。
You are missing the 4th parameter in your call to
inet_ntop()
. Here's a working example:Always check for errors when talking to network.
我未经证实的猜测是,您返回的是 IP v6 地址而不是 v4 地址,因此您的转换已关闭。
您可能想尝试使用 netstat 找出客户端的端口(通常会得到 1025 到 65535 之间的随机端口号),并查看该端口的十六进制值是否出现在 cliaddr 的十六进制表示形式中的某个位置。如果客户端端口与您认为的客户端地址之间存在关联,则您的转换不正确。
My unsubstantiated guess is that you're getting IP v6 addresses back instead of v4, so your conversion is off.
You might want to try using netstat to find out the client's port (you usually get a sort-of-random port number between 1025 and 65535) and see if the hex value of that appears somewhere in the hex representation of cliaddr. If there's a correlation between client port and what you believe to be the client address, then your conversion is incorrect.
我的下一个猜测:
是cout。<<足够聪明来取消引用正在返回的指针,或者您正在打印指针?
My next guess:
Is cout.<< clever enough to dereference the pointer that's being returned, or are you printing out the pointer?