-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtcp-non-blocking-select-echo-server.c
162 lines (118 loc) · 3.61 KB
/
tcp-non-blocking-select-echo-server.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/select.h>
#include <unistd.h>
#define PORT "9421"
/* function prototypes */
void die(const char *);
int main(int argc, char **argv)
{
int sockfd, new, maxfd, on = 1, nready, i;
struct addrinfo *res0, *res, hints;
char buffer[BUFSIZ];
fd_set master, readfds;
int error;
ssize_t nbytes;
(void)memset(&hints, '\0', sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
if (0 != (error = getaddrinfo(NULL, PORT, &hints, &res0)))
errx(EXIT_FAILURE, "%s", gai_strerror(error));
for (res = res0; res; res = res->ai_next)
{
if (-1 == (sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)))
{
perror("socket()");
continue;
}
if (-1 == (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int))))
{
perror("setsockopt()");
continue;
}
if (-1 == (bind(sockfd, res->ai_addr, res->ai_addrlen)))
{
perror("bind()");
continue;
}
break;
}
if (-1 == sockfd)
exit(EXIT_FAILURE);
freeaddrinfo(res0);
if (-1 == (listen(sockfd, 32)))
die("listen()");
if (-1 == (fcntl(sockfd, F_SETFD, O_NONBLOCK)))
die("fcntl()");
FD_ZERO(&master);
FD_ZERO(&readfds);
FD_SET(sockfd, &master);
maxfd = sockfd;
while (1)
{
memcpy(&readfds, &master, sizeof(master));
(void)printf("running select()\n");
if (-1 == (nready = select(maxfd + 1, &readfds, NULL, NULL, NULL)))
die("select()");
(void)printf("Number of ready descriptor: %d\n", nready);
for (i = 0; i <= maxfd && nready > 0; i++)
{
if (FD_ISSET(i, &readfds))
{
nready--;
if (i == sockfd)
{
(void)printf("Trying to accept() new connection(s)\n");
if (-1 == (new = accept(sockfd, NULL, NULL)))
{
if (EWOULDBLOCK != errno)
die("accept()");
break;
}
else
{
if (-1 == (fcntl(new, F_SETFD, O_NONBLOCK)))
die("fcntl()");
FD_SET(new, &master);
printf("accept new client fd:%d\n", new);
if (maxfd < new)
maxfd = new;
}
}
else
{
(void)printf("recv() data from fd:%d\n", i);
nbytes = recv(i, buffer, sizeof(buffer), 0);
if (nbytes <= 0)
{
if (EWOULDBLOCK != errno)
die("recv()");
break;
}
buffer[nbytes] = '\0';
printf("%s", buffer);
(void)printf("%zi bytes received.\n", nbytes);
send(i, buffer, nbytes, 0);
// close(i);
// FD_CLR(i, &master);
}
}
}
}
return 0;
}
void die(const char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}