57 lines
1.2 KiB
C
57 lines
1.2 KiB
C
#include "server_includes/sockets.c"
|
|
#include <pthread.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
|
|
#define ADDR "::"
|
|
|
|
int sd;
|
|
|
|
int run_server(const char **port_str) {
|
|
if (open_socket(ADDR, *port_str, &sd) != 0) {
|
|
return 1;
|
|
}
|
|
|
|
if (accept_loop(sd) != 0) {
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void sigint_handler(int sig) {
|
|
fprintf(stderr, "Gracefully shutting down...\n");
|
|
|
|
if (shutdown(sd, 2) != 0) {
|
|
perror("warning: global shutdown connection failed");
|
|
}
|
|
|
|
if (close(sd) != 0) {
|
|
perror("warning: global close connection failed");
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc < 2) {
|
|
fprintf(stderr, "Usage: %s PORT\n", argv[0]);
|
|
return 1;
|
|
}
|
|
char *port_str = argv[1];
|
|
|
|
pthread_t thread_id;
|
|
pthread_create(&thread_id, NULL, (void *)&run_server, &port_str);
|
|
|
|
fprintf(stderr, "Alive on %s:%s\n", ADDR, port_str);
|
|
|
|
signal(SIGINT, &sigint_handler);
|
|
|
|
int socket_res, status;
|
|
if ((status = pthread_join(thread_id, (void *)&socket_res)) != 0) {
|
|
fprintf(stderr, "join worker thread: %s\n", strerror(status));
|
|
return 1;
|
|
}
|
|
|
|
teardown_frames();
|
|
|
|
return socket_res;
|
|
}
|