c++ - 从远程客户端获取 IP 地址:remote_endpoint:传输端点未连接
问题描述
有人知道如何从套接字获取远程客户端的 IP 地址吗?我试过这个功能:我试过使用这个功能:
std::string s =socket.remote_endpoint().address().to_string();
但即使我可以从客户端 ping 到客户端,我总是会收到以下错误:在抛出 'boost::exception_detail::clone_impl >' 的实例后调用终止什么():remote_endpoint:传输端点未连接 ^[[AAbandon (核心转储)
有什么方法可以检索发件人的IP地址(ps:想在发送数据之前使用它进行检查)
boost::asio::io_service io_service;
boost::asio::ip::udp::socket socket(io_service);
socket.open(boost::asio::ip::udp::v4());
socket.bind(boost::asio::ip::udp::endpoint(
boost::asio::ip::udp::v4(),port));
std::cout << "Ready to receive" << std::endl;
//information about where the packets come from
boost::asio::ip::udp::endpoint sender;
/////retrieve ip
std::string s =socket.remote_endpoint().address().to_string();
std::cout << "The client's ip is:" << s << std::endl;
解决方案
struct sockaddr_in sAddr;
socklen_t sLen = sizeof sAddr;
[your socket] = accept([socket to listen], (struct sockaddr *) &sAddr, &sLen);
printf("%s",inet_ntoa(sAddr.sin_addr));
用你的替换your socket
和。socket to listen
推荐阅读
- angular - 如何以角度从一个组件导航到 2 个不同项目中存在的另一个组件
- java - ConstraintLayout:以编程方式添加 UI 元素
- azure - Azure Service Fabric StatelessService 不调用 OnCloseAsync
- javascript - 将离子日期时间转换为小时和分钟
- amazon-web-services - 将 aws lex 与 amazon comprehend 与 lambda 结合起来是否可行?
- mysql - 在 Group By 语句 MySQL 中包装 Case
- iccube - 将 ssas 多维数据集迁移到 iccube。icCube中有没有与SCOPE()等价的东西?
- angular - 如何使用角度验证自定义验证模式中的文本
- visual-studio-2010 - CSockt 不适用于 OpenSSL Ssl_connect 失败
- android - 如何获取列表的数据
如在全局变量中以 Oncreate() 方法获取数据?