「🔨」 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 <unistd.h>
|
||||
|
||||
#define BUFSIZE 200000
|
||||
|
||||
typedef struct {
|
||||
int id;
|
||||
char *msg;
|
||||
} t_client;
|
||||
} client_t;
|
||||
|
||||
t_client clients[FD_SETSIZE];
|
||||
client_t clients[FD_SETSIZE];
|
||||
|
||||
char recv_buf[3000];
|
||||
char send_buf[3000];
|
||||
char recv_buf[BUFSIZE];
|
||||
char send_buf[BUFSIZE];
|
||||
|
||||
int maxfd;
|
||||
int lastid = 0;
|
||||
int sockfd;
|
||||
int maxfd = 0;
|
||||
int lastid = -1;
|
||||
int sockfd = -1;
|
||||
|
||||
fd_set main_fd, read_fd;
|
||||
|
||||
void send_all();
|
||||
fd_set main_fd, read_fd, write_fd;
|
||||
|
||||
int extract_message(char **buf, char **msg) {
|
||||
char *newbuf;
|
||||
@ -68,18 +68,24 @@ char *str_join(char *buf, char *add) {
|
||||
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) {
|
||||
lastid++;
|
||||
clients[cli_fd].id = lastid;
|
||||
clients[cli_fd].msg = NULL;
|
||||
FD_SET(cli_fd, &main_fd);
|
||||
bzero(send_buf, 3000);
|
||||
bzero(send_buf, BUFSIZE);
|
||||
sprintf(send_buf, "server: client %d just arrived\n", lastid);
|
||||
send_all(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);
|
||||
send_all(cli_fd);
|
||||
clients[cli_fd].id = -1;
|
||||
@ -92,30 +98,29 @@ void rm_client(int cli_fd) {
|
||||
}
|
||||
|
||||
int read_client(int cli_fd) {
|
||||
char tmp[3000];
|
||||
int bytes = recv(cli_fd, tmp, 3000, 0);
|
||||
bzero(recv_buf, BUFSIZE);
|
||||
int bytes = recv(cli_fd, recv_buf, BUFSIZE, 0);
|
||||
|
||||
if (bytes <= 0)
|
||||
return (rm_client(cli_fd), 0);
|
||||
|
||||
tmp[bytes] = '\0';
|
||||
clients[cli_fd].msg = str_join(clients[cli_fd].msg, tmp);
|
||||
if (bytes <= 0) {
|
||||
if (strlen(clients[cli_fd].msg) > 0) {
|
||||
for (int i = 0; i < maxfd; i++)
|
||||
if (FD_ISSET(i, &main_fd) && i != sockfd && i != cli_fd)
|
||||
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;
|
||||
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);
|
||||
send_all(cli_fd);
|
||||
free(line);
|
||||
}
|
||||
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);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int send_error(void) {
|
||||
@ -151,13 +156,12 @@ int main(int ac, char **av) {
|
||||
return (send_error());
|
||||
|
||||
FD_ZERO(&main_fd);
|
||||
FD_ZERO(&read_fd);
|
||||
FD_SET(sockfd, &main_fd);
|
||||
maxfd = sockfd;
|
||||
|
||||
while (1) {
|
||||
read_fd = main_fd;
|
||||
int ret = select(maxfd + 1, &read_fd, NULL, NULL, NULL);
|
||||
read_fd = write_fd = main_fd;
|
||||
int ret = select(maxfd + 1, &read_fd, &write_fd, NULL, NULL);
|
||||
if (ret < 0)
|
||||
return (send_error());
|
||||
|
||||
|
Reference in New Issue
Block a user