Make server continuous

master
Nick Krichevsky 2018-09-09 23:34:29 -04:00
parent 6ba3ee9756
commit 984eb0b81b
1 changed files with 21 additions and 1 deletions

View File

@ -2,12 +2,20 @@
#include "../common/socket_helper.h"
#include "socket_server.h"
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <unistd.h>
bool running = true;
void handle_signal(int signal) {
running = false;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("%s", USAGE_STRING);
@ -16,11 +24,23 @@ int main(int argc, char* argv[]) {
long port_num = strtol(port, NULL, 10);
if (port_num < MIN_PORT || port_num > MAX_PORT || errno == ERANGE || errno == EINVAL) {
printf("%s", PORT_ERROR);
return 1;
}
struct sigaction sa = {.sa_handler = &handle_signal, .sa_flags = 0};
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
struct server_info info = setup(port_num);
if (info.status == STATUS_ERROR) {
printf("%s\n", strerror(errno));
return 1;
}
while (running) {
enum socket_result serve_result = serve_one_request(info.sock_fd);
if (serve_result != RESULT_OK && errno != EINTR) {
printf("Got error code %d when serving a request.\n", serve_result);
}
}
serve_one_request(info.sock_fd);
close(info.sock_fd);
}