new tester

This commit is contained in:
asus
2023-02-17 15:03:41 +01:00
parent 356267e288
commit 101d7b7581
24 changed files with 87 additions and 12648 deletions

1
1 Normal file
View File

@@ -0,0 +1 @@
Wrong number of arguments

BIN
a.out

Binary file not shown.

View File

@@ -1,19 +0,0 @@
Le passage de Lorem Ipsum standard, utilisé depuis 1500
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Section 1.10.32 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.)
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
Traduction de H. Rackham (1914)
"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
Section 1.10.33 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.)
"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
Traduction de H. Rackham (1914)
"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."

View File

@@ -1,3 +0,0 @@
server: client 1 just arrived
client 1: coucou
server: client 1 just left

View File

@@ -1,25 +0,0 @@
server: client 1 just arrived
client 1: coucou
server: client 1 just left
server: client 2 just arrived
client 2: good morning
server: client 2 just left
server: client 3 just arrived
client 3: holà que tal?
server: client 3 just left
server: client 4 just arrived
client 4: ␛[32mGREEN
client 4: ␛[31mRED
server: client 4 just left
server: client 5 just arrived
client 5:
server: client 5 just left
server: client 6 just arrived
client 6: ␛[0mhello bonjour
server: client 6 just left
server: client 7 just arrived
client 7: last one
server: client 7 just left
server: client 8 just arrived
client 8: ✅
server: client 8 just left

View File

@@ -1,21 +0,0 @@
server: client 1 just arrived
client 1: Le passage de Lorem Ipsum standard, utilisé depuis 1500
client 1:
client 1: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
client 1:
client 1: Section 1.10.32 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.)
client 1:
client 1: "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
client 1:
client 1: Traduction de H. Rackham (1914)
client 1:
client 1: "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
client 1:
client 1: Section 1.10.33 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.)
client 1:
client 1: "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
client 1:
client 1: Traduction de H. Rackham (1914)
client 1:
client 1: "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."
server: client 1 just left

View File

@@ -1,4 +0,0 @@
server: client 1 just arrived
server: client 2 just arrived
client 2: hello
server: client 2 just left

View File

@@ -1,3 +0,0 @@
server: client 2 just arrived
client 2: hello
server: client 2 just left

View File

@@ -1,22 +0,0 @@
server: client 1 just arrived
server: client 2 just arrived
client 2: Le passage de Lorem Ipsum standard, utilisé depuis 1500
client 2:
client 2: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
client 2:
client 2: Section 1.10.32 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.)
client 2:
client 2: "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
client 2:
client 2: Traduction de H. Rackham (1914)
client 2:
client 2: "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
client 2:
client 2: Section 1.10.33 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.)
client 2:
client 2: "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
client 2:
client 2: Traduction de H. Rackham (1914)
client 2:
client 2: "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."
server: client 2 just left

View File

@@ -1,21 +0,0 @@
server: client 2 just arrived
client 2: Le passage de Lorem Ipsum standard, utilisé depuis 1500
client 2:
client 2: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
client 2:
client 2: Section 1.10.32 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.)
client 2:
client 2: "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
client 2:
client 2: Traduction de H. Rackham (1914)
client 2:
client 2: "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
client 2:
client 2: Section 1.10.33 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.)
client 2:
client 2: "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
client 2:
client 2: Traduction de H. Rackham (1914)
client 2:
client 2: "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."
server: client 2 just left

View File

