Mercurial > public > hey
comparison src/main.c @ 2:052cf5cf100a
handle server shutdown and print server time
author | Dennis <dennisconcepcionmartin@gmail.com> |
---|---|
date | Mon, 17 Jul 2023 19:11:53 +0100 |
parents | 58952f1fb8da |
children | 691358f944e8 |
comparison
equal
deleted
inserted
replaced
1:58952f1fb8da | 2:052cf5cf100a |
---|---|
1 #include <signal.h> | |
1 #include <stdio.h> | 2 #include <stdio.h> |
2 #include <stdlib.h> | 3 #include <stdlib.h> |
3 #include <string.h> | 4 #include <string.h> |
4 #include <sys/types.h> | 5 #include <sys/types.h> |
5 #include <sys/socket.h> | 6 #include <sys/socket.h> |
6 #include <netinet/in.h> | 7 #include <netinet/in.h> |
7 #include <arpa/inet.h> | 8 #include <arpa/inet.h> |
9 #include <time.h> | |
10 | |
11 | |
12 int socketfd; | |
13 | |
14 | |
15 void sig_handler(int sig_num) { | |
16 if (sig_num == 2) { | |
17 printf("\nClosing socket\n"); | |
18 shutdown(socketfd, 2); | |
19 exit(0); | |
20 } | |
21 } | |
8 | 22 |
9 | 23 |
10 int main() { | 24 int main() { |
11 int socketfd = socket(AF_INET, SOCK_STREAM, 0); | 25 signal(SIGINT, sig_handler); |
26 socketfd = socket(AF_INET, SOCK_STREAM, 0); | |
12 | 27 |
13 if (socketfd == -1) { | 28 if (socketfd == -1) { |
14 printf("Socket creation failed\n"); | 29 perror("socket: "); |
15 exit(1); | 30 exit(1); |
16 } | 31 } |
17 | 32 |
18 struct sockaddr_in sin; | 33 struct sockaddr_in sin; |
19 memset(&sin, 0, sizeof(sin)); | 34 memset(&sin, 0, sizeof(sin)); |
20 sin.sin_family = AF_INET; | 35 sin.sin_family = AF_INET; |
21 sin.sin_addr.s_addr = inet_addr("127.0.0.1"); | 36 sin.sin_addr.s_addr = inet_addr("127.0.0.1"); |
22 sin.sin_port = htons(5050); | 37 sin.sin_port = htons(5050); |
23 | 38 |
24 if (bind(socketfd, (struct sockaddr*)&sin, sizeof(sin)) == -1) { | 39 if (bind(socketfd, (struct sockaddr*)&sin, sizeof(sin)) == -1) { |
25 printf("Socket binding failed\n"); | 40 perror("bind: "); |
41 exit(1); | |
26 } | 42 } |
27 | 43 |
28 //if (connect(socketfd, (struct sockaddr*)&sin, sizeof(sin) == -1)) { | |
29 //printf("Socket connection failed\n"); | |
30 //} | |
31 | |
32 if (listen(socketfd, 10) == -1) { | 44 if (listen(socketfd, 10) == -1) { |
33 printf("Listen failed\n"); | 45 perror("listen: "); |
46 exit(1); | |
34 } | 47 } |
35 | 48 |
36 char sendBuff[50] = "Hello world\n"; | 49 printf("Waiting for connections...\n"); |
50 char send_buffer[100]; | |
37 | 51 |
38 while (1) { | 52 while (1) { |
39 int connfd = accept(socketfd, (struct sockaddr*)NULL, NULL); | 53 int connfd = accept(socketfd, (struct sockaddr*)NULL, NULL); |
40 send(connfd, sendBuff, sizeof(sendBuff), 0); | 54 |
55 if (connfd == -1) { | |
56 perror("accept: \n"); | |
57 exit(1); | |
58 } | |
59 | |
60 time_t now = time(NULL); | |
61 snprintf(send_buffer, sizeof(send_buffer), "Server time: %s\n", ctime(&now)); | |
62 send(connfd, send_buffer, sizeof(send_buffer), 0); | |
41 shutdown(connfd, 2); | 63 shutdown(connfd, 2); |
42 } | 64 } |
43 | 65 |
44 | |
45 return 0; | 66 return 0; |
46 } | 67 } |