/*********************************************************************/
/* UDP server                                                        */    
/*                                                                   */    
/* This program is a UDP server that recieves from udp-cli a message */    
/* sent multiple times and the time is measured.                     */    
/*                                                                   */    
/*                                                                   */    
/*                                                                   */    
/*                                                                   */    
/*                                                                   */    
/*                                                                   */    
/*                                                                   */    
/*                                                                   */    
/*                                                                   */    
/*                                                                   */    
/*********************************************************************/
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <errno.h> 
    #include <string.h> 
    #include <sys/types.h> 
    #include <netinet/in.h> 
    #include <sys/socket.h> 
    #include <sys/wait.h> 
    #include <sys/time.h> 
    #include <sys/unistd.h> 

    #define MYPORT 9999    /* the port users will be sending to */

    #define MAXBUFLEN 100

    main()
    {
        int sockfd;
	int i;
        struct sockaddr_in my_addr;    /* my address information */
        struct sockaddr_in their_addr; /* connector's address information */
        int addr_len, numbytes;
        char buf[MAXBUFLEN];
	struct timeval time;
	struct timezone zone;


        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }
	else
		printf(" \n The socket got sockfd=%d \n ",sockfd);

        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; /* auto-fill with my IP */
        bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct */

        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) /* The bind command makes the ability to wait for messages */
                                                                       == -1) {
            perror("bind");
            exit(1);
        }
	printf("Before the loop \n");
        addr_len = sizeof(struct sockaddr);

	if (gettimeofday(&time,&zone) < 0 ){
		perror("bind");
		exit(1);
	};
	if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, \
                            (struct sockaddr *)&their_addr, &addr_len)) == -1) {
                        perror("recvfrom");
                        exit(1);
                }

	printf(" After first packet - Time= %ld and %ld \n",time.tv_sec, time.tv_usec);

	for (i=1;i<=100;i++){
        	if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, \
               	            (struct sockaddr *)&their_addr, &addr_len)) == -1) {
            		perror("recvfrom");
            		exit(1);
        	}
		if (gettimeofday(&time,&zone) < 0 ){
                	perror("bind");
                	exit(1);
        	};
        	printf("Time= %ld and %ld ",time.tv_sec, time.tv_usec);

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


	if (gettimeofday(&time,&zone) < 0 ){
		perror("bind");
		exit(1);
	};
	printf("Time= %ld and %ld \n",time.tv_sec, time.tv_usec);

        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);

        close(sockfd);
    }