Boost asio socket:如何获取连接的IP、端口地址?
我有一个使用 boost asio 的 TCP 服务器。我已接受套接字连接。如何获取与我的服务器通信的机器的IP、端口?
顺便说一句:是否可以获取有关连接的服务器用户看到我的 server4 机器的 IP 的信息?
I have a TCP server using boost asio. I have accepted a socket connection. How to get IP, Port of machine my server is communicating with?
BTW: Is it possible to get info on what ip that connected server user sees my server4 machine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以这样获取IP和端口:
You can get the IP and port like this:
如果您在remote_endpoint中收到错误文件描述符错误,您仍然可以参考下面的链接。
http://www.boost.org /doc/libs/1_51_0/doc/html/boost_asio/reference/basic_socket_acceptor/accept.html
“接受新连接并获取对等点的端点。”部分会对你有帮助。
您可以使用如下
still if you get Bad File Descriptor error in remote_endpoint you can refer to below link.
http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/basic_socket_acceptor/accept.html
"Accept a new connection and obtain the endpoint of the peer." part will be helpful to you.
You can use as below
我无法对原始答案发表评论。我只想提一下,投票最高的答案存在一些问题:
socket().remote_endpoint()
可能会抛出boost::system::system_error
。所以记得处理异常,否则你的程序可能会崩溃。我花了很多时间来调试这个问题。I cannot comment on the original answer. I just want to mention that there's some problem with the top voted answer:
socket().remote_endpoint()
may throwboost::system::system_error
. So remember to handle the exception or your program may crash. It took me many hours to debug this problem.http://www.boost.org /doc/libs/1_52_0/doc/html/boost_asio/reference/ip__tcp/endpoint.html
我没有这方面的经验,但看起来地址和端口成员函数应该可以解决问题
(编辑最新的增强版)
http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/reference/ip__tcp/endpoint.html
i don't have experience in it, but it looks like address and port member functions should do the trick
(edit for latest Boost version)