C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 03-25-2009, 10:50 PM   #1
Registered User
 
Join Date: Mar 2009
Posts: 22
how to get local ip adress

Hi,
How can I get local IP adress within a C program?

I tried this with gethostname function but it can not be helped since i dnt know the host name of the machine.

Thanks
wwwnuwan is offline   Reply With Quote
Old 03-25-2009, 10:57 PM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,328
Quote:
Originally Posted by wwwnuwan View Post
Hi,
How can I get local IP adress within a C program?

I tried this with gethostname function but it can not be helped since i dnt know the host name of the machine.

Thanks
read about gethostname
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-25-2009, 11:07 PM   #3
Registered User
 
Join Date: Mar 2009
Posts: 22
i tried with gethostname.
but it returns the hostname like "myLaptop".
I just need the ip like 192.168.10.10
my prev code was like as follows
Code:
    memset( hostname , 0 , MAXHOSTNAMELEN );
    gethostname( hostname,MAXHOSTNAMELEN );
    printf("MAXHOSTNAMELEN = %i \n " , MAXHOSTNAMELEN);
wwwnuwan is offline   Reply With Quote
Old 03-25-2009, 11:38 PM   #4
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
Passing your hostname to gethostbyname() should work. Depending on how your system is set up though, this may just return "127.0.0.1".
bithub is offline   Reply With Quote
Old 03-26-2009, 12:06 AM   #5
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,328
Quote:
Originally Posted by bithub View Post
Passing your hostname to gethostbyname() should work. Depending on how your system is set up though, this may just return "127.0.0.1".
you get a list of IPs - one of it could be 127.0.0.1, you should iterate through the list to find all local IPs
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-26-2009, 01:01 AM   #6
Registered User
 
Join Date: Mar 2009
Posts: 22
could you please give a some code example for this?

now my programe is as follows.
Code:
    char *hostname = malloc(MAXHOSTNAMELEN );
    memset( hostname , 0 , MAXHOSTNAMELEN );
    gethostname( hostname,MAXHOSTNAMELEN );
    struct hostent hostn = mallock(sizeof(struct hostent));
    gethostbyname(hostname);
here then how can i get the ip and print it onto the console?
wwwnuwan is offline   Reply With Quote
Old 03-26-2009, 01:11 AM   #7
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,328
Quote:
Originally Posted by wwwnuwan View Post
could you please give a some code example for this?

now my programe is as follows.
Code:
    char *hostname = malloc(MAXHOSTNAMELEN );
    memset( hostname , 0 , MAXHOSTNAMELEN );
    gethostname( hostname,MAXHOSTNAMELEN );
    struct hostent hostn = mallock(sizeof(struct hostent));
    gethostbyname(hostname);
here then how can i get the ip and print it onto the console?
There is a lot of samples in the net...

Winsock Programmer's FAQ: Get the Local IP Address(es) - for example
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-26-2009, 05:07 AM   #8
Registered User
 
Join Date: Mar 2009
Posts: 22
Hi,
Finaly i found the fellowing programs to obtain the local ip .
but it returns 127.0.1.1
please advice what goes wrong with this?

Code:
struct hostent *he;
struct in_addr a;

  int main (int argc)
{
    char *hostname = malloc(MAXHOSTNAMELEN );
    memset( hostname , 0 , MAXHOSTNAMELEN );
    gethostname( hostname,MAXHOSTNAMELEN );
    printf("hostname = %s \n " , hostname) ;
  he = gethostbyname (hostname);
  if (he)
  {
    printf("name: %s\n", he->h_name);
    while (*he->h_aliases)
      printf("alias: %s\n", *he->h_aliases++);
    while (*he->h_addr_list)
    {
      bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
      printf("address: %s\n", inet_ntoa(a));
    }
  }
  else
  printf("error \n");
    //herror(argv[0]);
  return 0;
}
Thanks.
wwwnuwan is offline   Reply With Quote
Old 03-26-2009, 05:47 AM   #9
Registered User
 
Join Date: Mar 2009
Posts: 22
I think this has no way to work out since my machine name (which I get by gethostname() method) is not in a DNS.
so I think it returns 127.0.0.1

any ideas about it?

Thanks.
wwwnuwan is offline   Reply With Quote
Old 03-26-2009, 05:48 AM   #10
Registered User
 
Join Date: Mar 2009
Posts: 22
sory its 127.0.1.1
wwwnuwan is offline   Reply With Quote
Old 03-26-2009, 12:39 PM   #11
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
Are you on windows or linux?

On linux, gethostbyname() will just return whatever is in /etc/hosts. You can always add a line to the hosts file which has your correct IP address.

Another option is to run "ipconfig" or "ifconfig" (depending on your OS), and parse out the result.
bithub is offline   Reply With Quote
Old 03-26-2009, 12:51 PM   #12
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
What you want is the address of the actual interface. You can't really get at this information in a portable way.

gethostbyname() is portable, but if it returns multiple answers you have no way of knowing which one is correct.

Honestly, I'd bite the bullet and use a non-portable method to query the network interface and obtain its address directly.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 03-31-2009, 08:07 AM   #13
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 575
Going with bithub's suggestion will be your best bet, especially if you have many interfaces, say a dual homed server with tied up vlans to them.
slingerland3g is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM
How to get multiple local IP addresses without gethostbyname() Aidman Linux Programming 1 09-11-2004 08:52 AM
getting local ip (not internet ip) ipe C Programming 12 03-17-2003 03:10 PM
How do you find the local IP without getting the loopback? BrianK C++ Programming 2 10-16-2002 08:32 PM


All times are GMT -6. The time now is 02:10 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22