-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_server.c
75 lines (60 loc) · 1.93 KB
/
udp_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8888
#define MAXLINE 1024
int sockfd;
static void signal_handler(int signum) {
if (signum == SIGINT || signum == SIGTERM) {
printf("\n\nSignal %d received, preparing to exit...\n", signum);
close(sockfd);
exit(0);
}
}
int main() {
char buffer[MAXLINE];
struct sockaddr_in servaddr, cliaddr;
socklen_t len;
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
// Create a UDP socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY; // Accept packets on any interface
servaddr.sin_port = htons(PORT); // Server port
// Bind the socket to the server address
if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("UDP Server is listening on port %d...\n", PORT);
while (1) {
// Receive a UDP packet
len = sizeof(cliaddr);
int n = recvfrom(sockfd, (char *)buffer, MAXLINE, 0, (struct sockaddr *)&cliaddr, &len);
// Print the received packet
if (n > 0) {
buffer[n] = '\0'; // Null-terminate the received data
printf("Received packet from %s:%d: %s\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port), buffer);
} else {
perror("Error receiving packet");
}
// Clear the buffer for the next packet
memset(buffer, 0, sizeof(buffer));
}
// Close the socket
close(sockfd);
return 0;
}