forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest04.c
64 lines (54 loc) · 1.38 KB
/
test04.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
57
58
59
60
61
62
63
64
#include "unp.h"
void
sig_alrm(int signo)
{
}
int
main(int argc, char **argv)
{
int sockfd, n;
struct sockaddr_in servaddr;
struct itimerval val;
fd_set rset, wset;
if (argc != 2)
err_quit("usage: a.out <IPaddress>");
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13); /* echo server */
Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
/* Set interval timer to go off before 3WHS completes */
Signal(SIGALRM, sig_alrm);
val.it_interval.tv_sec = 0;
val.it_interval.tv_usec = 0;
val.it_value.tv_sec = 0;
val.it_value.tv_usec = 50000; /* 50 ms */
if (setitimer(ITIMER_REAL, &val, NULL) == -1)
err_sys("setitimer error");
again:
if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) {
if (errno == EINTR) {
#ifdef notdef
goto again; /* second call to connect() -> EADDRINUSE */
#endif
#ifdef notdef
printf("interrupted system call\n");
exit(0);
#endif
} else
err_sys("connect error");
}
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
wset = rset;
sleep(4);
n = Select(sockfd+1, &rset, &wset, NULL, NULL);
printf("select returned %d\n", n);
if (FD_ISSET(sockfd, &rset))
printf("socket is readable\n");
if (FD_ISSET(sockfd, &wset))
printf("socket is writable\n");
str_cli(stdin, sockfd); /* do it all */
exit(0);
}