title: Read from socket freeze
body: <p>I have problem while reading data from client on server. The read() function will always freeze (block) after all data are readed and waiting for more data what is undesirable for me.</p>
  
  <p>Server program:</p>
  
  <pre><code>   soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
     struct sockaddr_in sin;  
     sin.sin_family = AF_INET;  
     sin.sin_port = htons(port);   
     sin.sin_addr.s_addr = INADDR_ANY; 
     bind(soc, (struct sockaddr*) &amp;sin, sizeof(sin));
  
     if (listen(soc, MAX))
        return;
  
     int socc;               // socket for clinet     
     while (1) {
        if ((socc = accept(soc, (struct sockaddr *) &amp;sin, sinlen)) &lt; 0)
           break;
        while ((result = read(socc, pointer, SIZE)) &gt; 0) {
           // after the data are readed, read function will block
        }
        // do some stuff and write reply to client =&gt; will never done
     }
  </code></pre>
  
  <p>Client program:</p>
  
  <pre><code>   ...
     soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)  
     struct sockaddr_in socketAddr;
     socketAddr.sin_family = AF_INET;
     socketAddr.sin_port = htons(port);
     memcpy(&amp;(socketAddr.sin_addr), host-&gt;h_addr, host-&gt;h_length);
     if (connect(soc, (sockaddr *)&amp;socketAddr, sizeof(socketAddr)) == -1)  
        return;
     if (write(soc, req.c_str(), strlen(req.c_str())) &lt; 0)
        return;
  </code></pre>
  
  <p>The main problem is that I don't know how much data will be client sending to server, so the server should read all data from socket and after nothing is coming, leave the reading cycle. But the server read whole message for example (30 bytes) and waiting for more (but no more is coming). The sockets are still opened because the client is waiting for reply from server.</p>