-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistant_server.c
43 lines (35 loc) · 1.25 KB
/
persistant_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
#include "pipe_networking.h"
#include <signal.h>
int from_client;
int to_client;
// Function to handle SIGINT (e.g., Ctrl+C on the server)
void handle_sigint(int signo) {
printf("\nServer: Caught SIGINT, cleaning up and exiting\n");
remove("/tmp/mario"); // Remove WKP before exiting
if (from_client) close(from_client);
if (to_client) close(to_client);
// exit(0);
}
int main() {
signal(SIGINT, handle_sigint); // Handle Ctrl+C
signal(SIGPIPE, SIG_IGN); // Ignore SIGPIPE to prevent broken pipe nonsense
while (1) {
printf("Server: Waiting for a client\n");
from_client = server_handshake(&to_client);
printf("Server: Handshake completed with a client!\n");
while (1) {
int random_number = rand() % 101;
printf("Server: Sending random number %d to client\n", random_number);
if (write(to_client, &random_number, sizeof(random_number)) == -1) {
perror("Server: Client disconnected or error writing.");
break;
}
sleep(1);
}
// Proper cleanup
close(from_client);
close(to_client);
printf("Server: Client disconnected. Ready to accept a new client.\n");
}
return 0;
}