Upload files to "/"

This commit is contained in:
8nl 2024-02-27 09:02:38 +00:00
parent 86bc116842
commit 698e5568ae

80
server.c Normal file
View File

@ -0,0 +1,80 @@
#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>
#define PORT 8888
#define BUFF_SIZE 2048
#define MAX_CLIENTS 10
typedef struct {
int fd;
struct sockaddr_in clients[MAX_CLIENTS + 1];
} DATA;
void *process_clients(void *p) {
DATA *data = (DATA *)p;
char buf[BUFF_SIZE + 1];
while (1) {
off_t ret = read(STDIN_FILENO, buf, sizeof(buf));
if (ret <= 0)
continue;
socklen_t len = sizeof(data->clients[0]);
for (size_t i = 0; i < MAX_CLIENTS; i++)
sendto(data->fd, buf, ret, 0, (struct sockaddr *)&data->clients[i], len);
}
}
int main(void) {
signal(SIGPIPE, SIG_IGN);
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
goto ERROR_WITHOUTCLOSE;
struct timeval tv = {.tv_sec = 1, .tv_usec = 0};
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;
static DATA data;
data.fd = fd;
pthread_t td;
pthread_create(&td, NULL, process_clients, (void *)&data);
int last = 0;
char buf[25];
socklen_t len = sizeof(data.clients[last]);
while (1) {
if (recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&data.clients[last], &len) > 0)
if (last <= MAX_CLIENTS)
last++;
}
ERROR:
close(fd);
ERROR_WITHOUTCLOSE:
fprintf(stderr, "streamer: %s\n", strerror(errno));
return 1;
}