Mercurial > public > hey
comparison src/main.c @ 4:691358f944e8
remove hardcoded host info
author | Dennis <dennisconcepcionmartin@gmail.com> |
---|---|
date | Tue, 18 Jul 2023 12:04:37 +0100 |
parents | 052cf5cf100a |
children | 45bac89a4da3 |
comparison
equal
deleted
inserted
replaced
3:e96eaa6b74c1 | 4:691358f944e8 |
---|---|
1 #include <signal.h> | 1 #include <signal.h> |
2 #include <stdio.h> | 2 #include <stdio.h> |
3 #include <stdlib.h> | 3 #include <stdlib.h> |
4 #include <unistd.h> | |
4 #include <string.h> | 5 #include <string.h> |
5 #include <sys/types.h> | 6 #include <sys/types.h> |
6 #include <sys/socket.h> | 7 #include <sys/socket.h> |
8 #include <netdb.h> | |
7 #include <netinet/in.h> | 9 #include <netinet/in.h> |
8 #include <arpa/inet.h> | 10 #include <arpa/inet.h> |
9 #include <time.h> | 11 #include <time.h> |
10 | 12 |
11 | 13 |
13 | 15 |
14 | 16 |
15 void sig_handler(int sig_num) { | 17 void sig_handler(int sig_num) { |
16 if (sig_num == 2) { | 18 if (sig_num == 2) { |
17 printf("\nClosing socket\n"); | 19 printf("\nClosing socket\n"); |
18 shutdown(socketfd, 2); | 20 close(socketfd); |
19 exit(0); | 21 exit(0); |
20 } | 22 } |
21 } | 23 } |
22 | 24 |
23 | 25 |
24 int main() { | 26 int main() { |
27 | |
25 signal(SIGINT, sig_handler); | 28 signal(SIGINT, sig_handler); |
26 socketfd = socket(AF_INET, SOCK_STREAM, 0); | 29 |
30 struct addrinfo hints; | |
31 struct addrinfo *servinfo; | |
32 | |
33 memset(&hints, 0, sizeof hints); // Make sure the struct is empty | |
34 hints.ai_family = AF_UNSPEC; // Don't care IPv4 or IPv6 | |
35 hints.ai_socktype = SOCK_STREAM; // TCP stream sockets | |
36 hints.ai_flags = AI_PASSIVE; // Fill in my IP for me | |
37 | |
38 if ((getaddrinfo(NULL, "5050", &hints, &servinfo)) != 0) { | |
39 perror("getaddrinfo: "); | |
40 exit(1); | |
41 } | |
42 | |
43 socketfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol); | |
27 | 44 |
28 if (socketfd == -1) { | 45 if (socketfd == -1) { |
29 perror("socket: "); | 46 perror("socket: "); |
30 exit(1); | 47 exit(1); |
31 } | 48 } |
32 | 49 |
33 struct sockaddr_in sin; | 50 if (bind(socketfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) { |
34 memset(&sin, 0, sizeof(sin)); | |
35 sin.sin_family = AF_INET; | |
36 sin.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
37 sin.sin_port = htons(5050); | |
38 | |
39 if (bind(socketfd, (struct sockaddr*)&sin, sizeof(sin)) == -1) { | |
40 perror("bind: "); | 51 perror("bind: "); |
41 exit(1); | 52 exit(1); |
42 } | 53 } |
54 | |
55 freeaddrinfo(servinfo); | |
43 | 56 |
44 if (listen(socketfd, 10) == -1) { | 57 if (listen(socketfd, 10) == -1) { |
45 perror("listen: "); | 58 perror("listen: "); |
46 exit(1); | 59 exit(1); |
47 } | 60 } |
48 | 61 |
62 char send_buffer[100]; | |
63 struct sockaddr_storage clientinfo; | |
64 socklen_t clientinfo_size; | |
65 | |
49 printf("Waiting for connections...\n"); | 66 printf("Waiting for connections...\n"); |
50 char send_buffer[100]; | |
51 | 67 |
52 while (1) { | 68 while (1) { |
53 int connfd = accept(socketfd, (struct sockaddr*)NULL, NULL); | 69 int connfd = accept(socketfd, (struct sockaddr *)&clientinfo, &clientinfo_size); |
54 | 70 |
55 if (connfd == -1) { | 71 if (connfd == -1) { |
56 perror("accept: \n"); | 72 perror("accept: \n"); |
57 exit(1); | 73 exit(1); |
58 } | 74 } |
59 | 75 |
60 time_t now = time(NULL); | 76 time_t now = time(NULL); |
61 snprintf(send_buffer, sizeof(send_buffer), "Server time: %s\n", ctime(&now)); | 77 snprintf(send_buffer, sizeof(send_buffer), "Server time: %s\n", ctime(&now)); |
62 send(connfd, send_buffer, sizeof(send_buffer), 0); | 78 send(connfd, send_buffer, sizeof(send_buffer), 0); |
63 shutdown(connfd, 2); | 79 close(connfd); |
64 } | 80 } |
65 | 81 |
66 return 0; | 82 return 0; |
67 } | 83 } |