-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
33 lines (26 loc) · 817 Bytes
/
utils.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
#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int setup_udp_server(int port) {
int sockfd;
struct sockaddr_in localaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("socket ");
fprintf(stderr, "socket creation failed\n");
return 0;
}
bzero((char *) &localaddr, sizeof(localaddr));
localaddr.sin_family = AF_INET;
localaddr.sin_addr.s_addr = htonl(INADDR_ANY);
localaddr.sin_port = htons((unsigned short)port);
if (bind(sockfd, (struct sockaddr *) &localaddr, sizeof(localaddr)) < 0) {
perror("bind ");
fprintf(stderr, "error binding to local addr on port %d\n", port);
return 0;
}
return sockfd;
}