|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Get ip address
Hi
I am trying to get IP address of of my Irix server from my C program running on the host machine. ------------------------------------------------ #include <unistd.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> main() { struct in_addr ia; printf("host id %u\n", gethostid()); ia.s_addr = htonl(gethostid()); printf("My address: %s\n", inet_ntoa(ia)); exit(0); } -------------------------------------------------------------------- result # a.out host id 3555268269 My address: 255.255.255.255 __________________________________________ result is incorrect. help me plz.. |
| Sponsored Links | |
|
|
|
#2
|
|||
|
|||
|
#include <stdio.h>
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> main ( ) { char Buf [ 200 ] ; struct hostent * Host = (struct hostent * ) malloc ( sizeof ( struct hostent )) ; gethostname ( Buf , 200 ) ; printf ( "%s\n", Buf ) ; Host = ( struct hostent * ) gethostbyname ( Buf ) ; printf ( "The name :: %s\n" , Host->h_name ) ; printf("IP Address : %s\n", inet_ntoa(*((struct in_addr *)Host->h_addr))); } This module correctly prints the address. |
| Sponsored Links | ||
|
|
|
#3
|
|||
|
|||
|
Quote:
Thank you !! but, result is incorect. ha1 The name :: ha1 IP Address : 255.255.255.255 result is same. |
|
#4
|
|||
|
|||
|
Replace
struct hostent * Host = (struct hostent * ) malloc ( sizeof ( struct hostent )); with struct hostent* Host; and you should be OK. |
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| SCO get MAC Address | jcarter2333 | Programming | 11 | 02-08-2007 08:51 AM |
| How to Achive IP address through MAC(Ethernet) address | krishnacins | IP Networking | 3 | 08-29-2005 08:45 PM |
| ce0 and ce1 have the same mac address | BG_JrAdmin | Solaris | 2 | 07-08-2005 07:30 PM |
| network address and broadcast address? | pnxi | UNIX for Dummies Questions & Answers | 7 | 11-10-2003 10:29 PM |
| IP address | krishna | UNIX for Advanced & Expert Users | 10 | 03-20-2002 04:36 AM |
|
|