This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserveur.c
142 lines (128 loc) · 3.53 KB
/
serveur.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
return 1;
}
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
int port = atoi(argv[1]);
if (sock_fd < 0)
{
perror("Socket connection failed");
return 1;
}
// bind socket to address
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
inet_aton("192.168.60.174", &server_addr.sin_addr);
int ret = bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (ret < 0)
{
perror("Bind failed");
return 1;
}
// Listen for incoming connections
ret = listen(sock_fd, 5);
if (ret < 0)
{
perror("Listen failed");
return 1;
}
// Create struct pollfd array
int max_cons = 50;
struct pollfd all_sock[max_cons];
// Initialize struct pollfd array
all_sock[0].fd = sock_fd;
all_sock[0].events = POLLIN;
all_sock[0].revents = 0;
for (int i = 1; i < max_cons; i++)
{
all_sock[i].fd = -1;
all_sock[i].events = 0;
all_sock[i].revents = 0;
}
// Call poll function
while (1)
{
poll(all_sock, max_cons, -1);
// Check on listen socket and accept
if (all_sock[0].revents & POLLIN)
{
struct sockaddr_in client_addr;
socklen_t size_addr = sizeof(client_addr);
int new_sockfd = accept(sock_fd, (struct sockaddr *)&client_addr, &size_addr);
if (new_sockfd < 0)
{
perror("Accept failed");
return 1;
}
printf("New client (%s:%hu)\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Find an empty slot in all_sock and add the new socket
for (int i = 1; i < max_cons; i++)
{
if (all_sock[i].fd == -1)
{
all_sock[i].fd = new_sockfd;
all_sock[i].events = POLLIN;
all_sock[i].revents = 0;
break;
}
}
}
else
{
for (int i = 1; i < max_cons; i++)
{
// Check if there's an event on the current client socket
if (all_sock[i].fd != -1 && all_sock[i].revents & POLLIN)
{
char buffer[1024];
int nb_recu = recv(all_sock[i].fd, buffer, sizeof(buffer), 0);
if (nb_recu == -1)
{
perror("Failed to retrieve messages");
return 1;
}
if (nb_recu == 0)
{
// The client disconnected
fprintf(stderr, "Connection terminated for client %d\n", i);
close(all_sock[i].fd);
all_sock[i].fd = -1; // Set the fd field to -1 to remove the client socket from the pollfd array
all_sock[i].revents = 0;
}
else
{
buffer[nb_recu] = '\0';
char message[1024];
snprintf(message, sizeof(message), "[Client %d]: %s", i, buffer);
fprintf(stdout, "%s", message);
// Transfer message to all the others
for (int j = 1; j < max_cons; j++)
{
if (all_sock[j].fd != -1 && i != j)
{
int n = write(all_sock[j].fd, message, strlen(message));
if (n < 0)
{
fprintf(stdout, "Error when sending the message to the client N°%d\n", j);
return 1;
}
}
}
}
}
}
}
}
return 0;
}