-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinet_numserv.c
More file actions
71 lines (67 loc) · 1.86 KB
/
inet_numserv.c
File metadata and controls
71 lines (67 loc) · 1.86 KB
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
/*
* Number server using internet stream sockets
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include "readlinebuf.c"
static void exit_err(char *reason) {
perror(reason);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv) {
int fd, sock, number, req, i;
struct addrinfo *nfo, *nfo_res, hints;
char hostnam[100], servnam[100], buf[100], *ep;
ssize_t nread;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
exit_err("socket");
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
if (getaddrinfo(NULL, argv[1], &hints, &nfo_res) == -1)
exit_err("getaddrinfo");
for(nfo = nfo_res; nfo != NULL; nfo = nfo->ai_next) {
if (bind(sock, nfo->ai_addr, nfo->ai_addrlen) == 0) {
puts("Bind success");
break;
}
}
if (nfo == NULL)
exit_err("Couldn't find an address to bind to");
if (getnameinfo(nfo->ai_addr, nfo->ai_addrlen, hostnam, 100, servnam, 100, NI_NUMERICHOST | NI_NUMERICSERV) == -1)
exit_err("getnameinfo");
if (listen(sock, 512) == -1)
exit_err("listen");
printf("Now listening on %s:%s\n", hostnam, servnam);
freeaddrinfo(nfo_res);
number = 0;
while ((fd = accept(sock, NULL, NULL)) != -1) {
puts("Accepted connection");
if ((nread = read(fd, buf, 100)) == -1)
exit_err("rec");
req = (int)strtol(buf, &ep, 10);
if (ep == buf) {
puts("Client request error. Closing");
close(fd);
continue;
}
for (i = 0; i < req; i++) {
// Will never reach 99 digits from formatting an int
// so don't worry about truncation...
if ((nread = snprintf(buf, 100, "%d\n", number++)) > 0)
write(fd, buf, nread + 1);
}
close(fd);
}
exit_err("read");
}