Results 1 to 3 of 3
hii.. I am new to unix network programming.I have a trouble in running the code for fetching the ip address of host. Can you please help me out in getting ...
- 02-05-2009 #1Linux Newbie
- Join Date
- Nov 2005
- Posts
- 172
Need help in running gethostbyname
hii.. I am new to unix network programming.I have a trouble in running the code for fetching the ip address of host. Can you please help me out in getting it done. The following is the code
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
struct hostent *he;
struct in_addr a;
int
main (int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "usage: %s hostname\n", argv[0]);
return 1;
}
he = gethostbyname (argv[1]);
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
herror(argv[0]);
return 0;
}
->Please correct me if the program is wrong....
-> the compilation was fine. I am unable to get the output if i give the hostname.
Can anyone help me ?
- 02-06-2009 #2Linux Newbie
- Join Date
- Nov 2005
- Posts
- 172
I am getting segmentation fault...
please help...
thanks in advance
- 02-06-2009 #3Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695


Reply With Quote