@@ -1,151 +0,0 @@
#!/bin/bash
ARGS="Wrong number of arguments"
FATAL="Fatal error"
err=0
port=0
while [ $port -lt 1024 -o $port -gt 10000 ]; do
port=$RANDOM
done
echo "Running tests on port $port..."
check()
{
# check exit status of most recently executed command
# [ $? -eq 1 ] && echo -e "✅\c" || { echo -e "❌\c"; err=$(( $err + 1 )); }
if [ $? -eq 1 ]
then
echo -e "✅\c";
else
echo -e "❌\c";
err=$(( $err + 1 ));
fi
# compare output of cat with first argument
# [ "$(cat log.txt)" = "$1" ] && echo -e "\033[70G✅" || { echo -e "\033[70G❌"; err=$(( $err + 1 )); }
if [ "$(cat log.txt)" = "$1" ]
then
echo -e "\033[70G✅";
else
echo -e "\033[70G❌";
err=$(( $err + 1 ));
fi
}
check_diff()
{
INDENT=50
echo "coucou"
case "$1" in
5 | 7)
INDENT=55
;;
esac
diff "log$1.txt" "assets/test$1" &> /dev/null
[ $? -eq 0 ] && echo -e "\033[${INDENT}G✅\c" || { echo -e "\033[${INDENT}G❌\c"; err=$(( $err + 1 )); }
}
### COMPIL
echo -e "\033[36m[+] Checking compilation...\033[0m\033[50G\c"
clang -Wall -Wextra -Werror mini_serv.c -o mini_serv &> log.txt
[ "$(cat log.txt)" = "" ] && echo "✅"|| { echo -e "\033[31mCompilation error...\033[0m"; cat log.txt; rm log.txt; exit 1; }
### HANDLE ERRORS
echo -e "\n\033[36m[+] Checking errors\033[0m\033[50GExit status\033[70GDiff Output"
echo -e "\033[33m[+] Checking no arguments output...\033[0m\033[50G\c"
./mini_serv &> log.txt
check "$ARGS"
echo -e "\033[33m[+] Checking two many arguments output...\033[0m\033[50G\c"
./mini_serv 1 2 &> log.txt
check "$ARGS"
echo -e "\033[33m[+] Checking syscall error output...\033[0m\033[50G\c"
./mini_serv "$port" & > log.txt
./mini_serv "$port" &> log.txt
check "$FATAL"
echo "[+] Killing mini_serv..."
killall -q mini_serv
### DIFF OUTPUT
echo -e "\n\033[36m[+] Checking output...\033[0m\033[50G"
echo -e "\033[33m[+] 1 client and 1 message...\033[0m\033[50G\c"
./mini_serv "$port" & > log1.txt
r=$(nc localhost "$port" > log1.txt) &
echo "coucou" | nc localhost "$port"
check_diff 1
echo -e "\n[+] Killing mini_serv..."
killall -q mini_serv
echo -e "\n\033[33m[+] 1 client and many messages...\033[0m\033[50G\c"
port=$(( $port - 5 ))
./mini_serv "$port" & > log2.txt
r=$(nc localhost "$port" > log2.txt) &
sleep 1
echo "coucou" | nc localhost "$port"
echo "good morning" | nc localhost "$port"
echo "holà que tal?" | nc localhost "$port"
echo -e "\033[32mGREEN\n\t\033[31mRED" | nc localhost "$port"
echo "" | nc localhost "$port"
echo -e "\033[0mhello\t\tbonjour" | nc localhost "$port"
echo "last one" | nc localhost "$port"
echo "✅" | nc localhost "$port"
check_diff 2
echo -e "\n[+] Killing mini_serv..."
killall -q mini_serv
echo -e "\n\033[33m[+] 1 client and multi lines...\033[0m\033[50G\c"
./mini_serv "$(( $port + 1 ))" & > log3.txt
r=$(nc localhost "$(( $port + 1 ))" > log3.txt) &
sleep 1
cat assets/lorem | nc localhost "$(( $port + 1 ))"
check_diff 3
echo -e "\n[+] Killing mini_serv..."
killall -q mini_serv
echo -e "\n\033[33m[+] 2 clients and a simple message...\033[0m\033[50G\c"
./mini_serv "$(( $port + 2 ))" & > log4.txt
r=$(nc localhost "$(( $port + 2 ))" > log4.txt) &
sleep 1
r2=$(nc localhost "$(( $port + 2 ))" > log5.txt) &
sleep 1
echo "hello" | nc localhost "$(( $port + 2 ))"
check_diff 4
check_diff 5
echo -e "\n[+] Killing mini_serv..."
killall -q mini_serv
echo -e "\n\033[33m[+] 2 clients and multi ligne message...\033[0m\033[50G\c"
./mini_serv "$(( $port - 1 ))" & > log6.txt
r=$(nc localhost "$(( $port - 1 ))" > log6.txt) &
sleep 1
r2=$(nc localhost "$(( $port - 1 ))" > log7.txt) &
sleep 1
cat assets/lorem | nc localhost "$(( $port - 1 ))"
check_diff 6
check_diff 7
echo -e "\n[+] Killing mini_serv..."
killall -q mini_serv
### SUMMARY
[ $err -eq 0 ] && echo -e "\n\033[32mAll tests passed :)\033[0m" || echo -e "\033[31m$err test(s) failed :(\033[0m"
### CLEAN
rm -rf mini_serv log*.txt
exit $err

