Make server continuous
parent
6ba3ee9756
commit
984eb0b81b
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue