title: C socket issue with read/write for loops
body: <p>I am creating a chat application where two programs (chat serve and chat client) send messages back and forth.
  I am trying to set it up to send one char at a time. I calculate how long the message should be and send that number from chat serve to chat client. The number transfer with no problem but when I try to read/write a message it is successfully sent with the print statements indicating the for loop should end however after the last letter is sent the program just hangs. With the print message for message succesffully sent/received never being triggered. However if Sig INT chat client while they are both hanging chat serve seems to finish its write and it displays finished sending message. I am at a bit of loss for what is going on. From my print statements it looks like the for loops should have their conditions met but the programs both seem to get stuck in them.</p>
  
  <p>Chat Serve: 
  Above the code shown I hardcode in "Tom:" as hosthandle and take user input for hostmessage which in this case I am putting as test. mone is </p>
  
  <pre><code>char mone[2]="";
  
  //send message to chatclient
  charcount= strlen(hostmessage);
  meslen=charcount+strlen(hosthandle);//total message length
  int number_to_send = meslen; 
  int converted_number = htonl(number_to_send);
  write(newsockfd, &amp;converted_number, sizeof(converted_number)); //sends number to chat client
  for (j=0;j&lt;strlen(hosthandle);j++)
  {
      mone[0]=hosthandle[j];
      n = write(newsockfd, mone, 2); //writes handle to chatclient
      if (n &lt;= 0)
      {
          perror("Message not \n");
      }
      printf("%s  %d \n",mone, j);
  }
  for (j=0;j&lt;strlen(hostmessage);j++)
  {
      mone[0]=hostmessage[j];
      n = write(newsockfd, mone, 2); //writes message to chatclient  
      printf("%s  %d \n",mone, j);
  }
  printf("Finished Sending Message");
  </code></pre>
  
  <p>Output of program (hostmessage is test message size=8)</p>
  
  <p>Transcript:</p>
  
  <pre><code>Tom: test
  T  0
  o  1
  m  2
  :  3
  t  0
  e  1
  s  2
  t  3
  </code></pre>
  
  <p>ChatClient</p>
  
  <pre><code>int received_int = 0; //this section of code receives message length from chatserve
  int return_status = read(sockfd, &amp;received_int, sizeof(received_int));
  if (return_status &gt; 0) {
      fprintf(stdout, "Received int = %d\n", ntohl(received_int));
      }
  else 
  {
      printf("did not receive message length");
  }
  for(j=0;j&lt;received_int;j++)
  {
      n=read(sockfd,kone,2); //reads in letter from chat serve
      if (n &lt;= 0)
      {
          perror("Message not recieved\n");
      }
      //printf("%d \n", n);
      printf("%s   %d \n",kone, j);
  }
  printf("Received message \n");
  </code></pre>
  
  <p>Output of program after this it just hangs when I feel like both for loops should have hit their counter.</p>
  
  <pre><code>Received int = 8
  T   0
  o   1
  m   2
  :   3
  t   4
  e   5
  s   6
  t   7
  </code></pre>