maxfd as in exam
This commit is contained in:
18
mini_serv.c
18
mini_serv.c
@@ -35,6 +35,7 @@ int main(int ac, char **av) {
|
|||||||
int id;
|
int id;
|
||||||
int ret;
|
int ret;
|
||||||
int port;
|
int port;
|
||||||
|
int tmp_max;
|
||||||
int max_fd;
|
int max_fd;
|
||||||
char buf[BUFSIZE];
|
char buf[BUFSIZE];
|
||||||
char msg[BUFSIZE];
|
char msg[BUFSIZE];
|
||||||
@@ -70,12 +71,13 @@ int main(int ac, char **av) {
|
|||||||
|
|
||||||
FD_ZERO(&fdset);
|
FD_ZERO(&fdset);
|
||||||
FD_SET(server_fd, &fdset);
|
FD_SET(server_fd, &fdset);
|
||||||
max_fd = server_fd;
|
tmp_max = server_fd;
|
||||||
id = 0;
|
id = 0;
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
rdset = fdset;
|
rdset = fdset;
|
||||||
wdset = fdset;
|
wdset = fdset;
|
||||||
|
max_fd = tmp_max;
|
||||||
|
|
||||||
select(max_fd + 1, &rdset, &wdset, NULL, NULL);
|
select(max_fd + 1, &rdset, &wdset, NULL, NULL);
|
||||||
|
|
||||||
@@ -83,7 +85,7 @@ int main(int ac, char **av) {
|
|||||||
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);
|
||||||
if (client_fd > max_fd)
|
if (client_fd > max_fd)
|
||||||
max_fd = client_fd;
|
tmp_max = client_fd;
|
||||||
clients[client_fd].id = id;
|
clients[client_fd].id = id;
|
||||||
bzero(clients[client_fd].msg, MSGSIZE);
|
bzero(clients[client_fd].msg, MSGSIZE);
|
||||||
sprintf(msg, "server: client %d just arrived\n", id);
|
sprintf(msg, "server: client %d just arrived\n", id);
|
||||||
@@ -100,12 +102,12 @@ int main(int ac, char **av) {
|
|||||||
broadcast(msg, &wdset, max_fd, client_fd);
|
broadcast(msg, &wdset, max_fd, client_fd);
|
||||||
FD_CLR(client_fd, &fdset);
|
FD_CLR(client_fd, &fdset);
|
||||||
close(client_fd);
|
close(client_fd);
|
||||||
if (client_fd == max_fd) {
|
//if (client_fd == max_fd) {
|
||||||
for(; max_fd > 2; --max_fd) {
|
// for(; max_fd > 2; --max_fd) {
|
||||||
if (FD_ISSET(max_fd, &fdset))
|
// if (FD_ISSET(max_fd, &fdset))
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
else if (ret > 0) {
|
else if (ret > 0) {
|
||||||
for(int i = 0, j = strlen(clients[client_fd].msg); i < ret; ++i, ++j) {
|
for(int i = 0, j = strlen(clients[client_fd].msg); i < ret; ++i, ++j) {
|
||||||
|
|||||||
126
mini_serv_test.c
126
mini_serv_test.c
@@ -1,126 +0,0 @@
|
|||||||
#include <errno.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#define MSGSIZE 1024
|
|
||||||
#define BUFSIZE 120000
|
|
||||||
|
|
||||||
typedef struct s_client {
|
|
||||||
int id;
|
|
||||||
char msg[MSGSIZE];
|
|
||||||
} t_client;
|
|
||||||
|
|
||||||
void error(char *msg) {
|
|
||||||
write(2, msg, strlen(msg));
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void broadcast(char *msg, fd_set *set, int max_fd, int client_fd) {
|
|
||||||
for(int fd = 0; fd < max_fd; ++fd) {
|
|
||||||
if (fd == client_fd)
|
|
||||||
continue;
|
|
||||||
if (FD_ISSET(fd, set))
|
|
||||||
send(fd, msg, strlen(msg), 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int ac, char **av) {
|
|
||||||
int server_fd;
|
|
||||||
int client_fd;
|
|
||||||
int id;
|
|
||||||
int ret;
|
|
||||||
int port;
|
|
||||||
int max_fd;
|
|
||||||
struct sockaddr_in addr;
|
|
||||||
socklen_t addr_len;
|
|
||||||
fd_set fdset;
|
|
||||||
fd_set rdset;
|
|
||||||
fd_set wdset;
|
|
||||||
char buf[BUFSIZE];
|
|
||||||
char msg[BUFSIZE];
|
|
||||||
t_client clients[FD_SETSIZE];
|
|
||||||
|
|
||||||
if (ac != 2)
|
|
||||||
error("Wrong number of arguments\n");
|
|
||||||
if ( (port = atoi(av[1])) == -1)
|
|
||||||
error("Fatal error\n");
|
|
||||||
addr_len = sizeof(addr);
|
|
||||||
bzero(&addr, addr_len);
|
|
||||||
|
|
||||||
// socket create and verification
|
|
||||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (server_fd == -1)
|
|
||||||
error("Fatal error\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 error\n");
|
|
||||||
if (listen(server_fd, 10) == -1)
|
|
||||||
error("Fatal error\n");
|
|
||||||
|
|
||||||
FD_ZERO(&fdset);
|
|
||||||
FD_SET(server_fd, &fdset);
|
|
||||||
id = 0;
|
|
||||||
max_fd = server_fd;
|
|
||||||
|
|
||||||
while(1) {
|
|
||||||
rdset = fdset;
|
|
||||||
wdset = fdset;
|
|
||||||
|
|
||||||
select(max_fd + 1, &rdset, &wdset, 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;
|
|
||||||
clients[client_fd].id = id;
|
|
||||||
bzero(clients[client_fd].msg, MSGSIZE);
|
|
||||||
sprintf(msg, "server: client %d just arrived\n", id);
|
|
||||||
broadcast(msg, &wdset, max_fd, client_fd);
|
|
||||||
id++;
|
|
||||||
}
|
|
||||||
|
|
||||||
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, &wdset, max_fd, client_fd);
|
|
||||||
FD_CLR(client_fd, &fdset);
|
|
||||||
close(client_fd);
|
|
||||||
}
|
|
||||||
else if (ret > 0) {
|
|
||||||
for(int i = 0, j = strlen(clients[client_fd].msg); i < ret; ++i, ++j) {
|
|
||||||
clients[client_fd].msg[j] = buf[i];
|
|
||||||
if (buf[i] == '\n') {
|
|
||||||
clients[client_fd].msg[j] = '\0';
|
|
||||||
sprintf(msg, "client %d: %s\n", clients[client_fd].id, clients[client_fd].msg);
|
|
||||||
broadcast(msg, &wdset, max_fd, client_fd);
|
|
||||||
bzero(clients[client_fd].msg, MSGSIZE);
|
|
||||||
j = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
client_fd++;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user