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 7945657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:56:06+00:00 2026-06-04T00:56:06+00:00

I am experimenting with the HTTP protocol using Winsock2. I am working on a

  • 0

I am experimenting with the HTTP protocol using Winsock2. I am working on a function

int recv_data(const char *hostname, char *resp);

The function is meant to send an HTTP HEAD request to a given host and then receive a response. It allocated memory at the pointer resp and copies the response there before returning the total number of bytes received for the response.

Here is my recieve loop:

int recv_data(const char *hostname, char *resp)
{
    int totalRecvd = 0;
    stringstream sStream;
    while (true)
    {
        char buffer[MAX_HEADER_SIZE];
        int retValue = recv(s, buffer, MAX_HEADER_SIZE, 0);
        if (retValue == 0)
            break;  // connection has been closed
        else if (retValue == SOCKET_ERROR)
            throw RecvException("http_headreq() unable to receive data");
        else    //
        {   
            buffer[retValue] = 0; // append null terminator
            sStream << buffer;    // dump buffer into stream
            totalRecvd += retValue + 1; // tally received bytes
        }
    }

    /** Allocate and read entire read stream into memory */
    resp = new char[totalRecvd + 1];
    strcpy(resp, sStream.str().c_str());
    return totalRecvd);
}

All of this works just fine and all and if I output resp at this point it outputs just fine. I just have a problem if I try to output resp after the function has returned apparently. I do not believe this should be how things normally go and if I am not mistaken I believe it has something to do with me using the stringstream to temporarily store the response. I think I have read somewhere about the data that stringstream collects going out of scope.

I was hoping that I could have this function set up this way where the caller can just pass in a char* and the function will allocate the correct amount of memory (which is determined at runtime depending on the host and the number of bytes returned by recv(). Is there anyway for me to get a permanent copy from a stringstream in memory as a char array with the pointer being bad after the function returns and the stringstream goes out of scope?

[EDIT]: Here is the solution posted below incorporated into my problem, anyone looking to reuse this for Winsock2 proramming have at it, seems to work well. Will recv data from the server until the connection is closed when recv() returns 0. The solution is passing in a reference to the pointer, because the new operator changes the pointer and that change is not reflected after the function returns unless it is passed in by reference.

int recv_data(SOCKET s, char *&data)
{
    int totalRecvd = 0;
    std::stringstream sStream;
    while (true)
    {
        char buffer[MAX_HEADER_SIZE];
        int retValue = recv(s, buffer, MAX_HEADER_SIZE, 0);
        if (retValue == 0)
            break;  // connection has been closed
        else if (retValue == SOCKET_ERROR)
            throw RecvException("http_headreq() unable to receive data");
        else    //
        {   
            buffer[retValue] = 0; // append null terminator
            sStream << buffer;    // dump buffer into stream
            totalRecvd += retValue + 1; // tally received bytes
        }
    }

    /** Allocate and read entire read stream into memory */
    data = new char[totalRecvd + 1];
    strcpy_s(data, totalRecvd, sStream.str().c_str());
    data[totalRecvd] = 0;
    return totalRecvd;
}
  • 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-06-04T00:56:08+00:00Added an answer on June 4, 2026 at 12:56 am

    resp is a local variable in the http_req function. Updating the value of resp will have no effect outside of http_req. This line:

    resp = new char[totalRecvd + 1];
    

    will have only local effect.

    Try this signature:

    int http_req(const char *hostname, char *&resp);
    

    Even better, try returning the data in a C++ way:

    std::string http_req(const std::string& hostname) {
        ...
        return sStream.str()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm experimenting with XSLT2, using a stylesheet based on this answer: <xsl:stylesheet version=2.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
http://jsfiddle.net/Codemonkey/ye5qv/2/ I am experimenting with writing large workloads to recur using setInterval so that
I'm experimenting with DIVs to align my page's contents: http://labs.pieterdedecker.be/test/test.htm As you can see,
I am experimenting with various responses from a simple NodeJS HTTP server. The effect
I am experimenting with OpenCL programming. I was following the article in http://www.thebigblob.com/getting-started-with-opencl-and-gpu-computing/ When
Hello I've been expirimenting with the Google Picker API (http://code.google.com/apis/picker/). I've got a working
Experimenting with android game development. I'm using PNG format for images, and was wondering
I am experimenting with following code: http://www.jsresources.org/examples/MidiNote.java.html to operate my Novation Launchpad MIDI Controller.
I am experimenting with embedding my pure offline mobile HTML5 ebook reader (http://www.hyper-books.com/) into
I wrote a simple HTTP client and server in Python for experimenting. The first

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.