Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the IP address of a network interface

Status
Not open for further replies.

maluk

Programmer
Oct 12, 2002
79
0
0
SG
Hi!

I am having a bit of a problem getting the local address of my computer. For example, my IP is something like [tt]192.68.1.100[/tt]. When a do a [tt]ifconfig -a[/tt], I can see that eth0 is assigned the said IP. So naturally, I tried getting the IP using the socket APIs. However, no matter how hard I try, I always keep on getting [tt]127.0.0.1[/tt] or any of the loopback addresses. I can't get the [tt]192.68.1.100[/tt]. What should I do to get the said IP using socket APIs.

NOTE: [tt]192.68.1.100[/tt] is not defined in my /etc/hosts file. Will this be a problem? However, I need to get the [tt]192.68.1.100[/tt] even though it was not specified in the /etc/hosts file.

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
Help! Anyone?

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
Where is a socket connection code snippet?..
 
Hi!
Sorry for the late reply.

Here is the snippet.

Code:
   int ret;
   struct hostent *phost;
   struct in_addr addr;
   char hostname[100];

   if(pIPAddrs == NULL)
      return -1; /* Need to find suitable return value */
   ret = gethostname(hostname, 100);
   if(ret == 0)
   {
      phost = gethostbyname(hostname);
      if(phost == NULL)
         return -1; /* Need to define a return value if made part of rtsrc */
      memcpy(&addr, phost->h_addr_list[0], sizeof(struct in_addr));
      strcpy(pIPAddrs, inet_ntoa(addr));
     
   }
   else{
      return -1;
   }

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top