Obtaining Own External IP Address in POSIX C

前端 未结 6 1660
长发绾君心
长发绾君心 2021-01-06 19:48

I\'m looking to obtain my own IP address in order to publish that information in to a Peer-to-Peer network. In POSIX/C we have getaddrinfo(NULL, ...), but this

6条回答
  •  抹茶落季
    2021-01-06 20:42

    Create a UDP socket, bind() to INADDR_ANY, connect() to some address on the internet, then use getsockname() on the socket to find out what address it was assigned.

    Another good solution, if the network's protocol supports it, is to just ask your bootstrapping peers what they think your IP is. This takes care of NATs automatically.

提交回复
热议问题