From 1b684971c3b591538c3ba0d3343ec76559f10203 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Thu, 7 Apr 2022 14:49:44 +0200 Subject: server.c: Move accept syscall to server_listen function --- main.c | 48 ++---------------------------------------------- 1 file changed, 2 insertions(+), 46 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 5db8d59..c3f7ae2 100644 --- a/main.c +++ b/main.c @@ -15,59 +15,15 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include -#include -#include -#include #include -#include -#include -#include -#include "./server.h" +#include "server.h" #define SERVER_PORT 6667 -#define BUFFER_SIZE 1024 -#define EXIT_COMMAND "exit" int main() { server_t server = server_create(SERVER_PORT); + server_listen(&server); - struct sockaddr_in client; - socklen_t client_len = sizeof(client); - - int client_fd = accept(server.fd, (struct sockaddr *) &client, &client_len); - if (client_fd == -1) { - perror("[ERROR] could not accept connection"); - close(server.fd); - return EXIT_FAILURE; - } - - bool running = true; - - while (running) { - char client_buf[BUFFER_SIZE]; - memset(client_buf, 0, BUFFER_SIZE); - - if (recv(client_fd, client_buf, BUFFER_SIZE, 0) == -1) { - perror("[ERROR] could not read data from client"); - continue; - } - - if (!strncasecmp(client_buf, EXIT_COMMAND, strlen(EXIT_COMMAND) - 1)) { - puts("[INFO] exiting program. bye bye!"); - close(client_fd); - close(server.fd); - exit(EXIT_SUCCESS); - } - - if (send(client_fd, client_buf, BUFFER_SIZE, 0) == -1) { - perror("[ERROR] could not send data to client"); - continue; - } - } - - close(client_fd); - close(server.fd); return EXIT_SUCCESS; } -- cgit v1.2.3