wip solution for split messages
This commit is contained in:
34
mini_serv.c
34
mini_serv.c
@@ -68,19 +68,23 @@ void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd)
|
||||
}
|
||||
|
||||
int main(int ac, char **av) {
|
||||
int port;
|
||||
int server_fd;
|
||||
int client_fd;
|
||||
int max_fd;
|
||||
int client_id;
|
||||
|
||||
int port;
|
||||
int max_fd;
|
||||
int ret;
|
||||
int clients[FD_SETSIZE];
|
||||
|
||||
char buf[BUFSIZE];
|
||||
char tmpbuf[BUFSIZE];
|
||||
char msg[BUFSIZE];
|
||||
|
||||
struct sockaddr_in addr;
|
||||
socklen_t addr_len;
|
||||
fd_set fdset;
|
||||
fd_set rdset;
|
||||
int clients[FD_SETSIZE];
|
||||
int ret;
|
||||
|
||||
if (ac != 2)
|
||||
error("Wrong number of arguments\n");
|
||||
@@ -109,8 +113,8 @@ int main(int ac, char **av) {
|
||||
clients[client_fd] = client_id;
|
||||
if (client_fd > max_fd)
|
||||
max_fd = client_fd;
|
||||
sprintf(buf, "server: client %d just arrived\n", client_id);
|
||||
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
||||
sprintf(msg, "server: client %d just arrived\n", client_id);
|
||||
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
||||
client_id++;
|
||||
}
|
||||
|
||||
@@ -122,13 +126,23 @@ int main(int ac, char **av) {
|
||||
bzero(buf, BUFSIZE);
|
||||
ret = recv(client_fd, buf, BUFSIZE, 0);
|
||||
if (ret == 0) {
|
||||
sprintf(buf, "server: client %d just left\n", clients[client_fd]);
|
||||
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
||||
sprintf(msg, "server: client %d just left\n", clients[client_fd]);
|
||||
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
||||
FD_CLR(client_fd, &fdset);
|
||||
}
|
||||
else if (ret > 0) {
|
||||
sprintf(tmpbuf, "client %d: %s", clients[client_fd], buf);
|
||||
broadcast(tmpbuf, &fdset, max_fd, server_fd, client_fd);
|
||||
|
||||
for(int i = 0, j = 0; buf[i] != '\n'; ++i, ++j) {
|
||||
tmpbuf[j] = buf[i];
|
||||
if (buf[i] == '\n') {
|
||||
tmpbuf[j] = '\0';
|
||||
sprintf(msg, "client %d: %s\n", clients[client_fd], tmpbuf);
|
||||
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
||||
bzero(tmpbuf, strlen(tmpbuf));
|
||||
j = -1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
client_fd++;
|
||||
|
||||
Reference in New Issue
Block a user