#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
 
#define MAXBUF 1024
 
int main(int argc, char* argv[]) {
 
	int udpSocket;
	int returnStatus;
	int addrlen;
	struct sockaddr_in udpClient, udpServer;
	char buf[MAXBUF];
 
	if(argc < 3) {
		fprintf(stderr, "Usage: %s <ip address> <port>", argv[0]);
		return 1;
	}
 
	/* create socket */
	udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
 
	if(udpSocket == -1) {
		fprintf(stderr, "Could not create socket!\n");
		return 1;
	} else {
		printf("Socket created.");
	}
 
	/* client address */
	/* use INADDR_ANY to use all local addresses */
	udpClient.sin_family = AF_INET;
	udpClient.sin_addr.s_addr = INADDR_ANY;
	udpClient.sin_port = 0;
 
	returnStatus= bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));
 
	if(returnStatus == 0) {
		fprintf(stderr, "Bind completed.\n");
	} else {
		fprintf(stderr, "Could not bind to address!\n");
		close(udpSocket);
		return 1;
	}
 
	/* set up message */
	strcpy(buf, "insomnia247.nl ftmfw!\n");
 
	/* server address */
	udpServer.sin_family = AF_INET;
	udpServer.sin_addr.s_addr = inet_addr(argv[1]);
	udpServer.sin_port = htons(atoi(argv[2]));
 
	returnStatus = sendto(udpSocket, buf, strlen(buf)+1, 0, (struct sockaddr*)&udpServer, sizeof(udpServer));
 
	if(returnStatus == -1) {
		fprintf(stderr, "Could not send message!\n");
	} else {
		printf("Message sent.\n");
 
		/* message sent, look for confirmantion */
		addrlen = sizeof(udpServer);
 
		returnStatus - recvfrom(udpSocket, buf, MAXBUF, 0, (struct sockaddr*)&udpServer, &addrlen);
 
		if(returnStatus == -1) {
			fprintf(stderr, "Did not receive confirmation.\n");
		} else {
			buf[returnStatus] = 0;
			printf("Received: %s\n", buf);
		}
	}
 
	/* cleanup */
	close(udpSocket);
	return 0;
}