Ticket #8320: ghbn.c

File ghbn.c, 1.3 KB (added by dknoto, 12 years ago)

Test code

Line 
1/**
2 * Test gethostbyname.function by DKnoto, 2012/02/04.
3 */
4
5#include <stdio.h>
6#include <errno.h>
7#include <netdb.h>
8#include <sys/socket.h>
9
10extern int h_errno;
11
12int main( int argc, char *argv[] )
13{
14 char *hn = NULL;
15 struct hostent *he = NULL;
16
17 /**
18 * Analiza ilości argumentów.
19 */
20 switch( argc )
21 {
22 case 2:
23 {
24 hn = argv[1];
25 break;
26 }
27 case 1:
28 {
29 hn = "localhost";
30 break;
31 }
32 default:
33 {
34 printf( "Erro: bad arguments number\n" );
35 return 1;
36 break;
37 }
38 }
39
40 /**
41 *
42 */
43 printf( "Host in \"%s\"\n", hn );
44 he = gethostbyname( hn );
45 if( he )
46 {
47 int i = 0;
48 printf( " h_name == \"%s\"\n", he->h_name );
49 while( he->h_aliases[i] )
50 {
51 printf( " h_aliases[%d] == \"%s\"\n", i, he->h_aliases[i] );
52 i++;
53 }
54 printf( " h_addrtype == %d\n", he->h_addrtype );
55 printf( " h_length == %d\n", he->h_length );
56
57 i = 0;
58 while( he->h_addr_list[i] )
59 {
60 int j = 0;
61 printf( " h_addr_list[%d] == %d", i,
62 (unsigned char)(he->h_addr_list[i][j]) );
63 for( j++; j < he->h_length; j++ )
64 {
65 printf( ".%d", (unsigned char)(he->h_addr_list[i][j]) );
66 }
67 printf( "\n" );
68 i++;
69 }
70 }
71 else
72 {
73 printf( " gethostbyname( \"%s\" ): Error, errno == %d, h_errno == %d\n",
74 hn, errno, h_errno );
75 }
76
77 return 0;
78}
79
80/**
81 * EOF
82 */