-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathecho-server.c
200 lines (164 loc) · 5.06 KB
/
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*-
*
* echo-server.c --- Simple echo server for testing purposes.
*
*/
/* TODO: IPv6 support, port to Winsock */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <Winsock2.h>
#include <Ws2tcpip.h>
#else
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#endif
#define DEFAULT_PORT 7
#define BACKLOG 10
#define MAXCONS FD_SETSIZE
static int
sendallto(int s, char *buf, int len, struct sockaddr *to, socklen_t tolen)
{
int sent = 0;
int left = len;
while (sent < len) {
int n = sendto(s, buf+sent, left, 0, to, tolen);
if (n == -1) { return -1; }
sent += n;
left -= n;
}
return 0;
}
static char time_str[9];
static const char* timestamp()
{
time_t now = time(NULL);
struct tm *tms = localtime(&now);
strftime(time_str, (sizeof time_str)-1, "%T", tms);
return time_str;
}
int main(int argc, char *argv[])
{
int fds[MAXCONS];
struct sockaddr_in addrs[MAXCONS];
int conns = 0;
int local_port, listenfd, dgramfd;
struct sockaddr_in local;
socklen_t sin_size = sizeof(struct sockaddr_in);
switch (argc) {
case 1: local_port = DEFAULT_PORT; break;
case 2: local_port = strtol(argv[1], NULL, 10); break;
default:
fprintf(stderr, "Usage: %s [port number]\n", argv[0]);
fprintf(stderr, "The default port is: %d.\n", DEFAULT_PORT);
exit(1);
}
if (local_port <= 0 || local_port > 65535) {
fprintf(stderr, "Error: invalid port number.\n");
exit(1);
}
printf("[%s] listening on port %d\n", timestamp(), local_port);
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
fds[conns++] = listenfd;
if ((dgramfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("dgram socket");
exit(1);
}
fds[conns++] = dgramfd;
local.sin_family = AF_INET;
local.sin_port = htons(local_port);
local.sin_addr.s_addr = INADDR_ANY;
memset(local.sin_zero, 0, sizeof local.sin_zero);
if (bind(listenfd, (struct sockaddr *) &local, sin_size) == -1) {
perror("bind");
exit(1);
}
if (bind(dgramfd, (struct sockaddr *) &local, sin_size) == -1) {
perror("dgram bind");
exit(1);
}
if (listen(listenfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
for (;;) {
int fd, max_fd, i, j, n;
fd_set readfds;
char buf[4096];
FD_ZERO(&readfds);
for (i = 0, max_fd = -1; i < conns; i++) {
if (fds[i] > max_fd)
max_fd = fds[i];
FD_SET(fds[i], &readfds);
}
if (select(max_fd+1, &readfds, NULL, NULL, NULL) == -1) {
perror("select");
exit(1);
}
if (FD_ISSET(listenfd, &readfds)) {
fd = accept(listenfd, (struct sockaddr *) &addrs[conns], &sin_size);
if (fd == -1) {
perror("accept");
continue;
} else {
printf("[%s] accepted connection from %s (fd: %d)\n",
timestamp(), inet_ntoa(addrs[conns].sin_addr), fd);
fds[conns++] = fd;
}
}
for (i = 1; i < conns; i++) {
if (FD_ISSET(fds[i], &readfds)) {
sin_size = sizeof(struct sockaddr_in);
n = recvfrom(fds[i], buf, (sizeof buf)-1, 0,
(struct sockaddr *) &addrs[i], &sin_size);
if (n == -1) {
perror("recvfrom");
goto error;
}
// n == 0? huh hmm..
if (n == 0) goto error;
printf("[%s] got %d bytes from %s (%s) ", timestamp(),
n, inet_ntoa(addrs[i].sin_addr),
i == 1 ? "udp" : "tcp");
for (j = 0; j < n; j++) {
if (j % 75 == 0)
printf("\n ");
if (isprint(buf[j]))
putchar(buf[j]);
else
printf("[0x%x]", buf[j]);
//putchar(isprint(buf[j]) ? buf[j] : '?');
}
if (sendallto(fds[i], buf, n, (struct sockaddr *) &addrs[i],
sizeof(struct sockaddr_in)) == -1) {
perror("\nsendto");
goto error;
}
printf("\n (echoed)\n");
continue;
error:
if (i == 1) continue;
printf("[%s] removing fd %d from set\n", timestamp(), fds[i]);
close(fds[i]);
for (j = i+1; j < conns; j++) {
fds[j-1] = fds[j];
addrs[j-1] = addrs[j];
}
conns--;
}
}
}
return 0;
}