maxfd as in exam

This commit is contained in:
asus
2023-02-21 14:29:13 +01:00
parent 1d690b43bd
commit 2d93332cf9
5 changed files with 178 additions and 12134 deletions

View File

@@ -35,6 +35,7 @@ int main(int ac, char **av) {
int id;
int ret;
int port;
int tmp_max;
int max_fd;
char buf[BUFSIZE];
char msg[BUFSIZE];
@@ -70,12 +71,13 @@ int main(int ac, char **av) {
FD_ZERO(&fdset);
FD_SET(server_fd, &fdset);
max_fd = server_fd;
tmp_max = server_fd;
id = 0;
while(1) {
rdset = fdset;
wdset = fdset;
max_fd = tmp_max;
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);
FD_SET(client_fd, &fdset);
if (client_fd > max_fd)
max_fd = client_fd;
tmp_max = client_fd;
clients[client_fd].id = id;
bzero(clients[client_fd].msg, MSGSIZE);
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);
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;
}
}
//if (client_fd == max_fd) {
// for(; max_fd > 2; --max_fd) {
// if (FD_ISSET(max_fd, &fdset))
// break;
// }
//}
}
else if (ret > 0) {
for(int i = 0, j = strlen(clients[client_fd].msg); i < ret; ++i, ++j) {