title: Guess integer game using socket programming using C
body: <p>I am relatively new to socket programming so kindly please bear with me.</p>
  
  <p>I am trying to make a simple guessing integer game using socket programming that has specific protocols (delimiters, "H", "L", "O"). </p>
  
  <p>I am not sure why the client resend a raw blank data, when the client user has not inputted any guess integer at all.</p>
  
  <p>Below are parts of the server.c file:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt; 
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;time.h&gt; /* time for randomizer*/ 
  
  
  void error(const char *msg)
  {
      perror(msg);
      exit(1);
  }
  
  int main(int argc, char *argv[])
  {
      int sockfd, newsockfd, portno, n;
      socklen_t clilen;
      char buffer[256];
      struct sockaddr_in serv_addr, cli_addr;
  
      int GuessedInteger, integerRandom, serverFlagCorrect;
      char charGuess[4], answerServer[1];
      char* delimiter = "\\n";
  
      /** initialization of variables **/
      serverFlagCorrect = 0;
  
      /** generate random integer from 1 to 100 **/
      srand (time(NULL));
      integerRandom = (rand() % 100) + 1;
  
      printf("This is the random integer : %d \n", integerRandom);   
  
      if (argc &lt; 2) {
          fprintf(stderr,"ERROR, no port provided\n");
          exit(1);
      }
  
      // Creates the socket socket() --&gt; endpoints of sockets
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (sockfd &lt; 0) 
         error("ERROR opening socket");
      // Creates the socket socket() --&gt; endpoints of sockets
  
      // assign unique new address
      bzero((char *) &amp;serv_addr, sizeof(serv_addr));
      portno = atoi(argv[1]);
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = INADDR_ANY;
      serv_addr.sin_port = htons(portno);
  
      if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,
               sizeof(serv_addr)) &lt; 0) 
               error("ERROR on binding");
      // assign unique new address
  
      // wait for a connection
      listen(sockfd,1);
      // wait for a connection
  
      // accepts the connection
      clilen = sizeof(cli_addr);
      newsockfd = accept(sockfd, 
                  (struct sockaddr *) &amp;cli_addr, 
                  &amp;clilen);
      if (newsockfd &lt; 0) 
           error("ERROR on accept");
      // accepts the connection
  
      while (serverFlagCorrect != 1) 
      {
  
          // reads the data being received
          bzero(buffer,256);
          n = read(newsockfd,buffer,255);
          if (n &lt; 0) error("ERROR reading from socket");
          // reads the data being received
  
          printf("Buffer from client: &lt;%s&gt;\n", buffer);
          memcpy(charGuess, buffer, sizeof(charGuess));
          printf("Message from client in charGuess: &lt;%s&gt;\n", charGuess);
  
          /* Put if statement here for error out if no \n at the end */
          int len = strlen(charGuess);
          const char *last_two = &amp;charGuess[len-2];
  
          printf("Last two characters of charGuess: &lt;%s&gt;\n", last_two);
  
          if (strncmp ( last_two, delimiter, 2) )
              error (" ERROR Wrong protocol received");
  
          /** process the string to integer for server comparison **/
          GuessedInteger = atoi(charGuess);
          printf("Guessed Integer : %d \n", GuessedInteger);
  
          /** Server response for comparison**/
          if (GuessedInteger &gt; integerRandom)
              memcpy(&amp;answerServer, "L", sizeof(answerServer));
          else if (GuessedInteger &lt; integerRandom)
              memcpy(&amp;answerServer, "H", sizeof(answerServer));
          else if (GuessedInteger == integerRandom)
              {
                  serverFlagCorrect = 1;
                  memcpy(&amp;answerServer, "O", sizeof(answerServer));
              }
          printf("Value of answerServer: %c\n", *answerServer);
          /** Server response for comparison**/
  
          // sends the answer
          n = write(newsockfd, answerServer, 1);
          if (newsockfd &lt; 0) 
              error("ERROR on accept");
          // sends the answer
  
          // closes what was sent
          close(newsockfd);
          // closes what was sent
  
      }        
  
      //closes the socket if random integer was found
      close(sockfd);
  
      return 0; 
  }
  </code></pre>
  
  <p>And below is the client.c file:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt; 
  
  void error(const char *msg)
  {
      perror(msg);
      exit(0);
  }
  
  int main(int argc, char *argv[])
  {
      int sockfd, portno, n;
      struct sockaddr_in serv_addr;
      struct hostent *server;
      char buffer[1024];
  
      int integerGuess, clientFlagCorrect;
      int numberOfTries;
      char charGuess[1024], answerServer[1];
      char* delimiter = "\\n";
  
  
      if (argc &lt; 3) {
         fprintf(stderr,"usage %s hostname port\n", argv[0]);
         exit(0);
      }
      portno = atoi(argv[2]);
  
      // Creates the socket socket() --&gt; endpoints of sockets
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (sockfd &lt; 0) 
          error("ERROR opening socket");
      // Creates the socket socket() --&gt; endpoints of sockets
  
      server = gethostbyname(argv[1]);
      if (server == NULL) {
          fprintf(stderr,"ERROR, no such host\n");
          exit(0);
      }
  
      // connects to the service in connect()
      bzero((char *) &amp;serv_addr, sizeof(serv_addr));
      serv_addr.sin_family = AF_INET;
      bcopy((char *)server-&gt;h_addr, 
           (char *)&amp;serv_addr.sin_addr.s_addr,
           server-&gt;h_length);
      serv_addr.sin_port = htons(portno);
      if (connect(sockfd,(struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0) 
          error("ERROR connecting");
      // connects to the service
  
  
      /** initialization of variables **/
      clientFlagCorrect = 0;
      numberOfTries = 0;
  
      while (clientFlagCorrect != 1) 
      {
          numberOfTries = numberOfTries + 1;
  
          /** initialization of variables **/
          integerGuess = 0;
          memset(charGuess, 0, sizeof(charGuess));
  
          // ask the user for the guessed number
          printf("Guess: ");
          bzero(buffer,sizeof(buffer));
          fgets(buffer,sizeof(buffer)-1,stdin);
          printf("Buffer to be processed : &lt;%s&gt;\n", buffer);
          // ask the user for the guessed number
  
          /** process the integer to string and add a delimiter **/
          integerGuess = atoi(buffer);
          printf("integerGuess : &lt;%d&gt; \n", integerGuess);
          sprintf( charGuess, "%d", integerGuess);    
          strcat( charGuess, delimiter);
          printf("String Guess : &lt;%s&gt; \n", charGuess);
  
          memset(buffer,0,sizeof(buffer));
          memcpy(buffer, charGuess, sizeof(charGuess));
          printf("Buffer to send : &lt;%s&gt;\n",buffer);
          /** process the integer to string and add a delimiter **/
  
          // send the string that was processed
          n = write(sockfd,buffer,strlen(buffer));
          if (n &lt; 0) 
               error("ERROR writing to socket");
          // send the string that was processed
  
          // reads the data being received
          bzero(buffer,256);
          n = read(sockfd,buffer,255);
          if (n &lt; 0) 
               error("ERROR reading from socket");
          // reads the data being received
  
          printf("Buffer received : &lt;%s&gt;\n",buffer);
  
          memcpy(&amp;answerServer, buffer, sizeof(answerServer));
          printf ("Value of answerServer : &lt;%c&gt; \n", *answerServer);
  
          /** Client response **/
          if (strncmp ( &amp; answerServer[0],"L",sizeof(answerServer)) == 0)
              printf("Lower \n");
          else if (strncmp ( &amp; answerServer[0],"H",sizeof(answerServer)) == 0)
              printf("Higher \n");
          else if (strncmp ( &amp; answerServer[0],"O",sizeof(answerServer)) == 0)
              {
                  printf("Correct \n");
                  clientFlagCorrect = 1;
              }
          else
              error("ERROR Wrong message received");
  
      }
  
      printf ("Tries: %d \n", numberOfTries);
  
      printf("%s\n",buffer);
  
      close(sockfd);
      return 0;
  }
  </code></pre>
  
  <p>Any advice in fixing this rather annoying bug that I have been trying to fix for half a day?</p>
  
  <p>Thought I mention (if it matters), I am not going to use fork here yet. I will also do something similar that has fork functions after I pulled this simple guessing game without using fork first.</p>
  
  <p>EDIT1: Added what I get via terminal (and sorry, I don't think it's good that I remove some parts of the code of the server and client):</p>
  
  <p>Server terminal:</p>
  
  <pre><code>$ gcc -Wall -o server server.c
  $ ./server 5678
  This is the random integer : 66 
  Buffer from client: &lt;20\n&gt;
  Message from client in charGuess: &lt;20\n&gt;
  Last two characters of charGuess: &lt;\n&gt;
  Guessed Integer : 20 
  Value of answerServer: H
  ERROR reading from socket: Bad file descriptor
  $ 
  </code></pre>
  
  <p>Client terminal:</p>
  
  <pre><code>$ ./client3 localhost 5678
  Guess: 20
  Buffer to be processed : &lt;20
  &gt;
  integerGuess : &lt;20&gt; 
  String Guess : &lt;20\n&gt; 
  Buffer to send : &lt;20\n&gt;
  Buffer received : &lt;H&gt;
  Value of answerServer : &lt;H&gt; 
  Higher 
  Guess:   
  </code></pre>
  
  <p>I've checked that it has been sending from client when the user from the client has not been sending anything yet.</p>