Sending large data package with TCP - winsock A small struct is sent beforehand to the client that specifies package_size amount of data should be received which is what p-&gt;data_value_1 contains.

Server side:

void Server::sendDataSingleUser(char *data_in, int user, unsigned int package_size){
    package_size_sent_ = 0;
    FD_ZERO(&amp;write_set_);
    FD_SET(sockets_[user], &amp;write_set_);

    while(package_size_sent_ &lt; package_size){
        timeout_.tv_sec = 3;
        timeout_.tv_usec = 0;
        i_result_ = select(0, 0, &amp;write_set_, 0, &amp;timeout_);

        // Socket free, send a package
        if(i_result_ &gt; 0){
            i_result_ = send(sockets_[num_clients_], data_in+package_size_sent_, package_size - package_size_sent_, 0);
            // Move buffer pointer
            if(i_result_ &gt; 0){
                package_size_sent_ += i_result_;
            }

            // send error
            if(i_result_ == SOCKET_ERROR){
                // Handle the error
            }
        }
        // Socket still bussy
        else{
            // Disconnect the client
        }
    }
}


Client side:

*size_p = new char[(unsigned int)p-&gt;data_value_1];
int temp_count = 0, data_received, package_size = p-&gt;data_value_1;

while(temp_count &lt; package_size){
    data_received = recv(connect_socket_, size_p+temp_count, package_size - temp_count, 0);
    // Move buffer pointer
    if(data_received &gt; 0){
        temp_count += data_received;
    }
}


What can be improved/what's incorrect?
In both read and write you should leave the loop after an error (unless you have explicitly tested and fixed the error).

while(totalRecieved &lt; readSize)
{
    dataRecieved = recv(socket, date + totalRecieved, readSize - totalRecieved);
    if (dataRecieved &gt; 0)
    {
        totalRecieved += dataRecieved;
        continue;
    }
    if ((dataRecieved == -1) &amp;&amp; (errno == EAGAIN || errno == EWOULDBLOCK))
    {
        // Simple error just try again.
        continue;
    }
    if (dataRecieved == -1 &amp;&amp; errno == EINTR)
    {
        // An interrupt was received.
        // This is usually a signal by another thread to give up
        // but you can ignore it if you want.
        continue;
    }
    // Any other error is bad.
    // looping is just going to cause infinite loops.
    break;
}

if (totalRecieved &lt; readSize)
{
    // BAD THINGS HAVE HAPPENED
}