-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmini_serv.c
120 lines (111 loc) · 4.3 KB
/
mini_serv.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
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
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
int maxSocket = 0, serverSocket = -1, next_id = 0;
int clients[65536], currmsg[65536];
char bufferRead[4096 * 42], bufferWrite[4096 * 42 + 42], bufferWriteMessage[4096 * 42];
fd_set readSockets, writeSockets, activeSockets;
void sendAll(int sender)
{
for (int i = 0; i <= maxSocket; i++)
if (FD_ISSET(i, &writeSockets) && i != sender)
send(i, bufferWrite, strlen(bufferWrite), 0);
}
void fatal()
{
write(2, "Fatal error\n", strlen("Fatal error\n"));
close(serverSocket);
exit(1);
}
int main(int argc, char **argv)
{
if (argc != 2)
{
write(2, "Wrong number of arguments\n", strlen("Wrong number of arguments\n"));
exit(1);
}
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverAddr;
socklen_t len;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(2130706433);
serverAddr.sin_port = htons(atoi(argv[1]));
if (serverSocket < 0)
fatal();
if ((bind(serverSocket, (const struct sockaddr *) &serverAddr, sizeof(serverAddr))) < 0)
fatal();
if (listen(serverSocket, 128) < 0)
fatal();
FD_ZERO(&activeSockets);
FD_SET(serverSocket, &activeSockets);
maxSocket = serverSocket;
bzero(clients, sizeof(clients));
while (1)
{
readSockets = writeSockets = activeSockets;
if (select(maxSocket + 1, &readSockets, &writeSockets, NULL, NULL) <= 0)
continue;
for (int socketId = 0; socketId <= maxSocket; socketId++)
{
if (FD_ISSET(socketId, &readSockets))
{
if (serverSocket == socketId)
{
int clientSocket = accept(serverSocket, (struct sockaddr *) &serverAddr, &len);
if (clientSocket < 0)
continue;
FD_SET(clientSocket, &activeSockets);
clients[clientSocket] = next_id++;
currmsg[clientSocket] = 0;
maxSocket = clientSocket > maxSocket ? clientSocket : maxSocket;
sprintf(bufferWrite, "server: client %d just arrived\n", clients[clientSocket]);
sendAll(clientSocket);
break;
}
else
{
int bytesRead = recv(socketId, bufferRead, 4096 * 42, 0);
if (bytesRead <= 0)
{
sprintf(bufferWrite, "server: client %d just left\n", clients[socketId]);
sendAll(socketId);
close(socketId);
FD_CLR(socketId, &activeSockets);
break;
}
else
{
for (int i = 0, j = 0; i < bytesRead; i++, j++)
{
bufferWriteMessage[j] = bufferRead[i];
if (bufferWriteMessage[j] == '\n')
{
bufferWriteMessage[j + 1] = '\0';
if (currmsg[socketId])
sprintf(bufferWrite, "%s", bufferWriteMessage);
else
sprintf(bufferWrite, "client %d: %s", clients[socketId], bufferWriteMessage);
currmsg[socketId] = 0;
j = -1;
sendAll(socketId);
}
else if (i == (bytesRead - 1))
{
bufferWriteMessage[j + 1] = '\0';
if (currmsg[socketId])
sprintf(bufferWrite, "%s", bufferWriteMessage);
else
sprintf(bufferWrite, "client %d: %s", clients[socketId], bufferWriteMessage);
currmsg[socketId] = 1;
sendAll(socketId);
break;
}
}
}
}
}
}
}
}