summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..18e33f5
--- /dev/null
+++ b/main.c
@@ -0,0 +1,68 @@
1#include <netinet/in.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <sys/socket.h>
6#include <unistd.h>
7
8#include <err.h>
9#include <errno.h>
10
11#include <pthread.h>
12
13void * next_customer(int new_socket)
14{
15 char *hello = "Hello from server";
16 char buffer[1024] = {0};
17 int valread = read(new_socket, buffer, 1024);
18 printf("%s %d\n", buffer, valread);
19
20 send(new_socket, hello, strlen(hello), 0);
21
22 printf("Hello message sent\n");
23 return NULL;
24}
25
26void serve(int server_fd)
27{
28 struct sockaddr_in address;
29 int addrlen = sizeof(address);
30 warn("waiting for connections on %d", server_fd);
31
32 for(int new_socket = 0
33 ; 1 ; new_socket=accept(server_fd, (struct sockaddr*) &address, (socklen_t*) &addrlen))
34 {
35 warn("next: %d", new_socket);
36 // next_customer(new_socket);
37 pthread_t thread_id;
38 pthread_create(&thread_id, NULL, next_customer, new_socket);
39 pthread_join(thread_id, NULL);
40 }
41}
42
43#define PORT 8080
44int main(int argc, char const *argv[])
45{
46 int server_fd;
47 int opt = 1;
48
49 struct sockaddr_in address;
50 address.sin_family = AF_INET;
51 address.sin_addr.s_addr = INADDR_ANY;
52 address.sin_port = htons(PORT);
53
54 0 == (server_fd = socket(AF_INET, SOCK_STREAM, 0))
55 ? err(EXIT_FAILURE, NULL)
56 : setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))
57 ? err(EXIT_FAILURE, "setsockopt failed on socket with fileno %d", server_fd)
58 : bind(server_fd, (struct sockaddr*) &address, sizeof(address))
59 ? err(EXIT_FAILURE, NULL)
60 : listen(server_fd, 3)
61 ? err(EXIT_FAILURE, NULL)
62 : serve(server_fd);
63
64
65 return 0;
66}
67
68// vim: shiftwidth=2 tabstop=2 number
..