238 lines
4.1 KiB
C
238 lines
4.1 KiB
C
#include <errno.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <netdb.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/select.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
|
|
/* what client include */
|
|
typedef struct s_client
|
|
{
|
|
int id;
|
|
int fd;
|
|
char *buf;
|
|
} t_client;
|
|
/* the space for max clients */
|
|
static t_client g_clients[FD_SETSIZE];
|
|
/* current max fds */
|
|
static int g_maxfds;
|
|
/* current client ids */
|
|
static int g_nexid;
|
|
/* the read and write sets */
|
|
fd_set readfds, writefds;
|
|
/* global server id */
|
|
int g_server;
|
|
|
|
int extract_message(char **buf, char **msg)
|
|
{
|
|
char *newbuf;
|
|
int i;
|
|
|
|
*msg = 0;
|
|
if (*buf == 0)
|
|
return (0);
|
|
i = 0;
|
|
while ((*buf)[i])
|
|
{
|
|
if ((*buf)[i] == '\n')
|
|
{
|
|
newbuf = calloc(1, sizeof(*newbuf) * (strlen(*buf + i + 1) + 1));
|
|
if (newbuf == 0)
|
|
return (-1);
|
|
strcpy(newbuf, *buf + i + 1);
|
|
*msg = *buf;
|
|
(*msg)[i + 1] = 0;
|
|
*buf = newbuf;
|
|
return (1);
|
|
}
|
|
i++;
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
char *str_join(char *buf, char *add)
|
|
{
|
|
char *newbuf;
|
|
int len;
|
|
|
|
if (buf == 0)
|
|
len = 0;
|
|
else
|
|
len = strlen(buf);
|
|
newbuf = malloc(sizeof(*newbuf) * (len + strlen(add) + 1));
|
|
if (newbuf == 0)
|
|
return (0);
|
|
newbuf[0] = 0;
|
|
if (buf != 0)
|
|
strcat(newbuf, buf);
|
|
free(buf);
|
|
strcat(newbuf, add);
|
|
return (newbuf);
|
|
}
|
|
|
|
void bc(int id, char *msg)
|
|
{
|
|
int i;
|
|
i = 0;
|
|
while (i <= g_maxfds)
|
|
{
|
|
if(i != id && i != g_server && g_clients[i].fd != -1 && FD_ISSET(i, &writefds))
|
|
{
|
|
send(i, msg, strlen(msg), 0);
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
void fatal(void)
|
|
{
|
|
write(2, "fatal error\n", 12);
|
|
exit(1);
|
|
}
|
|
|
|
|
|
void accept_connection(void)
|
|
{
|
|
char tmp[64];
|
|
int fd;
|
|
|
|
fd = accept(g_server, NULL, NULL);
|
|
if(fd < 0)
|
|
return ;
|
|
g_clients[fd].fd = fd;
|
|
g_clients[fd].id = g_nexid++;
|
|
g_clients[fd].buf = NULL;
|
|
sprintf(tmp, "server: client %d just arrived\n", g_clients[fd].id);
|
|
if(fd > g_maxfds)
|
|
g_maxfds++;
|
|
bc(fd, tmp);
|
|
};
|
|
|
|
|
|
void disconnect(int fd)
|
|
{
|
|
char tmp[64];
|
|
int id;
|
|
|
|
id = g_clients[fd].id;
|
|
g_clients[fd].id = -1;
|
|
g_clients[fd].fd = -1;
|
|
free(g_clients[fd].buf);
|
|
g_clients[fd].buf = NULL;
|
|
sprintf(tmp, "server: client %d just left\n", id);
|
|
bc(fd, tmp);
|
|
close(fd);
|
|
if(g_maxfds > g_server && g_clients[fd].fd == -1)
|
|
g_maxfds--;
|
|
}
|
|
|
|
|
|
|
|
void handle_connection(int fd)
|
|
{
|
|
char buff[1046];
|
|
int size;
|
|
char *msg;
|
|
char *out;
|
|
|
|
size = recv(fd, buff, sizeof(buff) - 1, 0);
|
|
if (size <= 0)
|
|
{
|
|
disconnect(fd);
|
|
return ;
|
|
}
|
|
buff[size] = 0;
|
|
g_clients[fd].buf = str_join(g_clients[fd].buf, buff);
|
|
if(!g_clients[fd].buf)
|
|
fatal();
|
|
while(extract_message(&g_clients[fd].buf, &msg))
|
|
{
|
|
out = calloc(sizeof(char) ,strlen(msg) + 64);
|
|
if(!out)
|
|
{
|
|
free(msg);
|
|
fatal();
|
|
}
|
|
sprintf(out, "client %d: %s", g_clients[fd].id, msg);
|
|
bc(fd, out);
|
|
free(out);
|
|
free(msg);
|
|
}
|
|
}
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
//int sockfd, connfd, len;
|
|
struct sockaddr_in servaddr;
|
|
int i; // counter
|
|
|
|
|
|
/* checking process for the arg */
|
|
if(argc != 2)
|
|
fatal();
|
|
// socket create and verification
|
|
g_server = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (g_server == -1)
|
|
fatal();
|
|
bzero(&servaddr, sizeof(servaddr));
|
|
|
|
// assign IP, PORT
|
|
servaddr.sin_family = AF_INET;
|
|
servaddr.sin_addr.s_addr = htonl(2130706433);
|
|
servaddr.sin_port = htons(atoi(argv[1]));
|
|
|
|
// Binding newly created socket to given IP and verification
|
|
if ((bind(g_server, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0)
|
|
fatal();
|
|
if (listen(g_server, 10) != 0)
|
|
fatal();
|
|
/* loop and init the t_client to all zeros */
|
|
i = 0;
|
|
while (i < FD_SETSIZE)
|
|
{
|
|
g_clients[i].id = -1;
|
|
g_clients[i].fd = -1;
|
|
g_clients[i].buf = NULL;
|
|
i++;
|
|
}
|
|
g_maxfds = g_server;
|
|
/* Loop thro all the clients */
|
|
while(42)
|
|
{
|
|
FD_ZERO(&readfds);
|
|
FD_ZERO(&writefds);
|
|
FD_SET(g_server, &readfds);
|
|
FD_SET(g_server, &writefds);
|
|
i = 0;
|
|
while(i <= g_maxfds)
|
|
{
|
|
if(i != g_server && g_clients[i].fd != -1)
|
|
{
|
|
FD_SET(i, &readfds);
|
|
FD_SET(i, &writefds);
|
|
}
|
|
i++;
|
|
};
|
|
if(select(g_maxfds + 1, &readfds, &writefds, NULL, NULL) < 0)
|
|
continue;
|
|
if(FD_ISSET(g_server, &readfds))
|
|
accept_connection();
|
|
i = 0;
|
|
while(i <= g_maxfds)
|
|
{
|
|
if(i != g_server && g_clients[i].id != -1 && FD_ISSET(i, &readfds))
|
|
{
|
|
handle_connection(i);
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
return (0);
|
|
|
|
}
|