-
Notifications
You must be signed in to change notification settings - Fork 0
/
server2.c
84 lines (79 loc) · 2.27 KB
/
server2.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
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#define EXPECTED_ARGS 1
#define QUEUE_SIZE 5
#define PORT 30003
#define SIZE 1000
int main(int argc, char* argv[]) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0) {
perror("socket");
return 1;
}
if(argc != EXPECTED_ARGS) {
printf("Usage: %s", argv[0]);
return 1;
}
char hostName[100];
gethostname(hostName, 100);
printf("Host name: %s\n", hostName);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = INADDR_ANY;
if(bind(sock, (const struct sockaddr *)&addr, sizeof(addr))) {
perror("bind");
return 1;
}
if(listen(sock, QUEUE_SIZE)) {
perror("listen");
return 1;
}
while(1) {
int clientFd = accept(sock, NULL, NULL);
if(clientFd < 0) {
perror("accept");
return 1;
}
printf("Client connected\n");
unsigned* buff = malloc(SIZE * sizeof(unsigned));
int startingPoint = 0;
while(startingPoint < SIZE) {
ssize_t readBytes = read(clientFd, buff, SIZE * sizeof(unsigned));
if(!readBytes) {
break; // EOF
}
int endPoint = startingPoint + readBytes / sizeof(unsigned);
if(endPoint > SIZE) {
endPoint = SIZE;
}
int i;
for(i = startingPoint; i < endPoint; i++) {
buff[i] = ntohl(buff[i]); // Convert from network byte order
}
if(buff[0] == 418) {
printf("I'm a teapot\n");
printf("Code recieved, terminating\n");
goto end; // I know, I know, goto is bad, but it's the only way to break out of two loops
}
for(i = startingPoint; i < endPoint; i++) {
printf("%u ", buff[i]);
}
startingPoint = endPoint;
}
printf("\nClient disconnected\n");
free(buff);
}
end:
printf("Server terminated\n");
close(sock);
return 0;
}