/*
** listener.c -- a datagram sockets "server" demo
*/

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

#define MYPORT 0x5613	// the port users will be connecting to

#define MAXBUFLEN 100

int main(void)
{
	int sockfd;
        unsigned int totBytes=0, pktCnt=0;
	struct sockaddr_in my_addr;	// my address information
	struct sockaddr_in their_addr; // connector's address information
	socklen_t addr_len;
	int numbytes;
	char buf[MAXBUFLEN];

	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror("socket");
		exit(1);
	}

	my_addr.sin_family = AF_INET;		 // host byte order
	my_addr.sin_port = htons(MYPORT);	 // short, network byte order
	my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
	memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

	if (bind(sockfd, (struct sockaddr *)&my_addr,
		sizeof(struct sockaddr)) == -1) {
		perror("bind");
		exit(1);
	}

	addr_len = sizeof(struct sockaddr);

    while (1)
    {
	    if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
		    (struct sockaddr *)&their_addr, &addr_len)) == -1) {
		    perror("Error recvfrom\n");
		    exit(1);
	    }

	    printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
	    printf("packet is %d bytes long\n", numbytes);
	    buf[numbytes] = '\0';
	    printf("packet contains \"%s\"\n",buf);

        if ((numbytes = sendto(sockfd, buf, numbytes, 0, 
            (struct sockaddr *)&their_addr, addr_len)) == -1) {
            perror("Error sendto\n");
            exit(1);
        }
	    printf("send packet\n");

    }
	close(sockfd);

	return 0;
}
