C Get IP address for server listening

I am writing a client / server program using C sockets. I point out that the server can listen on any network interface using INADDR_ANY in sockaddr_in.sin_addr.s_addr. This is equivalent to IP 0.0.0.0. Is it possible to get the actual IP that the server is listening on? (for example 192.168.1.100)

+3


source to share


2 answers


I was finally able to find a solution that works .

Edit : The link is dead, so see the link online .



Hope this can be helpful to others, as it did to me.

+1


source


When you bind a listening socket to INADDR_ANY

, the socket listens on all available local IPs. Cannot determine from socket which IP (s) it is listening on. If you need this information, you need to specify the local IP addresses separately (in which case you could just bind()

each IP address in its own socket individually if you need to get the preliminary binding data). However, once the accept()

client connection has been established, you can use getsockname()

on the accepted socket to find out which specific IP address the connection received.



+1


source







All Articles