chkim
1
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..
#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.
chkim
3
------------------------------------------------------------------------
Thank you !!
but, result is incorect.
ha1
The name :: ha1
IP Address : 255.255.255.255
result is same.
Replace
struct hostent * Host = (struct hostent * ) malloc ( sizeof ( struct hostent ));
with
struct hostent* Host;
and you should be OK.