Question

I'll start with the code:

typedef std::vector<unsigned char> CharBuf;   
static const int RCV_BUF_SIZE = 1024; 
SOCKET m_socket = a connected and working socket;

// ...

CharBuf buf; // Declare buffer
buf.resize(RCV_BUF_SIZE); // resize buffer to 1024
char* p_buf = reinterpret_cast<char*>(&buf[0]); // change from unsigned char to char
//char p_buf[RCV_BUF_SIZE];

int ret = recv(m_socket, p_buf, RCV_BUF_SIZE, 0); // Does not work

for (int i=0; i<RCV_BUF_SIZE; ++i) // Works (does not crash, so the buffer is ok)
    char c = p_buf[i];

//...

Now when I run this code ret becomes -1 and WSAGetLastError() returns 10014 which means the pointer is bad.
However I can't see why this shouldn't work? If I comment out the reinterpret_cast line and use the line below it works!
It could be argued that reinterpret_cast is risky, but I think it should be ok as both unsigned char and signed char has the exact same size.
std::vectors should be safe to address directly in memory as far as I know as well.

The funny part is that when I do the same thing with the same vector-type in send() it works! Send function:

void SendData(const CharBuf& buf)
{
    buf.resize(RCV_BUF_SIZE); // resize buffer to 1024
    const char* p_buf = reinterpret_cast<const char*>(&buf[0]); // change from unsigned char to char

    int ret = send(m_socket, p_buf, (int)buf.size(), 0); // Works
}

As we see, no difference except CharBuf being const in this case, can that change anything?

Why is recv() more sensitive than send()? How can recv() even know the pointer is invalid (which it obviously isn't)?? all it should see is a char array!

As per request my whole receive function (bear in mind that I can't spell out every function in it, but I think they should be fairly self-explanatory.

bool TcpSocket::ReceiveData(CharBuf* pData)
{
    if (!CheckInitialized("ReceiveData"))
        return false;


    if (m_status != CONNECTED_STAT)
    {
        AddToErrLog("Socket not connected", 1, "ReceiveData");
        return false;
    }

    int ret;
    pData->resize(RCV_BUF_SIZE);
    char* p_buf = reinterpret_cast<char*>(&pData[0]);

    ret = recv(m_socket, p_buf, RCV_BUF_SIZE, 0);

    switch (ret)
    {
    case 0: // Gracefully closed
        AddToLog("Connection gracefully closed", 2);
        Shutdown(); // The connection is closed, no idea to keep running

        return true;

    case SOCKET_ERROR: // Error
        ret = WSAGetLastError();
        if (ret == 10004) // This indicates the socket was closed while we were waiting
            AddToLog("Socket was shut down while waiting for data", 1, "ReceiveData(1)");
        else
            AddToErrLog("Receive data failed with code: " + CStr(ret));

        AddToLog("Connection ended with error", 2);

        Shutdown();
        return false;

    default: // Normal operation
        pData->resize(ret); // Remove unused space

        return true;
    }
}

Never mind. I found it while I was pasting the function. Like always, you find your error when you try to explain it for someone else :) I leave it up to the reader to figure out what was wrong, but I'll give &pData[0] as a hint. Thanks for your help :D

Was it helpful?

Solution

Found the answer myself while pasting the whole function, &pData[0] is a hint.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top