/************************************************************************/    
/*                                                                      */
/*  This program is server for "master mind" game. It recieves          */
/*       users request and process it, then it reply the number         */
/*       of correct digit and location and number of correct            */
/*       digit only.                                                    */
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*                                                                      */
/************************************************************************/    
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <errno.h> 
    #include <string.h> 
    #include <sys/types.h> 
    #include <netinet/in.h> 
    #include <netdb.h> 
    #include <sys/socket.h> 
    #include <sys/wait.h> 

    #define MYPORT 9999    /* the port users will be sending to */
    #define MAXBUFLEN 100 
    int main(int argc, char *argv[])
    {
	/************************/
	/*Definitions		*/
	/************************/
        int sockfd;
        struct sockaddr_in	their_addr; 	/*Server address information 	*/
        struct hostent 		*he;		/*Struct for name resolving	*/
        int 			numbytes;	/*Number of bytes that were sent*/
        int			addr_len; 
	int I; 					/*Index				*/
	char 			buf[MAXBUFLEN];  
	
        if (argc != 3) {
            fprintf(stderr,"usage: %s hostname message\n",argv[0]);
            exit(1);
        }

	/*--------------------------*/
	/*Communication preparation */
	/*--------------------------*/

        if ((he=gethostbyname(argv[1])) == NULL) {  /*Transform host name into address	*/
            herror("gethostbyname");
            exit(1);
        }

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

        their_addr.sin_family = AF_INET;      			/*Scoket address family		*/
        their_addr.sin_port = htons(MYPORT);  			/*Port in network byte order 	*/
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);	/*Host address			*/
        bzero(&(their_addr.sin_zero), 8);     			/* zero the rest of the struct	*/


	/*----------------------*/
	/*Communication process	*/
	/*----------------------*/


	printf("Now sending packet number %d \n",I);
        if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, \
       	     (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
           	perror("sendto");
           	exit(1);
       	}
	else
        	printf("sent %d bytes to %s\n",numbytes,inet_ntoa(their_addr.sin_addr));
	
        if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, \
	     (struct sockaddr *)&their_addr, &addr_len)) == -1) {
		perror("recvfrom");
		exit(1);
        }
	else {

		buf[numbytes] = '\0';   /*Add termination to the string         */
		printf("got packet from %s, length is: %d, packet='%s' \n",
			inet_ntoa(their_addr.sin_addr),numbytes,buf );
	}

        close(sockfd);

        return 0;
    }


