Jeeva
Jeeva

Reputation: 4663

Determining local IP address in P2P application

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

Answers (1)

Len Holgate
Len Holgate

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

Related Questions