# HG changeset patch # User Dennis # Date 1689617513 -3600 # Node ID 052cf5cf100a51c8a810ca19af7f314f21da2428 # Parent 58952f1fb8da04fd16a6318aa1b5ba97af03284b handle server shutdown and print server time diff -r 58952f1fb8da -r 052cf5cf100a src/main.c --- a/src/main.c Mon Jul 17 18:05:20 2023 +0100 +++ b/src/main.c Mon Jul 17 19:11:53 2023 +0100 @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,13 +6,27 @@ #include #include #include +#include + + +int socketfd; + + +void sig_handler(int sig_num) { + if (sig_num == 2) { + printf("\nClosing socket\n"); + shutdown(socketfd, 2); + exit(0); + } +} int main() { - int socketfd = socket(AF_INET, SOCK_STREAM, 0); + signal(SIGINT, sig_handler); + socketfd = socket(AF_INET, SOCK_STREAM, 0); if (socketfd == -1) { - printf("Socket creation failed\n"); + perror("socket: "); exit(1); } @@ -22,25 +37,31 @@ sin.sin_port = htons(5050); if (bind(socketfd, (struct sockaddr*)&sin, sizeof(sin)) == -1) { - printf("Socket binding failed\n"); + perror("bind: "); + exit(1); } - //if (connect(socketfd, (struct sockaddr*)&sin, sizeof(sin) == -1)) { - //printf("Socket connection failed\n"); - //} - if (listen(socketfd, 10) == -1) { - printf("Listen failed\n"); + perror("listen: "); + exit(1); } - char sendBuff[50] = "Hello world\n"; + printf("Waiting for connections...\n"); + char send_buffer[100]; while (1) { int connfd = accept(socketfd, (struct sockaddr*)NULL, NULL); - send(connfd, sendBuff, sizeof(sendBuff), 0); + + if (connfd == -1) { + perror("accept: \n"); + exit(1); + } + + time_t now = time(NULL); + snprintf(send_buffer, sizeof(send_buffer), "Server time: %s\n", ctime(&now)); + send(connfd, send_buffer, sizeof(send_buffer), 0); shutdown(connfd, 2); } - return 0; }