简体   繁体   English

接受后如何确定使用哪个IP地址?

[英]How to determine which IP address is in use after accept?

If I wrote a server bound to a wildcard address ( INADDR_ANY ), how to determine which IP address the client is connected to? 如果我写了一个绑定到通配符地址( INADDR_ANY )的服务器,如何确定客户端连接到哪个IP地址?

I tried the following code when the after a successful accept call, but it just returned 0.0.0.0. 在成功接受调用之后我尝试了以下代码,但它只返回0.0.0.0。

inet_ntop(AF_INET, &server_address.sin_addr, s, sizeof(s));

As stated in How to determine IP used by client connecting to INADDR_ANY listener socket in C , use getsockname , which gives you the socket which the connection is bound to (on local level). 如何确定客户端连接到C中的INADDR_ANY侦听器套接字所使用的IP ,请使用getsockname ,它为您提供连接所绑定的套接字(在本地级别)。

This is for C, but is applicable for C++ to. 这适用于C,但适用于C ++。

the accept system call will have a address of type struct sockaddr_in or struct sockaddr_in6. accept系统调用将具有struct sockaddr_in或struct sockaddr_in6类型的地址。

the client address will be stored there. 客户地址将存储在那里。

you can get it using inet_ntoa, or inet_ntop. 你可以使用inet_ntoa或inet_ntop来获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM