![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 22
| how to get local ip adress 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 | |
| | #2 |
| CSharpener Join Date: Oct 2006
Posts: 5,328
| read about gethostname
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #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 | |
| | #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 | |
| | #5 |
| CSharpener Join Date: Oct 2006
Posts: 5,328
| 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 | |
| | #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);
|
| wwwnuwan is offline | |
| | #7 | |
| CSharpener Join Date: Oct 2006
Posts: 5,328
| Quote:
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 | |
| | #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;
}
|
| wwwnuwan is offline | |
| | #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 | |
| | #10 |
| Registered User Join Date: Mar 2009
Posts: 22
| sory its 127.0.1.1 |
| wwwnuwan is offline | |
| | #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 | |
| | #12 |
| Senior software engineer 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 | |
| | #13 |
| Registered User 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |