-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.c
50 lines (41 loc) · 1.09 KB
/
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
#include "faketcp.h"
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
void* rdloop(int socket) {
char buffer[16];
buffer[15] = 0;
int ret = 1;
while (ret > 0) {
ret = ftcp_read(socket, buffer, sizeof(buffer) - 1);
if (ret > 0) {
write(1, buffer, ret);
printf("\nwriting to %d\n", socket);
ftcp_write(socket, buffer, ret);
}
}
return NULL;
}
int main(int argc, char** argv) {
short port = atoi(argv[1]);
int socket = ftcp_socket(PF_INET);
sockaddr_in addr, cl_addr;
memset((void*) &addr, 0, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(port);
socklen_t addrlen = sizeof(sockaddr_in);
if (ftcp_bind(socket, (sockaddr*) &addr, addrlen) < 0)
perror("bind");
if (ftcp_listen(socket) < 0)
perror("listen");
int sck;
while ((sck = ftcp_accept(socket, (sockaddr*) &cl_addr, &addrlen)) > 0) {
pthread_t id;
pthread_create(&id, NULL, (void* (*)(void*)) rdloop, (void*) (size_t) sck);
}
if (sck < 0)
perror("accept");
return 0;
}