wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <unistd.h> |
| 3 | #include <sys/socket.h> |
Wolfgang Denk | ecf3fb2 | 2009-04-04 16:14:51 +0200 | [diff] [blame] | 4 | #include <netinet/in.h> |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 5 | |
| 6 | int main (int argc, char *argv[]) |
| 7 | { |
| 8 | int s, len, o, port = 6666; |
| 9 | char buf[512]; |
| 10 | struct sockaddr_in addr; |
Mike Frysinger | fadad15 | 2009-01-09 04:38:17 -0500 | [diff] [blame] | 11 | socklen_t addr_len = sizeof addr; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 12 | |
| 13 | if (argc > 1) |
| 14 | port = atoi (argv[1]); |
| 15 | |
| 16 | s = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 17 | |
| 18 | o = 1; |
| 19 | len = 4; |
| 20 | setsockopt (3, SOL_SOCKET, SO_REUSEADDR, &o, len); |
| 21 | |
| 22 | addr.sin_family = AF_INET; |
| 23 | addr.sin_port = htons (port); |
| 24 | addr.sin_addr.s_addr = INADDR_ANY; /* receive broadcasts */ |
| 25 | |
| 26 | bind (s, (struct sockaddr *) &addr, sizeof addr); |
| 27 | |
| 28 | for (;;) { |
| 29 | len = recvfrom (s, buf, sizeof buf, 0, (struct sockaddr *) &addr, &addr_len); |
| 30 | if (len < 0) |
| 31 | break; |
| 32 | write (1, buf, len); |
| 33 | } |
| 34 | |
| 35 | return 0; |
| 36 | } |