0

I am running a TCP/IP server and binding it to INADDR_ANY (0.0.0.0). My network is multi-homed so has multiple local IP addresses. So by using INADDR_ANY, the client can connect to me using any of those local addresses.

I now want to construct a full url with my local IP address. I will send this address to the client and (assuming no NAT-traversal), the client should be able to use the sent address to connect to my server. What socket API can I use to find this address? This address has to be used in the IP packet, so should be discoverable.

doron
  • 27,972
  • 12
  • 65
  • 103

1 Answers1

0

There is no common socket API function across platforms for this type of query. You have to use OS-specific APIs instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • getifaddrs etc will give me all the my ip addresses, I however need to find out the one used by the remote end currently connecting to me – doron Feb 07 '20 at 11:59
  • @doron if a client is already connected to your TCP server, then `accept()` and `getpeername()` give you the client's IP, and `getsockname()` on the socket returned by `accept()` gives you the IP the client connected to. But if the client is already connected, why do you need to send a URL to the client to connect to? If the client needs to make a secondary connection to your server, it can just use the same IP it already used earlier. Your question doesn't make sense. Please clarify your scenario. – Remy Lebeau Feb 07 '20 at 18:01