127 lines
2.9 KiB
C
127 lines
2.9 KiB
C
#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);
|
|
}
|
|
|