networking – How to get my non-loopback network ip address in C? – Education Career Blog

For a communication between two hosts, I need to send the IP address of my host to the other site. The problem is that if I request my IP address, it might be that I get back my local loopback IP addres (127.x.x.x) , not the network (ethernet) IP address.

I use the following code:

char myhostname32;

gethostname(myhostname, 32);
hp = gethostbyname(myhostname);
unsigned my_ip = *(unsigned*)(hp->h_addr);

if( (my_ip % 256) == 127) {
  /* Wrong IP adress as it's 127.x.x.x */
  printf("Error, local IP address!");

The only way to solve it is to make sure my hostname in /etc/hosts is behind the real network address, not the local loopback (the default for e.g. Ubuntu).

Is there a way to solve this without relying on the content of /etc/hosts?

Edit: I changed the above code so it makes use of getaddrinfo, but I still get back the loopback device’s number (127.0,0,1) instead of the real IP address:

struct addrinfo hint = {0};
struct addrinfo *aip = NULL;
unsigned ip = 0;
struct sockaddr_in *sinp = NULL;

hint.ai_family = AF_INET; /* IPv4 */
hint.ai_socktype = SOCK_STREAM;

if(getaddrinfo(hostname, NULL, &hint, &aip) != 0) {
    return 0;
sinp = (struct sockaddr_in *) aip->ai_addr;
ip   = *(unsigned *) &sinp->sin_addr;

(I used to get back a list of 3 addrinfo’s with the three SOCK_STREAM,SOCK_DGRAM and SOCK_RAW, but the hint prevents that)

So my question still stands…


There is POSIX function getaddrinfo() that returns linked list of addresses for given hostname, so you just need to go through that list and find non-loopback address.

See man getaddrinfo.


Not an answer, but a relevant comment: be aware that as soon as you start sending addressing information in the content of packets, you run the risk of making your application unable to work across NAT:ing routers and/or through firewalls.

These technologies rely on the information in IP packet headers to keep track of the traffic, and if applications exchange addressing information inside packets, where they remain invisible to this inspection, they might break.

Of course, this might be totally irrelevant to your application, but I thought it worth pointing out in this context.


The originating address will be included in the packet sent… there’s no need to duplicate this information. It’s obtained when accepting the communication from the remote host (see beej’s guide to networking, specifically the part on accept())


I just ran into a situation where when only /etc/hosts has information in it and when I used getaddrinfo to get the IP address list, it returned each time. As it turned out, the hostname was aliased to localhost…something often easy to overlook. Here’s what happened:

The /etc/hosts file: localhost.localdomain localhost foo
::1 localhost6.localdomain6 localhost6 foo bie bletch

So, now, when you call getaddrinfo with host=”foo”, it returns 3 times. The error here, is that foo appears both on the line with “” and “”. Once I removed foo from the line with “” things worked fine.

Hope this helps someone.


Look at this:
Discovering public IP programmatically

Note that in some cases a computer can have more than one non-loopback IP address, and in that case the answers to that question tell you how to get the one that is exposed to the internet.


Even if the computer has only one physical network interface (an assumption that may or may not hold, even netbooks have two – ethernet and WLAN), VPNs can add even more IP adresses. Anyway, the host on the other side should be able to determine the IP your host used to contact it.


Use getaddrinfo()


You’re almost there. I’m not sure how you’re getting my_ip from hp.

gethostbyname() returns a pointer to a hostent structure which has an h_addr_list field.

The h_addr_list field is a null-terminated list of all the ip addresses bound to that host.

I think you’re getting the loopback address because it’s the first entry in h_addr_list.

EDIT: It should work something like this:

gethostname(myhostname, 32);
hp = gethostbyname(myhostname);
unsigned my_ip = *(unsigned*)(hp->h_addr);

for (int i = 0; hp->h_addr_listi != 0; ++i) {
  if (hp->h_addr_listi != INADDR_LOOPBACK) {
    // hp->addr_listi is a non-loopback address
// no address found


If /etc/hosts is still there and still the same, looking for all entries of h_addr_list won’t help.


Your new code hardwires the use of IPv4 (in the hint.ai_family field) which is a terrible idea.

Apart from that, you’re close, you just should loop through the results of getaddrinfo. Your code just gets the first IP address but there is an aip->ai_next field to follow…

struct addrinfo {
       struct addrinfo *ai_next;       /* next structure in linked list */

Leave a Comment