core: add all the required files
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
/* ============================================================================
|
||||
* mini_serv.c — the simplest version that passes the exam
|
||||
*
|
||||
* Build: cc -Wall -Wextra -Werror mini_serv.c -o mini_serv
|
||||
* Run: ./mini_serv <port>
|
||||
*
|
||||
* Shape: two arrays indexed by fd:
|
||||
* g_id[fd] -1 if the slot is free, otherwise that client's public id
|
||||
* g_buf[fd] pending bytes for a line that is not finished yet
|
||||
* The fd_set is rebuilt from g_id[] before every select(). That's it.
|
||||
*
|
||||
* The two helpers (str_join, extract_message) are GIVEN in the exam's main.c.
|
||||
* ==========================================================================*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
/* ---- the whole server state (all indexed by fd) ------------------------- */
|
||||
static int g_id[FD_SETSIZE]; /* -1 = free slot, else client id */
|
||||
static char *g_buf[FD_SETSIZE]; /* unfinished line for that client */
|
||||
static int g_server; /* the listening fd */
|
||||
static int g_maxfd; /* highest fd in use */
|
||||
static int g_nextid; /* next client id: 0, 1, 2, ... */
|
||||
|
||||
static void fatal(void)
|
||||
{
|
||||
write(2, "Fatal error\n", 12);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* ---- GIVEN in the exam's main.c: copy these two verbatim ---------------- */
|
||||
|
||||
static 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);
|
||||
}
|
||||
|
||||
static 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);
|
||||
}
|
||||
|
||||
/* ---- server helpers ----------------------------------------------------- */
|
||||
|
||||
/* Send s to every connected client except skip_fd (skip_fd = -1: everyone). */
|
||||
static void send_all(int skip_fd, const char *s)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = 0;
|
||||
while (fd <= g_maxfd)
|
||||
{
|
||||
if (fd != skip_fd && g_id[fd] != -1)
|
||||
send(fd, s, strlen(s), 0);
|
||||
fd++;
|
||||
}
|
||||
}
|
||||
|
||||
/* EOF/error on fd: announce, close, drop the slot and its buffer. */
|
||||
static void on_leave(int fd)
|
||||
{
|
||||
char msg[64];
|
||||
int id;
|
||||
|
||||
id = g_id[fd];
|
||||
close(fd);
|
||||
g_id[fd] = -1;
|
||||
free(g_buf[fd]);
|
||||
g_buf[fd] = NULL;
|
||||
sprintf(msg, "server: client %d just left\n", id);
|
||||
send_all(-1, msg);
|
||||
while (g_maxfd > g_server && g_id[g_maxfd] == -1)
|
||||
g_maxfd--;
|
||||
}
|
||||
|
||||
/* Data on fd: append it, then relay every COMPLETE line to the others. */
|
||||
static void on_read(int fd)
|
||||
{
|
||||
char chunk[4096];
|
||||
char *line;
|
||||
char *out;
|
||||
ssize_t n;
|
||||
|
||||
n = recv(fd, chunk, sizeof(chunk) - 1, 0);
|
||||
if (n <= 0)
|
||||
{
|
||||
on_leave(fd);
|
||||
return ;
|
||||
}
|
||||
chunk[n] = 0;
|
||||
g_buf[fd] = str_join(g_buf[fd], chunk);
|
||||
if (!g_buf[fd])
|
||||
fatal();
|
||||
while (extract_message(&g_buf[fd], &line) == 1)
|
||||
{
|
||||
out = malloc(strlen(line) + 64);
|
||||
if (!out)
|
||||
{
|
||||
free(line);
|
||||
fatal();
|
||||
}
|
||||
sprintf(out, "client %d: %s", g_id[fd], line);
|
||||
send_all(fd, out);
|
||||
free(out);
|
||||
free(line);
|
||||
}
|
||||
}
|
||||
|
||||
/* New connection: give it an id, a slot, and announce it to the others. */
|
||||
static void on_accept(void)
|
||||
{
|
||||
char msg[64];
|
||||
int fd;
|
||||
|
||||
fd = accept(g_server, NULL, NULL);
|
||||
if (fd < 0)
|
||||
return ;
|
||||
if (fd >= FD_SETSIZE)
|
||||
{
|
||||
close(fd);
|
||||
return ;
|
||||
}
|
||||
g_id[fd] = g_nextid++;
|
||||
g_buf[fd] = NULL;
|
||||
if (fd > g_maxfd)
|
||||
g_maxfd = fd;
|
||||
sprintf(msg, "server: client %d just arrived\n", g_id[fd]);
|
||||
send_all(fd, msg);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
fd_set readfds;
|
||||
int i;
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
write(2, "Wrong number of arguments\n", 26);
|
||||
return (1);
|
||||
}
|
||||
g_server = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (g_server < 0)
|
||||
fatal();
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(2130706433); /* 127.0.0.1 */
|
||||
addr.sin_port = htons(atoi(argv[1]));
|
||||
if (bind(g_server, (struct sockaddr *)&addr, sizeof(addr)) < 0)
|
||||
fatal();
|
||||
if (listen(g_server, 10) < 0)
|
||||
fatal();
|
||||
i = 0;
|
||||
while (i < FD_SETSIZE)
|
||||
{
|
||||
g_id[i] = -1;
|
||||
i++;
|
||||
}
|
||||
g_maxfd = g_server;
|
||||
while (1)
|
||||
{
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(g_server, &readfds);
|
||||
i = 0;
|
||||
while (i <= g_maxfd)
|
||||
{
|
||||
if (g_id[i] != -1)
|
||||
FD_SET(i, &readfds);
|
||||
i++;
|
||||
}
|
||||
if (select(g_maxfd + 1, &readfds, NULL, NULL, NULL) < 0)
|
||||
continue ;
|
||||
if (FD_ISSET(g_server, &readfds))
|
||||
on_accept();
|
||||
i = 0;
|
||||
while (i <= g_maxfd)
|
||||
{
|
||||
if (i != g_server && g_id[i] != -1 && FD_ISSET(i, &readfds))
|
||||
on_read(i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
Reference in New Issue
Block a user