-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
UDP-server.c
36 lines (30 loc) · 1.1 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
#define _GNU_SOURCE // NI_MAXHOST, NI_MAXSERV
#define _DARWIN_C_SOURCE // NI_MAXHOST, NI_MAXSERV
#include "UDP-lib.h"
#include <assert.h>
int
main(int argc, char *argv[])
{
struct sockaddr_storage peer_addr;
socklen_t peer_addr_len;
ssize_t nread; // signed size
char buf[BUFFER_SIZE];
int sfd = UDP_Open(NULL, "10000", 1), s;
assert(sfd > -1);
/* Read datagrams and echo them back to sender */
for (;;) {
peer_addr_len = sizeof(struct sockaddr_storage);
nread = UDP_Read(sfd, buf, (struct sockaddr *) &peer_addr, &peer_addr_len);
if (nread == -1)
continue; /* Ignore failed request */
char host[NI_MAXHOST], service[NI_MAXSERV];
s = getnameinfo((struct sockaddr *) &peer_addr,
peer_addr_len, host, NI_MAXHOST,
service, NI_MAXSERV, NI_NUMERICSERV);
if (s == 0)
printf("Received %zd bytes from %s:%s: %s\n",
nread, host, service, buf);
else
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
}
}