-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
146 lines (121 loc) · 4.27 KB
/
Copy pathserver.c
File metadata and controls
146 lines (121 loc) · 4.27 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_CLIENTS 10
int createTCPIpv4Socket() {
return socket(AF_INET, SOCK_STREAM, 0);
}
struct sockaddr_in* createIpv4Address(char *ip, int port) {
struct sockaddr_in *address = malloc(sizeof(struct sockaddr_in));
address->sin_family = AF_INET;
address->sin_port = htons(port);
if (strlen(ip) == 0) {
address->sin_addr.s_addr = INADDR_ANY;
} else {
inet_pton(AF_INET, ip, &address->sin_addr.s_addr);
}
return address;
}
struct AcceptedSocket {
int acceptedSocketFD;
struct sockaddr_in address;
int error;
bool acceptedSuccessfully;
};
struct AcceptedSocket* acceptedSockets[MAX_CLIENTS];
int acceptedSocketsCount = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
struct AcceptedSocket* acceptIncomingConnection(int serverSocketFD) {
struct sockaddr_in clientAddress;
socklen_t clientAddressSize = sizeof(struct sockaddr_in);
int clientSocketFD = accept(serverSocketFD, (struct sockaddr*)&clientAddress, &clientAddressSize);
struct AcceptedSocket* acceptedSocket = malloc(sizeof(struct AcceptedSocket));
acceptedSocket->address = clientAddress;
acceptedSocket->acceptedSocketFD = clientSocketFD;
acceptedSocket->acceptedSuccessfully = clientSocketFD >= 0;
if (!acceptedSocket->acceptedSuccessfully) {
acceptedSocket->error = clientSocketFD;
}
return acceptedSocket;
}
void sendReceivedMessageToOtherClients(char* buffer, int senderSocketFD) {
pthread_mutex_lock(&lock);
for (int i = 0; i < acceptedSocketsCount; i++) {
int receiverSocketFD = acceptedSockets[i]->acceptedSocketFD;
if (receiverSocketFD != senderSocketFD) {
send(receiverSocketFD, buffer, strlen(buffer), 0);
}
}
pthread_mutex_unlock(&lock);
}
void* receiveAndPrintIncomingMessages(void* arg) {
int clientSocketFD = *((int*)arg);
free(arg);
char buffer[1024];
while (true) {
ssize_t byteReceived = recv(clientSocketFD, buffer, sizeof(buffer) - 1, 0);
if (byteReceived > 0) {
buffer[byteReceived] = '\0';
printf("Message from %d: %s\n", clientSocketFD, buffer);
sendReceivedMessageToOtherClients(buffer, clientSocketFD);
} else if (byteReceived <= 0) {
break;
}
}
close(clientSocketFD);
pthread_exit(NULL);
}
void receiveAndPrintIncomingMessagesInSeperateThread(struct AcceptedSocket* pSocket) {
pthread_t id;
int* socketCopy = malloc(sizeof(int));
*socketCopy = pSocket->acceptedSocketFD;
pthread_create(&id, NULL, receiveAndPrintIncomingMessages, socketCopy);
pthread_detach(id);
}
void startAcceptingIncomingConnections(int serverSocketFD) {
while (true) {
struct AcceptedSocket* clientSocket = acceptIncomingConnection(serverSocketFD);
if (!clientSocket->acceptedSuccessfully) {
perror("Failed to accept connection");
free(clientSocket);
continue;
}
pthread_mutex_lock(&lock);
if (acceptedSocketsCount < MAX_CLIENTS) {
acceptedSockets[acceptedSocketsCount++] = clientSocket;
receiveAndPrintIncomingMessagesInSeperateThread(clientSocket);
} else {
printf("Max clients reached. Rejecting connection.\n");
close(clientSocket->acceptedSocketFD);
free(clientSocket);
}
pthread_mutex_unlock(&lock);
}
}
int main() {
int serverSocketFD = createTCPIpv4Socket();
struct sockaddr_in* serverAddress = createIpv4Address("", 8000);
int result = bind(serverSocketFD, (struct sockaddr*)serverAddress, sizeof(*serverAddress));
if (result == 0) {
printf("Socket was bound successfully\n");
} else {
perror("Bind failed");
exit(EXIT_FAILURE);
}
int listenResult = listen(serverSocketFD, MAX_CLIENTS);
if (listenResult != 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
printf("Server is listening on port 8000...\n");
startAcceptingIncomingConnections(serverSocketFD);
shutdown(serverSocketFD, SHUT_RDWR);
close(serverSocketFD);
free(serverAddress);
return 0;
}