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

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 (and compiles without warnings):

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.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, (struct sockaddr*)&si_me, sizeof(si_me))==-1)
		diep("bind");

	for (i=0; i<NPACK; i++)
	{
		ssize_t len;
		if ((len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*)&si_other, &slen)) < 0)
			diep("recvfrom()");
		buf[len] = '\0';
		printf("Received packet (%d bytes) from %s:%d\nData: %s\n\n",
			len, 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, (struct sockaddr*)&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)