From Electron Cloud
Revision as of 11:23, 24 February 2009 by Ecloud (Talk | contribs)

Jump to: navigation, search

See what ports are bound:

netstat -lun

Watch UDP traffic:

tcpdump -A -u port 9877 -i iface

Modified version of the UDP server linked above, which sends replies:

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#define BUFLEN 512
#define NPACK 10
 #define PORT 9930

 void diep(char *s)
 {
   perror(s);
   exit(1);
 }

 int main(void)
 {
   struct sockaddr_in si_me, si_other;
   int s, i, slen=sizeof(si_other);
   char buf[BUFLEN];

   if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
     diep("socket");

   memset((char *) &si_me, 0, sizeof(si_me));
   si_me.sin_family = AF_INET;
   si_me.sin_port = htons(PORT);
   si_me.sin_addr.s_addr = htonl(INADDR_ANY);
   if (bind(s, &si_me, sizeof(si_me))==-1)
       diep("bind");

   for (i=0; i<NPACK; i++) {
     if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
       diep("recvfrom()");
     printf("Received packet from %s:%d\nData: %s\n\n",
            inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
		char reply[256];
		int rlen = snprintf(reply, 255, "I'll see your '%s' and send you 'this'\n", buf);
		sendto(s, reply, rlen, 0, &si_other, slen);
   }

   close(s);
   return 0;
}

Chat with the "server" using socat:

socat - udp:localhost:9930

(or substitute localhost for a remote machine on which it's running)