「🔨」 fix(rm client): now sending the end of the buffer at disconnect
This commit is contained in:
68
mini_serv.c
68
mini_serv.c
@ -7,23 +7,23 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define BUFSIZE 200000
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int id;
|
int id;
|
||||||
char *msg;
|
char *msg;
|
||||||
} t_client;
|
} client_t;
|
||||||
|
|
||||||
t_client clients[FD_SETSIZE];
|
client_t clients[FD_SETSIZE];
|
||||||
|
|
||||||
char recv_buf[3000];
|
char recv_buf[BUFSIZE];
|
||||||
char send_buf[3000];
|
char send_buf[BUFSIZE];
|
||||||
|
|
||||||
int maxfd;
|
int maxfd = 0;
|
||||||
int lastid = 0;
|
int lastid = -1;
|
||||||
int sockfd;
|
int sockfd = -1;
|
||||||
|
|
||||||
fd_set main_fd, read_fd;
|
fd_set main_fd, read_fd, write_fd;
|
||||||
|
|
||||||
void send_all();
|
|
||||||
|
|
||||||
int extract_message(char **buf, char **msg) {
|
int extract_message(char **buf, char **msg) {
|
||||||
char *newbuf;
|
char *newbuf;
|
||||||
@ -68,18 +68,24 @@ char *str_join(char *buf, char *add) {
|
|||||||
return (newbuf);
|
return (newbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void send_all(int cli_fd) {
|
||||||
|
for (int i = 0; i <= maxfd; i++)
|
||||||
|
if (FD_ISSET(i, &main_fd) && i != sockfd && i != cli_fd)
|
||||||
|
send(i, send_buf, strlen(send_buf), 0);
|
||||||
|
}
|
||||||
|
|
||||||
void append_client(int cli_fd) {
|
void append_client(int cli_fd) {
|
||||||
lastid++;
|
lastid++;
|
||||||
clients[cli_fd].id = lastid;
|
clients[cli_fd].id = lastid;
|
||||||
clients[cli_fd].msg = NULL;
|
clients[cli_fd].msg = NULL;
|
||||||
FD_SET(cli_fd, &main_fd);
|
FD_SET(cli_fd, &main_fd);
|
||||||
bzero(send_buf, 3000);
|
bzero(send_buf, BUFSIZE);
|
||||||
sprintf(send_buf, "server: client %d just arrived\n", lastid);
|
sprintf(send_buf, "server: client %d just arrived\n", lastid);
|
||||||
send_all(cli_fd);
|
send_all(cli_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rm_client(int cli_fd) {
|
void rm_client(int cli_fd) {
|
||||||
FD_CLR(cli_fd, &read_fd);
|
bzero(send_buf, BUFSIZE);
|
||||||
sprintf(send_buf, "server: client %d, just left\n", clients[cli_fd].id);
|
sprintf(send_buf, "server: client %d, just left\n", clients[cli_fd].id);
|
||||||
send_all(cli_fd);
|
send_all(cli_fd);
|
||||||
clients[cli_fd].id = -1;
|
clients[cli_fd].id = -1;
|
||||||
@ -92,30 +98,29 @@ void rm_client(int cli_fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int read_client(int cli_fd) {
|
int read_client(int cli_fd) {
|
||||||
char tmp[3000];
|
bzero(recv_buf, BUFSIZE);
|
||||||
int bytes = recv(cli_fd, tmp, 3000, 0);
|
int bytes = recv(cli_fd, recv_buf, BUFSIZE, 0);
|
||||||
|
|
||||||
if (bytes <= 0)
|
if (bytes <= 0) {
|
||||||
return (rm_client(cli_fd), 0);
|
if (strlen(clients[cli_fd].msg) > 0) {
|
||||||
|
for (int i = 0; i < maxfd; i++)
|
||||||
tmp[bytes] = '\0';
|
if (FD_ISSET(i, &main_fd) && i != sockfd && i != cli_fd)
|
||||||
clients[cli_fd].msg = str_join(clients[cli_fd].msg, tmp);
|
send(i, clients[cli_fd].msg, strlen(clients[cli_fd].msg), 0);
|
||||||
|
}
|
||||||
|
rm_client(cli_fd);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
recv_buf[bytes] = 0;
|
||||||
|
clients[cli_fd].msg = str_join(clients[cli_fd].msg, recv_buf);
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
while (extract_message(&clients[cli_fd].msg, &line)) {
|
while (extract_message(&clients[cli_fd].msg, &line)) {
|
||||||
bzero(send_buf, 3000);
|
bzero(send_buf, BUFSIZE);
|
||||||
sprintf(send_buf, "client %d: %s", clients[cli_fd].id, line);
|
sprintf(send_buf, "client %d: %s", clients[cli_fd].id, line);
|
||||||
send_all(cli_fd);
|
send_all(cli_fd);
|
||||||
free(line);
|
free(line);
|
||||||
}
|
}
|
||||||
return 1;
|
return (1);
|
||||||
}
|
|
||||||
|
|
||||||
void send_all(int cli_fd) {
|
|
||||||
size_t len = strlen(send_buf);
|
|
||||||
|
|
||||||
for (int i = 0; i <= maxfd; i++)
|
|
||||||
if (FD_ISSET(i, &main_fd) != 0 && i != cli_fd && i != sockfd)
|
|
||||||
send(i, send_buf, len, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int send_error(void) {
|
int send_error(void) {
|
||||||
@ -151,13 +156,12 @@ int main(int ac, char **av) {
|
|||||||
return (send_error());
|
return (send_error());
|
||||||
|
|
||||||
FD_ZERO(&main_fd);
|
FD_ZERO(&main_fd);
|
||||||
FD_ZERO(&read_fd);
|
|
||||||
FD_SET(sockfd, &main_fd);
|
FD_SET(sockfd, &main_fd);
|
||||||
maxfd = sockfd;
|
maxfd = sockfd;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
read_fd = main_fd;
|
read_fd = write_fd = main_fd;
|
||||||
int ret = select(maxfd + 1, &read_fd, NULL, NULL, NULL);
|
int ret = select(maxfd + 1, &read_fd, &write_fd, NULL, NULL);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return (send_error());
|
return (send_error());
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user