From Electron Cloud
- good explanation of problems with UDP on interfaces that have multiple IP addresses and consequently why it's a good idea to bind separately to each interface's IP address
- trivial UDP server example which binds to a port, receives packets and prints them out
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)