开发者

IP address/MAC of a client

开发者 https://www.devze.com 2023-03-26 08:27 出处:网络
Is it possible to get the IP address of client machine from sockaddr. From client I mean a system or browser using which a user is connected to a server.

Is it possible to get the IP address of client machine from sockaddr.

From client I mean a system or browser using which a user is connected to a server.

I assume that client is behind some proxy/routers and is not directly c开发者_如何学运维onnected to the server.


You can get the IP address that the server sends it response packets to. Often this is the IP address of the client computer, but it doesn't have to be:

  • IPv4 NAT devices (often used by corporate networks) hides the client's IP address and exposes an address of the NAT device instead.
  • 6-to-4 translation. If the client is a native IPv6-only client your IPv4 server will see the IPv4 address of the 6-to-4 gateway that provides IPv4 connectivity.
  • Proxies where the client isn't directly connected to the Internet, but rather connects to a proxy server that performs requests on behalf of the server. In this case the server will see the proxy servers address.

If you need the IP address of the client computer, the client will have to send that as data to the server. Please note that there is no guarantee that the address you receive this way is the real one. Even if it is the real one, it cannot be guaranteed to be unique (many corporate networks use addresses in the 192.168.X.X series reserved for internal use). There is also no very likely that you won't be able to connect to the client on it's internal address.


Check this answer to a the same question (if i understand your question correctly) Getting IPV4 address from a sockaddr structure

socket.h states:

int accept (int socket, struct sockaddr *address,
                                 socklen_t *address_len);

address: Either a null pointer, or a pointer to a sockaddr structure where the address of the connecting socket will be returned.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号