6000
log.txt

File diff suppressed because it is too large Load Diff

6000
log1.txt

File diff suppressed because it is too large Load Diff

BIN
mini_serv

Binary file not shown.

View File

@@ -1,108 +1,100 @@
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> //sprintf #include <stdio.h>
#define MSGSIZE 1024
#define BUFSIZE 42000 #define BUFSIZE MSGSIZE + 20
typedef struct s_client { typedef struct s_client {
int id; int id;
char msg[1024]; char msg[MSGSIZE];
} t_client; } t_client;
void error(char *str) { void error(char *msg) {
write(2, str, strlen(str)); write(2, msg, strlen(msg));
exit(1); exit(1);
} }
int init_socket(struct sockaddr_in *addr, int len, int port) { void broadcast(char *msg, fd_set *set, int max_fd, int server_fd, int current_fd) {
int server_fd;
if ( (server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
error("Fatal error2\n");
bzero(addr, len);
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = htonl(2130706433); //127.0.0.1
addr->sin_port = htons(port);
if (bind(server_fd, (const struct sockaddr *)addr, len) == -1)
error("Fatal error3\n");
if (listen(server_fd, 10) == -1)
error("Fatal error4\n");
return server_fd;
}
void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd) {
for(int i = 0; i <= max_fd; ++i) { for(int i = 0; i <= max_fd; ++i) {
if (i == server_fd) if (i == server_fd)
continue; continue;
if (i == sender_fd) if (i == current_fd)
continue; continue;
if (FD_ISSET(i, set)) if (FD_ISSET(i, set))
send(i, buf, strlen(buf), 0); send(i, msg, strlen(msg), 0);
} }
} }
int main(int ac, char **av) { int main(int ac, char **av) {
int server_fd; int server_fd;
int client_fd; int client_fd;
int client_id; int id;
int ret;
int port; int port;
int max_fd; int max_fd;
int ret;
t_client clients[FD_SETSIZE];
t_client client;
char buf[BUFSIZE]; char buf[BUFSIZE];
char msg[BUFSIZE]; char msg[BUFSIZE];
struct sockaddr_in addr;
struct sockaddr_in addr;
socklen_t addr_len; socklen_t addr_len;
fd_set fdset; fd_set fdset;
fd_set rdset; fd_set rdset;
t_client clients[FD_SETSIZE];
if (ac != 2) if (ac != 2)
error("Wrong number of arguments\n"); error("Wrong number of arguments\n");
if ( (port = atoi(av[1])) == -1) if ( (port = atoi(av[1])) == -1)
error("Fatal error1\n"); error("Fatal error1\n");
addr_len = sizeof(addr); addr_len = sizeof(addr);
bzero(&addr, addr_len);
server_fd = init_socket(&addr, addr_len, port); // socket create and verification
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1)
error("Fatal error2\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 error3\n");
if (listen(server_fd, 10) == -1)
error("Fatal error4\n");
FD_ZERO(&fdset); FD_ZERO(&fdset);
FD_SET(server_fd, &fdset); FD_SET(server_fd, &fdset);
max_fd = server_fd; max_fd = server_fd;
client_id = 0; id = 0;
bzero(&clients, sizeof(clients));
while (1) { while(1) {
rdset = fdset; rdset = fdset;
select(max_fd + 1, &rdset, NULL, NULL, NULL); select(max_fd + 1, &rdset, NULL, NULL, NULL);
// new connection
if (FD_ISSET(server_fd, &rdset)) { if (FD_ISSET(server_fd, &rdset)) {
client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
FD_SET(client_fd, &fdset); FD_SET(client_fd, &fdset);
clients[client_fd].id = client_id;
if (client_fd > max_fd) if (client_fd > max_fd)
max_fd = client_fd; max_fd = client_fd;
sprintf(msg, "server: client %d just arrived\n", client_id); 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, &fdset, max_fd, server_fd, client_fd);
client_id++; id++;
} }
// new message
client_fd = 0; client_fd = 0;
while(client_fd <= max_fd) { while(client_fd <= max_fd) {
ret = 1; ret = 1;
if (FD_ISSET(client_fd, &rdset)) { if (FD_ISSET(client_fd, &rdset)) {
bzero(buf, BUFSIZE);
ret = recv(client_fd, buf, BUFSIZE, 0); ret = recv(client_fd, buf, BUFSIZE, 0);
if (ret == 0) { if (ret == 0) {
sprintf(msg, "server: client %d just left\n", clients[client_fd].id); sprintf(msg, "server: client %d just left\n", clients[client_fd].id);
@@ -117,25 +109,23 @@ int main(int ac, char **av) {
} }
} }
else if (ret > 0) { else if (ret > 0) {
for(int i = 0, j = strlen(clients[client_fd].msg); i < ret; ++i, ++j) {
client = clients[client_fd]; clients[client_fd].msg[j] = buf[i];
for(int i = 0, j = strlen(client.msg); i < ret; ++i, ++j) {
client.msg[j] = buf[i];
if (buf[i] == '\n') { if (buf[i] == '\n') {
client.msg[j] = '\0'; clients[client_fd].msg[j] = '\0';
sprintf(msg, "client %d: %s\n", client.id, client.msg); 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, &fdset, max_fd, server_fd, client_fd);
bzero(client.msg, strlen(client.msg)); bzero(clients[client_fd].msg, MSGSIZE);
j = -1; j = -1;
} }
} }
} }
} }
client_fd++; ++client_fd;
} }
} }
return (0); return(0);
} }

