working
This commit is contained in:
45
mini_serv.c
45
mini_serv.c
@@ -31,7 +31,7 @@
|
||||
// strstr
|
||||
// write
|
||||
|
||||
#define BUFSIZE 1024
|
||||
#define BUFSIZE 42000
|
||||
|
||||
void error(char *str) {
|
||||
write(2, str, strlen(str));
|
||||
@@ -57,24 +57,13 @@ int init_socket(struct sockaddr_in *addr, int len, int port) {
|
||||
}
|
||||
|
||||
void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd) {
|
||||
write(1, "---\n", 4);
|
||||
for(int i = 0; i <= max_fd; ++i) {
|
||||
printf("[%d]\n", i);
|
||||
if (i == server_fd)
|
||||
{
|
||||
write(1, "is server\n", 10);
|
||||
continue;
|
||||
}
|
||||
if (i == sender_fd)
|
||||
{
|
||||
write(1, "is sender\n", 10);
|
||||
continue;
|
||||
}
|
||||
if (FD_ISSET(i, set))
|
||||
{
|
||||
send(i, buf, strlen(buf), 0);
|
||||
write(1, buf, strlen(buf));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +74,7 @@ int main(int ac, char **av) {
|
||||
int max_fd;
|
||||
int client_id;
|
||||
char buf[BUFSIZE];
|
||||
char tmpbuf[BUFSIZE];
|
||||
struct sockaddr_in addr;
|
||||
socklen_t addr_len;
|
||||
fd_set fdset;
|
||||
@@ -99,6 +89,7 @@ int main(int ac, char **av) {
|
||||
addr_len = sizeof(addr);
|
||||
|
||||
server_fd = init_socket(&addr, addr_len, port);
|
||||
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(server_fd, &fdset);
|
||||
max_fd = server_fd;
|
||||
@@ -107,14 +98,12 @@ int main(int ac, char **av) {
|
||||
/*
|
||||
*/
|
||||
while (1) {
|
||||
// FD_ZERO(&rdset);
|
||||
rdset = fdset;
|
||||
|
||||
select(max_fd + 1, &rdset, NULL, NULL, NULL);
|
||||
|
||||
// new connection
|
||||
if (FD_ISSET(server_fd, &rdset))
|
||||
{
|
||||
if (FD_ISSET(server_fd, &rdset)) {
|
||||
client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
|
||||
FD_SET(client_fd, &fdset);
|
||||
clients[client_fd] = client_id;
|
||||
@@ -129,25 +118,21 @@ int main(int ac, char **av) {
|
||||
client_fd = 0;
|
||||
while(client_fd <= max_fd) {
|
||||
ret = 1;
|
||||
if (FD_ISSET(client_fd, &rdset))
|
||||
if (FD_ISSET(client_fd, &rdset)) {
|
||||
bzero(buf, BUFSIZE);
|
||||
ret = recv(client_fd, buf, BUFSIZE, 0);
|
||||
if (ret == 0) {
|
||||
sprintf(buf, "server: client %d just left\n", clients[client_fd]);
|
||||
write(1, buf, strlen(buf));
|
||||
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
||||
FD_CLR(client_fd, &fdset);
|
||||
}
|
||||
else {
|
||||
sprintf(buf, "client %d: %s\n", clients[client_fd], buf);
|
||||
write(1, buf, strlen(buf));
|
||||
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
||||
if (ret == 0) {
|
||||
sprintf(buf, "server: client %d just left\n", clients[client_fd]);
|
||||
broadcast(buf, &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);
|
||||
}
|
||||
}
|
||||
client_fd++;
|
||||
}
|
||||
|
||||
|
||||
// "client %d: "
|
||||
// "server: client %d just left\n"
|
||||
}
|
||||
|
||||
return (0);
|
||||
|
||||
Reference in New Issue
Block a user