diff options
author | Johnny Richard <johnny@johnnyrichard.com> | 2022-04-07 14:49:44 +0200 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2022-04-07 14:54:39 +0200 |
commit | 1b684971c3b591538c3ba0d3343ec76559f10203 (patch) | |
tree | 575f66a3b7543cc970eceee1ec8ed8319fd4599e /main.c | |
parent | 33e14b74df70ecbfcdb56b71a0ea7d4f1e05b1f4 (diff) |
server.c: Move accept syscall to server_listen function
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 48 |
1 files changed, 2 insertions, 46 deletions
@@ -15,59 +15,15 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -#include <errno.h> -#include <netinet/in.h> -#include <stdbool.h> -#include <stdio.h> #include <stdlib.h> -#include <string.h> -#include <sys/socket.h> -#include <unistd.h> -#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; } |