Little fixes

This commit is contained in:
Your Name 2024-02-28 23:42:58 +03:00
parent 32d06992f7
commit a0de029a7f

View File

@ -14,7 +14,6 @@
#define MAX_CLIENTS 125 #define MAX_CLIENTS 125
typedef struct { typedef struct {
int fd;
struct sockaddr_in clients[MAX_CLIENTS + 1]; struct sockaddr_in clients[MAX_CLIENTS + 1];
int client_fds[MAX_CLIENTS + 1]; int client_fds[MAX_CLIENTS + 1];
} DATA; } DATA;
@ -31,7 +30,7 @@ void *process_clients(void *p) {
socklen_t len = sizeof(struct sockaddr_in); socklen_t len = sizeof(struct sockaddr_in);
for (size_t i = 0; i < MAX_CLIENTS; i++) for (size_t i = 0; i < MAX_CLIENTS; i++)
if (data->client_fds[i] != -1) if (data->client_fds[i] != -1)
if (sendto(data->client_fds[i], buf, ret, 0, (struct sockaddr *)&data->clients[i], len) != ret) if (sendto(data->client_fds[i], buf, ret, 0, (struct sockaddr *)&data->clients[i], len) <= 0)
data->client_fds[i] = -1; data->client_fds[i] = -1;
} }
} }
@ -58,18 +57,17 @@ int main(void) {
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
goto ERROR; goto ERROR;
if (listen(fd, -1) < 0) if (listen(fd, 0) < 0)
goto ERROR; goto ERROR;
/* Thread of common buffer */ /* Thread of common buffer */
static DATA data; static DATA data;
memset(data.client_fds, -1, sizeof(data.client_fds)); memset(data.client_fds, -1, sizeof(data.client_fds));
data.fd = fd;
pthread_t td; pthread_t td;
pthread_create(&td, NULL, process_clients, (void *)&data); pthread_create(&td, NULL, process_clients, (void *)&data);
/* Get clients */ /* Accept clients and update the fds buffer */
socklen_t len = sizeof(addr); socklen_t len = sizeof(addr);
while (1) { while (1) {
int client_fd = accept(fd, (struct sockaddr *)&addr, &len); int client_fd = accept(fd, (struct sockaddr *)&addr, &len);