 <p>Well infinite data is theoretically possible while the practical implementation differ from process to process. </p> <ul> <li>Approach 1 - Generally many protocol do send size in the first few bytes ( 4 bytes ) and you can have a while loop </li> </ul> <p>{</p> <pre><code>int i = 0, ret = 1; unsigned char buffer[4]; while ( i&lt;4 &amp;&amp; ret == 0) socket.receive(buffer + i, 1 , ret); // have a while loop to read the amount of data you need. Malloc the buffer accordingly </code></pre> <p>}</p> <ul> <li>Approach 2 - Or in your case where you don't know the lenght ( infinite )</li> </ul> <p>{</p> <pre><code>char *buffer = (char *)malloc(TCP_MAX_BUF_SIZE); std::size_t total = 0, received = 0; while ( total &lt; TCP_MAX_BUF_SIZE &amp;&amp; return &gt;= 0) { socket.receive(buffer, sizeof(buffer), received); total += received; } //do something with your data </code></pre> <p>}</p> <p>You will have to break at somepoint and process your data Dispatch it to another thread of release the memory.</p>