This commit is contained in:
asus
2023-02-20 16:19:41 +01:00
parent 3ed006b652
commit 1d690b43bd
9 changed files with 12371 additions and 75 deletions
BIN
View File
Binary file not shown.
+6000 -41
View File
File diff suppressed because it is too large Load Diff
+5997 -12
View File
File diff suppressed because it is too large Load Diff
+84
View File
File diff suppressed because one or more lines are too long
+84
View File
File diff suppressed because one or more lines are too long
+8 -8
View File
@@ -8,7 +8,7 @@
#include <stdio.h>
#define MSGSIZE 1024
#define BUFSIZE MSGSIZE + 120000
#define BUFSIZE 120000
typedef struct s_client {
int id;
@@ -20,10 +20,8 @@ void error(char *msg) {
exit(1);
}
void broadcast(char *msg, fd_set *set, int max_fd, int server_fd, int current_fd) {
void broadcast(char *msg, fd_set *set, int max_fd, int current_fd) {
for(int i = 0; i <= max_fd; ++i) {
if (i == server_fd)
continue;
if (i == current_fd)
continue;
if (FD_ISSET(i, set))
@@ -44,6 +42,7 @@ int main(int ac, char **av) {
socklen_t addr_len;
fd_set fdset;
fd_set rdset;
fd_set wdset;
t_client clients[FD_SETSIZE];
if (ac != 2)
@@ -76,8 +75,9 @@ int main(int ac, char **av) {
while(1) {
rdset = fdset;
wdset = fdset;
select(max_fd + 1, &rdset, NULL, NULL, NULL);
select(max_fd + 1, &rdset, &wdset, NULL, NULL);
if (FD_ISSET(server_fd, &rdset)) {
client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
@@ -87,7 +87,7 @@ int main(int ac, char **av) {
clients[client_fd].id = id;
bzero(clients[client_fd].msg, MSGSIZE);
sprintf(msg, "server: client %d just arrived\n", id);
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
broadcast(msg, &wdset, max_fd, client_fd);
id++;
}
@@ -97,7 +97,7 @@ int main(int ac, char **av) {
ret = recv(client_fd, buf, BUFSIZE, 0);
if (ret == 0) {
sprintf(msg, "server: client %d just left\n", clients[client_fd].id);
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
broadcast(msg, &wdset, max_fd, client_fd);
FD_CLR(client_fd, &fdset);
close(client_fd);
if (client_fd == max_fd) {
@@ -113,7 +113,7 @@ int main(int ac, char **av) {
if (buf[i] == '\n') {
clients[client_fd].msg[j] = '\0';
sprintf(msg, "client %d: %s\n", clients[client_fd].id, clients[client_fd].msg);
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
broadcast(msg, &wdset, max_fd, client_fd);
bzero(clients[client_fd].msg, MSGSIZE);
j = -1;
}
+126
View File
@@ -0,0 +1,126 @@
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define MSGSIZE 1024
#define BUFSIZE 120000
typedef struct s_client {
int id;
char msg[MSGSIZE];
} t_client;
void error(char *msg) {
write(2, msg, strlen(msg));
exit (1);
}
void broadcast(char *msg, fd_set *set, int max_fd, int client_fd) {
for(int fd = 0; fd < max_fd; ++fd) {
if (fd == client_fd)
continue;
if (FD_ISSET(fd, set))
send(fd, msg, strlen(msg), 0);
}
}
int main(int ac, char **av) {
int server_fd;
int client_fd;
int id;
int ret;
int port;
int max_fd;
struct sockaddr_in addr;
socklen_t addr_len;
fd_set fdset;
fd_set rdset;
fd_set wdset;
char buf[BUFSIZE];
char msg[BUFSIZE];
t_client clients[FD_SETSIZE];
if (ac != 2)
error("Wrong number of arguments\n");
if ( (port = atoi(av[1])) == -1)
error("Fatal error\n");
addr_len = sizeof(addr);
bzero(&addr, addr_len);
// socket create and verification
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1)
error("Fatal error\n");
// assign IP, PORT
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
addr.sin_port = htons(port);
// Binding newly created socket to given IP and verification
if ((bind(server_fd, (const struct sockaddr *)&addr, addr_len)) == -1)
error("Fatal error\n");
if (listen(server_fd, 10) == -1)
error("Fatal error\n");
FD_ZERO(&fdset);
FD_SET(server_fd, &fdset);
id = 0;
max_fd = server_fd;
while(1) {
rdset = fdset;
wdset = fdset;
select(max_fd + 1, &rdset, &wdset, NULL, NULL);
if (FD_ISSET(server_fd, &rdset)) {
client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
FD_SET(client_fd, &fdset);
if (client_fd > max_fd)
max_fd = client_fd;
clients[client_fd].id = id;
bzero(clients[client_fd].msg, MSGSIZE);
sprintf(msg, "server: client %d just arrived\n", id);
broadcast(msg, &wdset, max_fd, client_fd);
id++;
}
client_fd = 0;
while(client_fd <= max_fd) {
ret = 1;
if (FD_ISSET(client_fd, &rdset)) {
bzero(buf, BUFSIZE);
ret = recv(client_fd, buf, BUFSIZE, 0);
if (ret == 0) {
sprintf(msg, "server: client %d just left\n", clients[client_fd].id);
broadcast(msg, &wdset, max_fd, client_fd);
FD_CLR(client_fd, &fdset);
close(client_fd);
}
else if (ret > 0) {
for(int i = 0, j = strlen(clients[client_fd].msg); i < ret; ++i, ++j) {
clients[client_fd].msg[j] = buf[i];
if (buf[i] == '\n') {
clients[client_fd].msg[j] = '\0';
sprintf(msg, "client %d: %s\n", clients[client_fd].id, clients[client_fd].msg);
broadcast(msg, &wdset, max_fd, client_fd);
bzero(clients[client_fd].msg, MSGSIZE);
j = -1;
}
}
}
}
client_fd++;
}
}
return (0);
}
+35 -14
View File
@@ -1,9 +1,24 @@
#!/bin/bash
path="mini_serv.c"
nine=0
for var in "$@"
do
if [ "$var" = "nine" ]
then
nine=1
else
path="$var"
fi
done
port=0
while [ $port -lt 1024 -o $port -gt 10000 ]; do
while [ $port -lt 1024 -o $port -gt 10000 ]
do
port=$RANDOM
done
id=1
test_number=0
@@ -13,7 +28,8 @@ launch_client() {
while read -r line
do
echo "$test_number - receiver $id $(date +%s%N): $line"
#echo "$test_number - receiver $id $(date +%s%N): $line"
echo "$test_number - receiver $id : $line"
done < <(echo "$1" | nc localhost "$port" 2>&1) &
sleep 0.1
@@ -32,10 +48,26 @@ new_test() {
launch_client ""
}
clang -Wall -Wextra -Werror mini_serv.c 2>&1
clang -Wall -Wextra -Werror $path 2>&1
./a.out "$port" 2>&1 &
## # # # # # # # # # # # # # # # # # # # # # # # # #
# #
if [ "$nine" -eq 1 ]
then
nc localhost "$port" 2>&1 &
for i in {1..3000}
do
#launch_client
nc localhost "$port" 2>&1 &
stop_last_client
done
killall -q a.out nc
exit 1
fi
# # # # # # # # # # # # # # # # # # # # # # # # # #
#
new_test "open a client an close, 3 times"
@@ -96,17 +128,6 @@ launch_client "$text"
stop_last_client
## # # # # # # # # # # # # # # # # # # # # # # # # #
# #
for i in {1..3000}
do
#launch_client
nc localhost "$port" 2>&1 &
stop_last_client
done
echo ""
echo "done"
killall -q a.out nc
+37
View File
@@ -0,0 +1,37 @@
#!/bin/bash
port=0
while [ "$port" -lt 1024 -o "$port" -gt 10000 ]
do
port=$RANDOM
done
id=0
launch() {
while read -r line
do
echo "receiver $id: $line"
done < <(nc 127.0.0.1 "$port" 2>&1) &
((++id))
}
clang -Wall -Wextra -Werror mini_serv.c
./a.out "$port" 2>&1 &
launch
#launch
#launch
#launch
#kill -KILL $(pidof nc | tr ' ' '\n' | head -n1 )
#launch
#kill -KILL $(pidof nc | tr ' ' '\n' | head -n1 )
#launch
#kill -KILL $(pidof nc | tr ' ' '\n' | head -n1 )
for i in {1..30}
do
launch
kill -KILL $(pidof nc | tr ' ' '\n' | head -n1 )
done
killall -q a.out nc