1 /*
2 * Copyright 2006, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Oliver Tappe, zooey@hirschkaefer.de
7 */
8
9
10 #include <arpa/inet.h>
11 #include <netinet/in.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/socket.h>
16
17 #define MAXLEN 65536
18
19
20 static void
udp_server(int sockFD)21 udp_server(int sockFD)
22 {
23 char buf[MAXLEN];
24 long status;
25
26 while (1) {
27 status = recvfrom(sockFD, buf, MAXLEN-1, 0, NULL, NULL);
28 if (status < 0) {
29 printf("recvfrom(): %lx (%s)\n", status, strerror(status));
30 exit(5);
31 }
32 buf[status] = 0;
33 printf("%s", buf);
34 }
35 }
36
37
38 int
main(int argc,char ** argv)39 main(int argc, char** argv)
40 {
41 long status;
42 int sockFD;
43 struct sockaddr_in localAddr;
44
45 if (argc < 2) {
46 printf("usage: %s <local-port>\n", argv[0]);
47 exit(5);
48 }
49
50 sockFD = socket(AF_INET, SOCK_DGRAM, 0);
51
52 memset(&localAddr, 0, sizeof(struct sockaddr_in));
53 localAddr.sin_family = AF_INET;
54 localAddr.sin_port = htons(atoi(argv[1]));
55 printf("binding to port %u\n", ntohs(localAddr.sin_port));
56 status = bind(sockFD, (struct sockaddr *)&localAddr, sizeof(struct sockaddr_in));
57 if (status < 0) {
58 printf("bind(): %lx (%s)\n", status, strerror(status));
59 exit(5);
60 }
61
62 udp_server(sockFD);
63 return 0;
64 }
65