-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.c
56 lines (51 loc) · 1.1 KB
/
hello.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
int main()
{
// create sockte test
int sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(-1 != sd)
{
close(sd);
}
else
{
perror("socket");
return -1;
}
printf("---------------create socket test end\n");
// gethostent test
struct hostent * phe;
phe = gethostent();
if(NULL != phe)
{
printf("name: %s,\n addrtype: %d,\n length: %d,\n IP Address: %s, %x\n", phe->h_name, phe->h_addrtype, phe->h_length, inet_ntoa(*((struct in_addr*)phe->h_addr)), *(int*)(phe->h_addr));
}
char **pptr;
for(pptr = phe->h_aliases; *pptr != NULL; pptr++)
{
printf("alias: %s\n", *pptr);
}
switch(phe->h_addrtype)
{
case AF_INET:
case AF_INET6:
pptr = phe->h_addr_list;
char str[32];
for(; *pptr!=NULL;pptr++)
{
printf("address, %d, %d: %s\n", (*pptr)[0], (*pptr)[1], inet_ntop(phe->h_addrtype, *pptr, str, sizeof(str)));
}
break;
default:
printf("error\n");
break;
}
printf("---------------gethostent test end\n");
// getaddrinfo test
// other test below
return 0;
}