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

  • SEARCH
  • Home
  • 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 641013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:00:59+00:00 2026-05-13T21:00:59+00:00

I did my own Socket class, to be able to send and receive HTTP

  • 0

I did my own Socket class, to be able to send and receive HTTP requests.
But I still got some problems. The following code (my receive function) is still buggy, and crashing sometimes.
I tried debugging it, but it must be somewhere in the pointer arithmetics / memory management.

int Socket::Recv(char *&vpszRecvd)
{
 //vpszRecvd = NULL;
 int  recvsize = 0;
 char TempBuf[1024];
 int  Result = 0;
 char* temp;


 do
 {
  memset(TempBuf, 0, sizeof(TempBuf));

  Result = recv( this->sSocket, TempBuf, sizeof(TempBuf) -1, 0 );
  if (recvsize == 0)
   recvsize = Result;

  if ( Result > 0 )
  {
   if ( vpszRecvd != NULL )
   {
    if (temp == NULL)
    {
     temp = (char*)calloc(recvsize + 1, sizeof(char));
    }
    else
    {
     realloc(temp, recvsize + 1);
    }
    if (temp == NULL)
     return 0;

    memcpy(temp, vpszRecvd, recvsize);
    realloc(vpszRecvd, recvsize + Result);

    if (vpszRecvd == NULL)
     return 0;

    memset(vpszRecvd, 0, recvsize + Result);
    memcpy(vpszRecvd, TempBuf, Result);
    memcpy(vpszRecvd + recvsize, TempBuf, Result);
    recvsize += Result; 
   }
   else
   {
    realloc(vpszRecvd, Result);

    if (vpszRecvd == NULL)
     return 0;

    memset(vpszRecvd, 0, Result);
    memcpy(vpszRecvd, TempBuf, Result);
    recvsize += Result;
   }
  }
  else if (  Result == 0 )
  {
   return recvsize;

  }
  else //if (  Result == SOCKET_ERROR )
  {
   closesocket(this->sSocket);
   this->sSocket = INVALID_SOCKET;
   return SOCKET_ERROR;
  }
 }
 while( Result > 0 );

 return recvsize;
}

Does anybody see anything that could cause the crash, or does anyone have a better / faster / smaller and stable example how to receive a full packet via recv()?

I can’t use strings, it must be done with chars, though.

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-13T21:01:00+00:00Added an answer on May 13, 2026 at 9:01 pm

    You don’t initialise temp and, on top of that, your call to realloc is wrong. It should be:

    temp = realloc (temp, recvsize+1);
    

    When you call realloc as you have, you throw away the new address and there’s a good chance that the old address has now been freed. All bets are off when you then try to dereference it.

    The reason realloc returns a new address is because expansion of the buffer may necessitate it being moved if that current block is surrounded in the memory arena (in other words, it can’t just expand into a free block following it). In that case, a new block will be created in the arena, the contents transferred from the old block and the old block freed. You have to get the return value from realloc in case that happens.

    Keep in mind that realloc doesn’t have to return a new pointer, it may give you the same pointer if, for example, there was enough free space after the block to satisfy the new size or if you’re reducing the size.

    It can also return NULL if it can’t expand the block, you should watch out for that as well, especially since:

    temp = realloc (temp, newsize);
    

    will result in a memory leak when it returns NULL (it doesn’t free the old block).

    A few other things:

    • you rarely need to use calloc, especially in this case since you’re copying over the memory anyway.
    • similarly, you don’t need to memset a memory chunk to 0 if you’re immediately going to memcpy over it.
    • provided you initialise temp to NULL, you can just use realloc without testing it. That’s because realloc(NULL,7) is identical to malloc(7) – realloc is perfectly capable of starting with a null pointer.
    • since you don’t need calloc, this is for education only – sizeof(char) is always 1 by definition.
    • you seem to be doing an awful lot of unnecessary copying of data.

    Why don’t we start with something a bit simpler? Now, this is totally from my head so there may be some bugs but it’s at least cut down from the memory-moving behemoth in the question 🙂 so should be easier to debug.

    It’s basically broken down into:

    • initialise empty message.
    • enter infinite loop.
      • get a segment.
      • if error occurred, free everything and return error.
      • if no more segments, return current message.
      • create space for new segment at end of message.
      • if no space could be created, free everything and return empty message.
      • append segment to message and adjust message size.

    and the code looks like this:

    int Socket::Recv(char *&vpszRecvd) {
        int  recvsize = 0;
        char TempBuf[1024];
        int  Result = 0;
        char *oldPtr;
    
        // Optional free current and initialise to empty.
    
        //if (vpszRecvd != NULL) free (vpszRecvd);
        vpszRecvd = NULL;
    
        // Loop forever (return inside loop on end or error).
    
        do {
            Result = recv( this->sSocket, TempBuf, sizeof(TempBuf) -1, 0 );
    
            // Free memory, close socket on error.
    
            if (Result < 0) {
                free (vpszRecvd);
                closesocket(this->sSocket);
                this->sSocket = INVALID_SOCKET;
                return SOCKET_ERROR;
            }
    
            // Just return data and length on end.
    
            if (Result == 0) {
                return recvsize;
            }
    
            // Have new data, use realloc to expand, even for initial malloc.
    
            oldPtr = vpszRecvd;
            vpszRecvd = realloc (vpszRecvd, recvsize + Result);
    
            // Check for out-of-memory, free memory and return 0 bytes.
    
            if (vpszRecvd == NULL) {
                free (oldPtr);
                return 0;
            }
    
            // Append it now that it's big enough and adjust the size.
    
            memcpy (&(vpszRecvd[recvsize], TempBuf, Result);
            recvsize += Result;
        } while (1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.