mini tester ok
This commit is contained in:
@@ -108,6 +108,13 @@ int main(int ac, char **av) {
|
|||||||
sprintf(msg, "server: client %d just left\n", clients[client_fd].id);
|
sprintf(msg, "server: client %d just left\n", clients[client_fd].id);
|
||||||
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
||||||
FD_CLR(client_fd, &fdset);
|
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) {
|
else if (ret > 0) {
|
||||||
|
|
||||||
|
|||||||
@@ -108,6 +108,13 @@ int main(int ac, char **av) {
|
|||||||
sprintf(msg, "server: client %d just left\n", clients[client_fd].id);
|
sprintf(msg, "server: client %d just left\n", clients[client_fd].id);
|
||||||
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
||||||
FD_CLR(client_fd, &fdset);
|
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) {
|
else if (ret > 0) {
|
||||||
|
|
||||||
|
|||||||
141
mini_serv_03.c
141
mini_serv_03.c
@@ -1,128 +1,51 @@
|
|||||||
|
#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 <stdio.h>
|
|
||||||
|
|
||||||
#define BUFSIZE 160000
|
#define BUFSIZE 160000
|
||||||
#define MSGSIZE 1024
|
#define MSGSIZE 160000
|
||||||
|
|
||||||
typedef struct s_client {
|
int main() {
|
||||||
int id;
|
int sockfd, connfd, len;
|
||||||
char msg[MSGSIZE];
|
struct sockaddr_in servaddr, cli;
|
||||||
} t_client;
|
|
||||||
|
|
||||||
void error(char *msg) {
|
|
||||||
write(1, msg, strlen(msg));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void broadcast(fd_set *set, int max_fd, char *msg, int server_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))
|
|
||||||
send(i, msg, strlen(msg), 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int ac, char **av) {
|
|
||||||
int server_fd;
|
|
||||||
int client_fd;
|
|
||||||
int id;
|
|
||||||
int port;
|
|
||||||
int max_fd;
|
|
||||||
int ret;
|
|
||||||
struct sockaddr_in addr;
|
|
||||||
socklen_t addr_len;
|
|
||||||
fd_set fdset;
|
|
||||||
fd_set rdset;
|
|
||||||
t_client clients[FD_SETSIZE];
|
|
||||||
t_client client;
|
|
||||||
char buf[BUFSIZE];
|
|
||||||
char msg[BUFSIZE];
|
|
||||||
|
|
||||||
if (ac != 2)
|
|
||||||
error("Wrong number of arguments\n");
|
|
||||||
if ( (port = atoi(av[1])) == -1)
|
|
||||||
error("Fatal error\n");
|
|
||||||
addr_len = sizeof(addr);
|
|
||||||
|
|
||||||
// socket create and verification
|
// socket create and verification
|
||||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (server_fd == -1)
|
if (sockfd == -1) {
|
||||||
error("Fatal error\n");
|
printf("socket creation failed...\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("Socket successfully created..\n");
|
||||||
|
bzero(&servaddr, sizeof(servaddr));
|
||||||
|
|
||||||
// assign IP, PORT
|
// assign IP, PORT
|
||||||
addr.sin_family = AF_INET;
|
servaddr.sin_family = AF_INET;
|
||||||
addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
||||||
addr.sin_port = htons(port);
|
servaddr.sin_port = htons(8081);
|
||||||
|
|
||||||
// Binding newly created socket to given IP and verification
|
// Binding newly created socket to given IP and verification
|
||||||
if ((bind(server_fd, (const struct sockaddr *)&addr, addr_len)) == -1)
|
if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
|
||||||
error("Fatal error\n");
|
printf("socket bind failed...\n");
|
||||||
if (listen(server_fd, 10) == -1)
|
exit(0);
|
||||||
error("Fatal error\n");
|
|
||||||
|
|
||||||
FD_ZERO(&fdset);
|
|
||||||
FD_SET(server_fd, &fdset);
|
|
||||||
max_fd = server_fd;
|
|
||||||
id = 0;
|
|
||||||
bzero(&clients, sizeof(clients));
|
|
||||||
|
|
||||||
while(1) {
|
|
||||||
rdset = fdset;
|
|
||||||
|
|
||||||
select(max_fd + 1, &rdset, NULL, 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;
|
|
||||||
client = clients[client_fd];
|
|
||||||
client.id = id;
|
|
||||||
bzero(client.msg, MSGSIZE);
|
|
||||||
sprintf(msg, "server: client %d arrived\n", client.id);
|
|
||||||
broadcast(&fdset, max_fd, msg, server_fd, client_fd);
|
|
||||||
id++;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
client_fd = 0;
|
printf("Socket successfully binded..\n");
|
||||||
while(client_fd <= max_fd) {
|
if (listen(sockfd, 10) != 0) {
|
||||||
ret = 1;
|
printf("cannot listen\n");
|
||||||
if (FD_ISSET(client_fd, &rdset)) {
|
exit(0);
|
||||||
|
|
||||||
client = clients[client_fd];
|
|
||||||
bzero(buf, BUFSIZE);
|
|
||||||
ret = recv(client_fd, buf, BUFSIZE, 0);
|
|
||||||
|
|
||||||
if (ret == 0) {
|
|
||||||
sprintf(msg, "server: client %d left\n", client.id);
|
|
||||||
broadcast(&fdset, max_fd, msg, server_fd, client_fd);
|
|
||||||
FD_CLR(client_fd, &fdset);
|
|
||||||
}
|
}
|
||||||
|
len = sizeof(cli);
|
||||||
else if (ret > 0) {
|
connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
|
||||||
for(int i = 0, j = strlen(client.msg); i < ret; ++i, ++j) {
|
if (connfd < 0) {
|
||||||
client.msg[j] = buf[i];
|
printf("server acccept failed...\n");
|
||||||
if (buf[i] == '\n') {
|
exit(0);
|
||||||
client.msg[j] = '\0';
|
|
||||||
sprintf(msg, "client %d: %s\n", client.id, client.msg);
|
|
||||||
broadcast(&fdset, max_fd, msg, server_fd, client_fd);
|
|
||||||
bzero(client.msg, MSGSIZE);
|
|
||||||
j = -1;
|
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
}
|
printf("server acccept the client...\n");
|
||||||
}
|
|
||||||
client_fd++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
10
tester.sh
10
tester.sh
@@ -1,17 +1,11 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
port=0
|
port=8081
|
||||||
while [ $port -lt 1024 -o $port -gt 10000 ]; do
|
|
||||||
port=$RANDOM
|
|
||||||
done
|
|
||||||
echo "Running tests on port $port..."
|
|
||||||
|
|
||||||
clang -Wall -Wextra -Werror mini_serv.c -o mini_serv
|
clang -Wall -Wextra -Werror mini_serv.c -o mini_serv
|
||||||
|
|
||||||
./mini_serv "$port" &
|
./mini_serv "$port" &
|
||||||
nc localhost "$port" &
|
nc localhost "$port" &
|
||||||
|
|
||||||
for i in {1..30}
|
for i in {1..3000}
|
||||||
do
|
do
|
||||||
nc localhost "$port" &
|
nc localhost "$port" &
|
||||||
kill -KILL $(pidof nc | tr ' ' '\n' | head -n1)
|
kill -KILL $(pidof nc | tr ' ' '\n' | head -n1)
|
||||||
|
|||||||
Reference in New Issue
Block a user