#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>

/*
 * In the included file <netinet/in.h> a sockaddr_in is defined as follows:
 * struct sockaddr_in {
 *	short	sin_family;
 *	u_short	sin_port;
 *	struct in_addr sin_addr;
 *	char	sin_zero[8];
 * }; 
 *
 * This program creates a datagram socket, binds a name to it, then reads
 * from the socket.
 */
main()
{
	int sock, length;
	struct sockaddr_in name;
	char buf[1024];

	/* Create socket from which to read. */
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) {
		perror("opening datagram socket");
		exit(1);
	}
	/* Create name with wildcards. */
	name.sin_family = AF_INET;
	name.sin_addr.s_addr = INADDR_ANY;
	name.sin_port = htons(32254);
	if (bind(sock, &name, sizeof(name))) {
		perror("binding datagram socket");
		exit(1);
	}
	while ((length = read(sock, buf, 1024)) > 0) {
	     time_t time_val;
	     char *time_str;
	     
	     (void) time(&time_val);
	     time_str = ctime(&time_val);
	     write(1, time_str, strlen(time_str) - 1);
	     write(1, " ", 1);
	     write(1, buf, length);
	}

	close(sock);
}
