forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daytimetcpcli1.lc
56 lines (48 loc) · 3.29 KB
/
daytimetcpcli1.lc
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 "unp.h"## 1 ##src/names/daytimetcpcli1.c##
int## 2 ##src/names/daytimetcpcli1.c##
main(int argc, char **argv)## 3 ##src/names/daytimetcpcli1.c##
{## 4 ##src/names/daytimetcpcli1.c##
int sockfd, n;## 5 ##src/names/daytimetcpcli1.c##
char recvline[MAXLINE + 1];## 6 ##src/names/daytimetcpcli1.c##
struct sockaddr_in servaddr;## 7 ##src/names/daytimetcpcli1.c##
struct in_addr **pptr;## 8 ##src/names/daytimetcpcli1.c##
struct in_addr *inetaddrp[2];## 9 ##src/names/daytimetcpcli1.c##
struct in_addr inetaddr;## 10 ##src/names/daytimetcpcli1.c##
struct hostent *hp;## 11 ##src/names/daytimetcpcli1.c##
struct servent *sp;## 12 ##src/names/daytimetcpcli1.c##
if (argc != 3)## 13 ##src/names/daytimetcpcli1.c##
err_quit("usage: daytimetcpcli1 <hostname> <service>");## 14 ##src/names/daytimetcpcli1.c##
if ((hp = gethostbyname(argv[1])) == NULL) {## 15 ##src/names/daytimetcpcli1.c##
if (inet_aton(argv[1], &inetaddr) == 0) {## 16 ##src/names/daytimetcpcli1.c##
err_quit("hostname error for %s: %s", argv[1],## 17 ##src/names/daytimetcpcli1.c##
hstrerror(h_errno));## 18 ##src/names/daytimetcpcli1.c##
} else {## 19 ##src/names/daytimetcpcli1.c##
inetaddrp[0] = &inetaddr;## 20 ##src/names/daytimetcpcli1.c##
inetaddrp[1] = NULL;## 21 ##src/names/daytimetcpcli1.c##
pptr = inetaddrp;## 22 ##src/names/daytimetcpcli1.c##
}## 23 ##src/names/daytimetcpcli1.c##
} else {## 24 ##src/names/daytimetcpcli1.c##
pptr = (struct in_addr **) hp->h_addr_list;## 25 ##src/names/daytimetcpcli1.c##
}## 26 ##src/names/daytimetcpcli1.c##
if ((sp = getservbyname(argv[2], "tcp")) == NULL)## 27 ##src/names/daytimetcpcli1.c##
err_quit("getservbyname error for %s", argv[2]);## 28 ##src/names/daytimetcpcli1.c##
for (; *pptr != NULL; pptr++) {## 29 ##src/names/daytimetcpcli1.c##
sockfd = Socket(AF_INET, SOCK_STREAM, 0);## 30 ##src/names/daytimetcpcli1.c##
bzero(&servaddr, sizeof(servaddr));## 31 ##src/names/daytimetcpcli1.c##
servaddr.sin_family = AF_INET;## 32 ##src/names/daytimetcpcli1.c##
servaddr.sin_port = sp->s_port;## 33 ##src/names/daytimetcpcli1.c##
memcpy(&servaddr.sin_addr, *pptr, sizeof(struct in_addr));## 34 ##src/names/daytimetcpcli1.c##
printf("trying %s\n", Sock_ntop((SA *) &servaddr, sizeof(servaddr)));## 35 ##src/names/daytimetcpcli1.c##
if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) == 0)## 36 ##src/names/daytimetcpcli1.c##
break; /* success */## 37 ##src/names/daytimetcpcli1.c##
err_ret("connect error");## 38 ##src/names/daytimetcpcli1.c##
close(sockfd);## 39 ##src/names/daytimetcpcli1.c##
}## 40 ##src/names/daytimetcpcli1.c##
if (*pptr == NULL)## 41 ##src/names/daytimetcpcli1.c##
err_quit("unable to connect");## 42 ##src/names/daytimetcpcli1.c##
while ((n = Read(sockfd, recvline, MAXLINE)) > 0) {## 43 ##src/names/daytimetcpcli1.c##
recvline[n] = 0; /* null terminate */## 44 ##src/names/daytimetcpcli1.c##
Fputs(recvline, stdout);## 45 ##src/names/daytimetcpcli1.c##
}## 46 ##src/names/daytimetcpcli1.c##
exit(0);## 47 ##src/names/daytimetcpcli1.c##
}## 48 ##src/names/daytimetcpcli1.c##