-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
86 lines (75 loc) · 2.68 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
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
// Insprations:
// https://www.educative.io/edpresso/how-to-implement-udp-sockets-in-c
// https://www.geeksforgeeks.org/udp-server-client-implementation-c/
// http://beej.us/guide/bgnet/pdf/bgnet_usl_c_1.pdf Chapter 6
// https://coursys.sfu.ca/2021su-cmpt-300-d1/pages/tut_sockets/view
#include "server.h"
#include <arpa/inet.h> //inet_ntoa
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h> //pthread
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //memset
#include <sys/socket.h> //socket
#include <unistd.h> //close
#include "args.h"
#include "list.h"
static const unsigned int MAX_INCOMING_MESSAGE_LEN = 4096;
void *init_server(void *_args) {
char *message_slot = NULL;
struct args_s *args = (struct args_s *)_args;
unsigned int port = args->port;
List *list = args->list;
pthread_cond_t *cond = args->cond;
pthread_mutex_t *lock = args->lock;
struct sockaddr_in client_addr, server_addr;
int client_addr_len = sizeof client_addr;
// create socket
int server_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (server_socket < 0) {
printf("[SERVER] Could not create the socket\n");
return NULL;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = INADDR_ANY;
int res = bind(server_socket, (struct sockaddr *)&server_addr,
sizeof server_addr);
if (res < 0) {
printf("[SERVER] Port could not be binded\n");
exit(1);
}
while (1) {
pthread_mutex_lock(lock);
while (List_count(list)) {
pthread_cond_wait(cond, lock);
}
message_slot = calloc(MAX_INCOMING_MESSAGE_LEN, sizeof *message_slot);
res = recvfrom(server_socket, message_slot, MAX_INCOMING_MESSAGE_LEN, 0,
(struct sockaddr *)&client_addr, &client_addr_len);
if (res < 0) {
printf("[SERVER] Could not recieve incoming messages\n");
exit(1);
}
// const char *client_ipv4 = inet_ntoa(client_addr.sin_addr);
// const uint16_t client_port = ntohs(client_addr.sin_port);
if (message_slot && strlen(message_slot)) {
if (List_add(list, (void *)message_slot) == -1) {
printf(
"\nWARNING: Server could not add \"%s\" to the messages to "
"be "
"printed list\n",
message_slot);
free(message_slot);
}
} else {
free(message_slot);
}
message_slot = NULL;
pthread_cond_signal(cond);
pthread_mutex_unlock(lock);
}
close(server_socket);
return NULL;
}