Reputation: 4663
I am developing an P2P application where a peers talk to the server to inform its Private and Public IP. The application uses UDP for communication.
To get the private IP the client uses gethostbyname
and bind it to that IP. The problem is when the system has more than one NIC. The problem is when one of the NIC is not connected to internet. So to avoid it i am using INADDR_ANY
and bind it.
Now i need to get my local IP address to inform to the server. Is there any API which will tell me which IP address of the NIC is active?
Upvotes: 2
Views: 607
Reputation: 21644
You need to bind to an explicit IP address rather than INADDR_ANY
as binding to INADDR_ANY
will mean that calling getsockname()
on the socket to get the local address will simply return INADDR_ANY
.
So, what you need to do is iterate the available endpoints (using getaddrinfo()
) and create a socket on each. These will then give you the correct address if you call getsockname()
on them later.
Upvotes: 1