Send and receive data code I need to send and receive data. Here is solution for my original problem. I want to know how safe my finished code is and how I can improve it.

int sslWrite(SSL *ssl, const std::string &amp;data)
{
    return sslWrite(ssl, &amp;data[0], data.size());
}




int sslWrite(SSL *ssl, const void *buf, unsigned num)
{
    const int INT_SIZE = sizeof(int);
    const int numv = INT_SIZE + num;

    std::vector&lt;char&gt; bufv(numv);

    memcpy(&amp;bufv[0], &amp;numv, INT_SIZE);
    if (num != 0) {
        memcpy(&amp;bufv[INT_SIZE], buf, num);
    }

    int n = 0;
    while (n &lt; num) {
        int w = SSL_write(ssl, &amp;bufv[0], numv - n);
        if (w &lt; 0) {
            return 0;
        }
        n += w;
    }

    return n;
}




const int BUF_INIT_SIZE = 0x10000;

std::vector&lt;char&gt; sslRead(SSL *ssl)
{
    const int INT_SIZE = sizeof(int);
    std::vector&lt;char&gt; bufv(BUF_INIT_SIZE);

    int m = SSL_read(ssl, &amp;bufv.at(0), BUF_INIT_SIZE);

    if (m == 0) {
        return std::vector&lt;char&gt;();
    }

    if (m &lt; 0) {
        throw std::runtime_error("m &lt; 0");
    }

    const int n = *reinterpret_cast&lt;int *&gt;(&amp;bufv[0]);
    bufv.resize(n);

    while (m &lt; n) {
        int k = SSL_read(ssl, &amp;bufv.at(m), n - m);
        if (k &lt; 0) {
            throw std::runtime_error("k &lt; 0");
        }
        m += k;
    }

    bufv.resize(m);

    if (m &lt; INT_SIZE) {
        throw std::runtime_error("m &lt; INT_SIZE");
    }

    bufv.erase(bufv.begin(), bufv.begin() + INT_SIZE);
    return bufv;
}
Portability

The code would fail if the sender and receiver platforms disagree on the size of integer.

The code would fail if the sender and receiver platforms disagree on the endianness of integer.
Partial reads

There is a chance that the very first read returns just a couple of bytes. The code still interprets it as a valid integer.
Overall

There is no need to use the same buffer for both size and payload.

m,n,k are patently bad variable names.
Recommendations

Implement a separate send_size() and read_size() routines.