Use tcp instead udp

This commit is contained in:
Your Name 2024-02-28 23:26:03 +03:00
parent dda82161ff
commit 32d06992f7
2 changed files with 26 additions and 19 deletions

View File

@ -1,4 +1,4 @@
#Multiuser udp server for broadcasting music #Multiuser tcp server for broadcasting music
#Record audio in sterio #Record audio in sterio
@ -6,4 +6,4 @@ arecord -D loopout --buffer-size=20000 -f S16_LE -t raw -r 48000 -c 2 | ./server
#Geting raw pcm data and send to speaker #Geting raw pcm data and send to speaker
echo "JOIN" | nc -u server_ip server_port | aplay -r 48000 -f S16_LE -c 2 nc server_ip server_port | aplay -r 48000 -f S16_LE -c 2

View File

@ -9,13 +9,14 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <pthread.h> #include <pthread.h>
#define PORT 8889 #define PORT 8888
#define BUFF_SIZE 8192 #define BUFF_SIZE 8192
#define MAX_CLIENTS 125 #define MAX_CLIENTS 125
typedef struct { typedef struct {
int fd; int fd;
struct sockaddr_in clients[MAX_CLIENTS + 1]; struct sockaddr_in clients[MAX_CLIENTS + 1];
int client_fds[MAX_CLIENTS + 1];
} DATA; } DATA;
void *process_clients(void *p) { void *process_clients(void *p) {
@ -29,21 +30,20 @@ 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++)
sendto(data->fd, buf, ret, 0, (struct sockaddr *)&data->clients[i], len); 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;
} }
} }
int main(void) { int main(void) {
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
int fd = socket(AF_INET, SOCK_DGRAM, 0); int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) if (fd < 0)
goto ERROR_WITHOUTCLOSE; goto ERROR_WITHOUTCLOSE;
struct timeval tv = {.tv_sec = 0, .tv_usec = 50}; struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
goto ERROR;
if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
goto ERROR; goto ERROR;
@ -58,24 +58,31 @@ 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)
goto ERROR;
/* Thread of common buffer */
static DATA data; static DATA data;
memset(data.client_fds, -1, sizeof(data.client_fds));
data.fd = fd; 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);
int last = 0; /* Get clients */
char buf[1]; socklen_t len = sizeof(addr);
socklen_t len = sizeof(struct sockaddr_in);
while (1) { while (1) {
if (last <= MAX_CLIENTS) { int client_fd = accept(fd, (struct sockaddr *)&addr, &len);
if (recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&data.clients[last], &len) > 0) if (client_fd < 0)
last++; continue;
}
else for (size_t i = 0; i < MAX_CLIENTS; i++) {
last = 0; if (data.client_fds[i] == -1) {
data.clients[i] = addr;
data.client_fds[i] = client_fd;
break;
}
}
} }
ERROR: ERROR: