hi all~
**********************************************
>cat hostinfo.c
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char **pp;
struct in_addr addr;
struct hostent *hostp;
if (argc != 2)
{
fprintf(stderr, "usage: %s <domain name or
dotted-decimal>\n", argv[0]);
exit(0);
}
if (inet_aton(argv[1], &addr) != 0)
hostp = gethostbyaddr((const char *)&addr, sizeof(addr),
AF_INET);
else
hostp = gethostbyname(argv[1]);
printf("official hostname: %s\n", hostp->h_name);
for (pp = hostp->h_aliases; *pp != NULL; pp++)
printf("alias: %s\n", *pp);
for (pp = hostp->h_addr_list; *pp != NULL; pp++)
{
addr.s_addr = *((unsigned int *)*pp);
printf("address: %s\n", inet_ntoa(addr));
}
exit(0);
}
**********************************************
>gcc -o hostinfo hostinfo.c
**********************************************
>./hostinfo 127.0.0.1
official hostname: localhost.localdomain
alias: localhost
address: 127.0.0.1
**********************************************
but
>./hostinfo 192.168.1.1
segmentation fault
**********************************************
why? who could tell me?? thank you very much!!!
|
|
0
|
|
|
|
Reply
|
Final
|
12/14/2005 8:55:53 AM |
|
Final wrote:
> hi all~
> **********************************************
>> cat hostinfo.c
>
> #include <netdb.h>
> #include <netinet/in.h>
> #include <arpa/inet.h>
> #include <stdio.h>
> #include <unistd.h>
>
> int main(int argc, char **argv)
> {
> char **pp;
> struct in_addr addr;
> struct hostent *hostp;
>
> if (argc != 2)
> {
> fprintf(stderr, "usage: %s <domain name or
> dotted-decimal>\n", argv[0]);
> exit(0);
> }
>
> if (inet_aton(argv[1], &addr) != 0)
> hostp = gethostbyaddr((const char *)&addr, sizeof(addr),
> AF_INET);
> else
> hostp = gethostbyname(argv[1]);
>
> printf("official hostname: %s\n", hostp->h_name);
What if gethostbyname cannot translate the address, and returns
NULL ?
(See h_errno and the hstrerror function for help in debugging.)
|
|
0
|
|
|
|
Reply
|
ISO
|
12/14/2005 9:24:08 AM
|
|
Final wrote:
> hi all~
> **********************************************
>
>>cat hostinfo.c
>
>
> #include <netdb.h>
> #include <netinet/in.h>
> #include <arpa/inet.h>
> #include <stdio.h>
> #include <unistd.h>
>
> int main(int argc, char **argv)
> {
> char **pp;
> struct in_addr addr;
> struct hostent *hostp;
>
> if (argc != 2)
> {
> fprintf(stderr, "usage: %s <domain name or
> dotted-decimal>\n", argv[0]);
> exit(0);
> }
>
> if (inet_aton(argv[1], &addr) != 0)
> hostp = gethostbyaddr((const char *)&addr, sizeof(addr),
> AF_INET);
> else
> hostp = gethostbyname(argv[1]);
>
> printf("official hostname: %s\n", hostp->h_name);
>
> for (pp = hostp->h_aliases; *pp != NULL; pp++)
> printf("alias: %s\n", *pp);
>
> for (pp = hostp->h_addr_list; *pp != NULL; pp++)
> {
> addr.s_addr = *((unsigned int *)*pp);
> printf("address: %s\n", inet_ntoa(addr));
> }
>
> exit(0);
> }
> **********************************************
>
>>gcc -o hostinfo hostinfo.c
>
> **********************************************
>
>>./hostinfo 127.0.0.1
>
>
> official hostname: localhost.localdomain
> alias: localhost
> address: 127.0.0.1
> **********************************************
> but
>
>>./hostinfo 192.168.1.1
>
>
> segmentation fault
> **********************************************
> why? who could tell me?? thank you very much!!!
>
In the statement "for (pp = hostp->h_aliases; *pp != NULL; pp++)",
you're supposed to look for pp != NUll, not *pp != NULL. As it
stands if pp == NULL, you will try to dereference a NULL pointer,
leading to a SIGSEGV.
--
Fletcher Glenn
|
|
0
|
|
|
|
Reply
|
Fletcher
|
12/14/2005 4:08:04 PM
|
|
|
2 Replies
156 Views
(page loaded in 0.125 seconds)
Similiar Articles: Segmentation fault (core dumped) - comp.unix.solarisHi there, I tried to test my OPEN MP compiler under SUN. I compiled the scource code with the following command: omcc -o openmp_test openmp_test.c -... segmentation fault (SIGSEGV) - comp.lang.fortranAfter compiling the whole codes without no errors, I'm getting into the trouble of "segmentation fault" when I try to run the executable file. I... Segmentation fault exiting isql from unixODBC - comp.databases ...Hi, I have been using the Pervasive ODBC drivers (odbcci.so) for a long time to connect a Linux RedHat 7.2 box to a Pervasive SQL Server 2000i under ... Segmentation fault on trying to deliver to bighotpay.com - comp ...I get a Segmentation fault when trying to send auto-replies back to a couple of domains (such as bigboobfund.com, bighotpay.com, fundtreesite.com).... May be slightly OT: Apache on Solaris Segmentation Fault - comp ...Hi, I have Apache 2.0.52, which I compiled using the gcc from (I think) the Companion CD for Solaris 9) awhile ago, running on a Solaris 9 SPARC sy... segmentation fault with shared memory - comp.unix.programmer ...hi everybody i ve a pb with this code #include #include #include #include #include #include #define SHM_SIZE 4... Segmentation Fault error in MPICH - comp.parallel.mpisegmentation fault (SIGSEGV) - comp.lang.fortran After compiling the whole codes without no errors, I'm getting into the trouble of "segmentation fault" when I try to ... segmentation fault when shared object using STL is statically ...Hi I am sure there are some people who have gone through this problem, but could not find specific solution to this. I am using GCC 3.3.2, on Solaris... debugging segfault in ALSA.so - comp.music.midi> But whenever I send the process an INT signal, either from ^C at > the keyboard or from a different xterm, I get Segmentation fault > and my Perl signal handler doesn't ... ssh dumps core on s10_72 x86 - comp.unix.solarissegmentation fault (SIGSEGV) - comp.lang.fortran ssh dumps core on s10_72 x86 - comp.unix.solaris segmentation fault (SIGSEGV) - comp.lang.fortran ssh dumps core on s10_72 ... Segmentation fault - Wikipedia, the free encyclopediaA segmentation fault (often shortened to segfault), bus error or access violation is generally an attempt to access memory that the CPU cannot physically address. Why Does The Segmentation Fault Occur on Linux / UNIX Systems?According to wikipedia: A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a ... 7/17/2012 10:24:04 AM
|