View File

@@ -1,141 +0,0 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h> //sprintf
#define BUFSIZE 42000
typedef struct s_client {
int id;
char msg[1024];
} t_client;
void error(char *str) {
write(2, str, strlen(str));
exit(1);
}
int init_socket(struct sockaddr_in *addr, int len, int port) {
int server_fd;
if ( (server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
error("Fatal error2\n");
bzero(addr, len);
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = htonl(2130706433); //127.0.0.1
addr->sin_port = htons(port);
if (bind(server_fd, (const struct sockaddr *)addr, len) == -1)
error("Fatal error3\n");
if (listen(server_fd, 10) == -1)
error("Fatal error4\n");
return server_fd;
}
void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd) {
for(int i = 0; i <= max_fd; ++i) {
if (i == server_fd)
continue;
if (i == sender_fd)
continue;
if (FD_ISSET(i, set))
send(i, buf, strlen(buf), 0);
}
}
int main(int ac, char **av) {
int server_fd;
int client_fd;
int client_id;
int port;
int max_fd;
int ret;
t_client clients[FD_SETSIZE];
t_client client;
char buf[BUFSIZE];
char msg[BUFSIZE];
struct sockaddr_in addr;
socklen_t addr_len;
fd_set fdset;
fd_set rdset;
if (ac != 2)
error("Wrong number of arguments\n");
if ( (port = atoi(av[1])) == -1)
error("Fatal error1\n");
addr_len = sizeof(addr);
server_fd = init_socket(&addr, addr_len, port);
FD_ZERO(&fdset);
FD_SET(server_fd, &fdset);
max_fd = server_fd;
client_id = 0;
bzero(&clients, sizeof(clients));
while (1) {
rdset = fdset;
select(max_fd + 1, &rdset, NULL, NULL, NULL);
// new connection
if (FD_ISSET(server_fd, &rdset)) {
client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
FD_SET(client_fd, &fdset);
clients[client_fd].id = client_id;
if (client_fd > max_fd)
max_fd = client_fd;
sprintf(msg, "server: client %d just arrived\n", client_id);
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
client_id++;
}
// new message
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, &fdset, max_fd, server_fd, client_fd);
FD_CLR(client_fd, &fdset);
close(client_fd);
if (client_fd == max_fd) {
for(; max_fd > 2; --max_fd) {
if (FD_ISSET(max_fd, &fdset))
break;
}
}
}
else if (ret > 0) {
client = clients[client_fd];
for(int i = 0, j = strlen(client.msg); i < ret; ++i, ++j) {
client.msg[j] = buf[i];
if (buf[i] == '\n') {
client.msg[j] = '\0';
sprintf(msg, "client %d: %s\n", client.id, client.msg);
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
bzero(client.msg, strlen(client.msg));
j = -1;
}
}
}
}
client_fd++;
}
}
return (0);
}

View File

