1 /* udp_o.c -- read packets from the network */
4 #include <sys/socket.h>
5 #include <netinet/in.h>
13 #define UDP_PORT 10094
19 int sock, fromlen, l, i, portno;
20 char buf[BUFSIZE], *p;
21 struct sockaddr_in from;
27 fprintf(stderr,"Usage: %s [<portnumber>]\n",argv[0]);
30 if(argc>1)portno = atoi(argv[1]);
33 sock = socket(AF_INET, SOCK_DGRAM, 0);
35 perror("opening raw socket");
39 bzero((char *)&from, sizeof from);
40 from.sin_family = AF_INET;
41 from.sin_addr.s_addr = htonl(INADDR_ANY);
42 from.sin_port = htons(portno);
43 if (bind(sock, (struct sockaddr *)&from, sizeof from) < 0){
44 perror("binding UDP socket");
50 fromlen = sizeof from;
51 l = recvfrom(sock, buf, BUFSIZE, 0,
52 (struct sockaddr *)&from, &fromlen);
58 fprintf(stdout,"---------->recv %d bytes from port %d host %s\n", l,
60 (char *)inet_ntoa(from.sin_addr));
62 fprintf(stdout,"{%d.%d.%d.%d->%d.%d.%d.%d}\n",buf[12],buf[13],
63 buf[14],buf[15],buf[16],buf[17],buf[18],buf[19]);
65 for(p=buf+40,i=0;i<(l-40);i++,p++){
66 if(isprint(*p))putc(*p,stdout);
67 else putc('.',stdout);
73 /* all done, close the socket and exit */