gpt4 book ai didi

c - 为什么 inet_ntoa 总是返回错误的 IP 地址(套接字编程公司)

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:15 25 4
gpt4 key购买 nike

我有带套接字的简单客户端/服务器应用程序,因此客户端使用服务器 ip 连接到服务器,我想使用 inet_ntoa 返回已连接客户端的 ip,但它总是返回已连接客户端的错误 ip。为什么 ?以及如何解决。

我希望服务器返回连接机器的真实ip,以便稍后扫描开放端口

最佳答案

struct sockaddr_in addr_remote;
...
connfd = accept(sockfd, (SA*)&cli, &len);
...
printf( " Welcome %s " , inet_ntoa(addr_remote.sin_addr));

您将 inet_ntoaaddr_remote 一起使用。 addr_remote 永远不会在您的代码中设置,因此内容有点不可预测。您可能打算在 accept 中使用 addr_remote 但您在那里使用了 cli

关于c - 为什么 inet_ntoa 总是返回错误的 IP 地址(套接字编程公司),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55572331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com