Go Back   The UNIX and Linux Forums > Top Forums > Programming
Search Forums:



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-25-2002
Registered User
 

Join Date: Apr 2002
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 04-26-2002
Registered User
 

Join Date: Jan 2002
Location: India
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
#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  
Old 04-28-2002
Registered User
 

Join Date: Apr 2002
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally posted by S.P.Prasad
#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.
------------------------------------------------------------------------
Thank you !!
but, result is incorect.

ha1
The name :: ha1
IP Address : 255.255.255.255

result is same.
    #4  
Old 05-01-2002
Registered User
 

Join Date: May 2002
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Replace

struct hostent * Host = (struct hostent * ) malloc ( sizeof ( struct hostent ));

with

struct hostent* Host;

and you should be OK.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 11:19 PM.