2024-02-27 09:02:38 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2024-02-28 20:26:03 +00:00
|
|
|
#define PORT 8888
|
2024-02-27 21:29:34 +00:00
|
|
|
#define BUFF_SIZE 8192
|
|
|
|
#define MAX_CLIENTS 125
|
2024-02-27 09:02:38 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int fd;
|
|
|
|
struct sockaddr_in clients[MAX_CLIENTS + 1];
|
2024-02-28 20:26:03 +00:00
|
|
|
int client_fds[MAX_CLIENTS + 1];
|
2024-02-27 09:02:38 +00:00
|
|
|
} DATA;
|
|
|
|
|
|
|
|
void *process_clients(void *p) {
|
|
|
|
DATA *data = (DATA *)p;
|
2024-02-27 21:29:34 +00:00
|
|
|
char buf[BUFF_SIZE];
|
2024-02-27 09:02:38 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
off_t ret = read(STDIN_FILENO, buf, sizeof(buf));
|
|
|
|
if (ret <= 0)
|
|
|
|
continue;
|
|
|
|
|
2024-02-27 16:16:46 +00:00
|
|
|
socklen_t len = sizeof(struct sockaddr_in);
|
2024-02-27 09:02:38 +00:00
|
|
|
for (size_t i = 0; i < MAX_CLIENTS; i++)
|
2024-02-28 20:26:03 +00:00
|
|
|
if (data->client_fds[i] != -1)
|
|
|
|
if (sendto(data->client_fds[i], buf, ret, 0, (struct sockaddr *)&data->clients[i], len) != ret)
|
|
|
|
data->client_fds[i] = -1;
|
2024-02-27 09:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
2024-02-28 20:26:03 +00:00
|
|
|
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
2024-02-27 09:02:38 +00:00
|
|
|
if (fd < 0)
|
|
|
|
goto ERROR_WITHOUTCLOSE;
|
|
|
|
|
2024-02-28 20:26:03 +00:00
|
|
|
struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
|
2024-02-27 09:02:38 +00:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
|
|
|
|
goto ERROR;
|
|
|
|
|
|
|
|
int reuse = 1;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(int)) < 0)
|
|
|
|
goto ERROR;
|
|
|
|
|
|
|
|
static struct sockaddr_in addr;
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(PORT);
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
|
|
|
|
goto ERROR;
|
|
|
|
|
2024-02-28 20:26:03 +00:00
|
|
|
if (listen(fd, -1) < 0)
|
|
|
|
goto ERROR;
|
|
|
|
|
|
|
|
/* Thread of common buffer */
|
2024-02-27 09:02:38 +00:00
|
|
|
static DATA data;
|
2024-02-28 20:26:03 +00:00
|
|
|
memset(data.client_fds, -1, sizeof(data.client_fds));
|
2024-02-27 09:02:38 +00:00
|
|
|
data.fd = fd;
|
|
|
|
|
|
|
|
pthread_t td;
|
|
|
|
pthread_create(&td, NULL, process_clients, (void *)&data);
|
|
|
|
|
2024-02-28 20:26:03 +00:00
|
|
|
/* Get clients */
|
|
|
|
socklen_t len = sizeof(addr);
|
2024-02-27 09:02:38 +00:00
|
|
|
while (1) {
|
2024-02-28 20:26:03 +00:00
|
|
|
int client_fd = accept(fd, (struct sockaddr *)&addr, &len);
|
|
|
|
if (client_fd < 0)
|
|
|
|
continue;
|
2024-02-27 10:59:19 +00:00
|
|
|
|
2024-02-28 20:26:03 +00:00
|
|
|
for (size_t i = 0; i < MAX_CLIENTS; i++) {
|
|
|
|
if (data.client_fds[i] == -1) {
|
|
|
|
data.clients[i] = addr;
|
|
|
|
data.client_fds[i] = client_fd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-02-27 09:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ERROR:
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
ERROR_WITHOUTCLOSE:
|
|
|
|
fprintf(stderr, "streamer: %s\n", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|