Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3352504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:02:18+00:00 2026-05-18T02:02:18+00:00

I’ll start with the code: typedef std::vector<unsigned char> CharBuf; static const int RCV_BUF_SIZE =

  • 0

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 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:02:18+00:00Added an answer on May 18, 2026 at 2:02 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.