title: non-terminating while loop, while using recv
body: <p>I'm trying to receive a big message from a client program, both using SOCK_STREAM sockets, and as a result, I have to use a while loop on my recv call. However, it freezes, and doesn't seem to be printing anything inside the loop.</p>
  
  <p>Here's the code for the server</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt; 
  #include &lt;stdlib.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  #define BUFSIZE 1024
  extern int errno;
  void error(char *msg)
  {
      perror(msg);
      exit(0);
  }
  
  int main(int argc, char *argv[])
  {
       int sock, newsock, len, fromlen, n;
       unsigned short port;
       struct sockaddr_in server, from;
       char buffer[BUFSIZE];
       char *msg = "I Got your message";
  
       if (argc &lt; 2) {
           fprintf(stderr,"usage %s portnumber\n",argv[0]);
           exit(0);
       }
       port = (unsigned short) atoi(argv[1]);
       sock=socket(AF_INET, SOCK_STREAM, 0);
       if (sock &lt; 0) error("Opening socket");
       server.sin_family=AF_INET;
       server.sin_addr.s_addr=INADDR_ANY;
       server.sin_port=htons(port);  
       len=sizeof(server);
       if (bind(sock, (struct sockaddr *)&amp;server, len) &lt; 0) 
            error("binding socket");
       fromlen=sizeof(from);
       if (listen(sock,5) &lt; 0) 
            error("listening");
  
       // accept connections loop
       while (1) {
           newsock=accept(sock, (struct sockaddr *)&amp;from, &amp;fromlen);
           if (newsock &lt; 0) error("Accepting");
           printf("A connection has been accepted from %s\n",
                   inet_ntoa((struct in_addr)from.sin_addr));
  
           // receive long messages loop
           while ((n = recv(newsock,buffer,BUFSIZE-1,0)) &gt; 0){
              buffer[n] = '\0';
              printf("\n message: \n%s\n", buffer);
           }
  
          len = strlen(msg);
          n = send(newsock,msg,len,0);
          if (n &lt; len) error("Error writing");
  
         if (close(newsock) &lt; 0) error("closing");           
       }
       return 0; // we never get here 
  }
  </code></pre>
  
  <p>And here's the code for the client</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt; /* for atoi */
  
  char *msg = "Very long message";
  void error(char *msg)
  {
  perror(msg);
  exit(0);
  }
  
  int main(int argc, char *argv[])
  {
  int sock, n;
  unsigned short port;
  struct sockaddr_in server;
  struct hostent *hp;
  char buffer[1024];
  
  if (argc != 3) { 
       printf("Usage: %s server port\n", argv[0]);
       exit(1);
  }
  sock= socket(AF_INET, SOCK_STREAM, 0);
  if (sock &lt; 0) error("Opening socket");
  
  server.sin_family = AF_INET;
  hp = gethostbyname(argv[1]);
  if (hp==NULL) error("Unknown host");
  memcpy((char *)&amp;server.sin_addr,(char *)hp-&gt;h_addr,         
        hp-&gt;h_length);
  port = (unsigned short)atoi(argv[2]);
  server.sin_port = htons(port);
  if (connect(sock, (struct sockaddr *)&amp;server, sizeof server) &lt; 0)
           error("Connecting");
  n = send(sock, msg, strlen(msg),0);
  printf("sent %d bytes\n", n);
  if (n &lt; strlen(msg))
           error("Writing to socket");
  
  n = recv(sock, buffer, 1023,0);
  if (n &lt; 1) error("reading from socket");
  buffer[n]='\0';
  printf("The message from the server is %s\n",buffer);
  if (close(sock) &lt; 0) error("closing");
  printf("Client terminating\n");
  
  return 0;
  }
  </code></pre>
  
  <p>The ultimate goal is to receive the full message on the server end, and then send a confirmation back to the client that the message was received.</p>