@@ -1,125 +0,0 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFSIZE 42000
typedef struct s_client {
int id;
char msg[1024];
} t_client;
// GLOBALS
int server_fd;
int id;
int max_fd;
socklen_t addr_len;
fd_set fdset;
t_client clients[FD_SETSIZE];
void error(char *msg) {
write(2, msg, strlen(msg));
exit(1);
}
void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd) {
for(int i = 0; i <= max_fd; ++i) {
if (i == server_fd)
continue;
if (i == sender_fd)
continue;
if (FD_ISSET(i, set))
send(i, buf, strlen(buf), 0);
}
}
void handle_socket(struct sockaddr *addr) {
fd_set rdset;
int client_fd;
int ret;
t_client client;
char buf[BUFSIZE];
char msg[BUFSIZE];
rdset = fdset;
select(max_fd + 1, &rdset, NULL, NULL, NULL);
if (FD_ISSET(server_fd, &rdset)) {
client_fd = accept(server_fd, addr, &addr_len);
FD_SET(client_fd, &fdset);
if (client_fd > max_fd)
max_fd = client_fd;
client = clients[client_fd];
client.id = id;
bzero(client.msg, strlen(client.msg));
sprintf(buf, "server: client %d arrived\n", id);
}
client_fd = 0;
while(client_fd < max_fd) {
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, &fdset, max_fd, server_fd, client_fd);
FD_CLR(client_fd, &fdset);
}
else if (ret > 0) {
client = clients[client_fd];
for(int i = 0, j = strlen(client.msg); i < ret; ++i, ++j) {
client.msg[j] = buf[i];
if (buf[i] == '\n') {
client.msg[j] = '\0';
sprintf(msg, "client %d: %s\n", client.id, client.msg);
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
bzero(client.msg, strlen(client.msg));
j = -1;
}
}
}
}
client_fd++;
}
}
int main(int ac, char **av) {
struct sockaddr_in addr;
int port;
if (ac != 2)
error("Fatal error\n");
port = atoi(av[1]);
addr_len = sizeof(addr);
// socket create and verification
if ( (server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
error("Fatal error\n");
bzero(&addr, addr_len);
// 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);
max_fd = server_fd;
while(1)
handle_socket((struct sockaddr *)&addr);
return (0);
}

View File

@@ -1,51 +0,0 @@
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define BUFSIZE 160000
#define MSGSIZE 160000
int main() {
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
servaddr.sin_port = htons(8081);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
if (listen(sockfd, 10) != 0) {
printf("cannot listen\n");
exit(0);
}
len = sizeof(cli);
connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
}

View File

@@ -1,15 +1,33 @@
#!/bin/bash #!/bin/bash
port=8081 port=0
clang -Wall -Wextra -Werror mini_serv.c -o mini_serv while [ $port -lt 1024 -o $port -gt 10000 ]; do
./mini_serv "$port" & port=$RANDOM
nc localhost "$port" & done
clang -Wall -Wextra -Werror mini_serv.c
./a.out "$port" 2>&1 &
nc localhost "$port" 2>&1 &
sleep 0.1
test1="on short line"
test2="line 1
line 2
line 4"
test3="one long line qqqqqqqqqqqqqq wwwwwwwwwwww eeeeeeeeeeeeee rrrrrrrrrrrrr tttttttttttt yyyyyyyyyyyyyyy uy iiiiiiiiiiiiiiiii oooooooo"
echo "$test1" | nc localhost "$port" 2>&1 &
sleep 0.1
echo "$test2" | nc localhost "$port" 2>&1 &
sleep 0.1
echo "$test3" | nc localhost "$port" 2>&1 &
sleep 0.1
for i in {1..3000} for i in {1..3000}
do do
nc localhost "$port" & nc localhost "$port" 2>&1 &
kill -KILL $(pidof nc | tr ' ' '\n' | head -n1) kill -KILL $(pidof nc | tr ' ' '\n' | head -n1)
done done
killall -q mini_serv nc killall -q a.out nc

16
utils/commands.txt Normal file
View File

@@ -0,0 +1,16 @@
clang -Wall -Wextra -Werror mini_serv.c
./a.out <port>
nc localhost <port>
echo "test1" | nc localhost <port>
bash tester.sh 2>/dev/null
bash tester.sh 2>/dev/null | sort 1>log1.txt
rm mini_serv.c; cp mini_serv_03.c mini_serv.c; bash tester.sh 2>/dev/null | sort 1>log1.txt