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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:43:49+00:00 2026-05-17T06:43:49+00:00

I am trying to record the time between ‘http request’ package and ‘http response’

  • 0

I am trying to record the time between ‘http request’ package and ‘http response’ package.

I write an socket client using winsock. The code is below

        if (send(sock, request.c_str(), request.length(), 0) != request.length())
            die_with_error("send() sent a different number of bytes than expected");

        // Record the time of httpRequestSent
        ::QueryPerformanceCounter(&httpRequestSent);
        ::QueryPerformanceFrequency(&frequency);

        //get response
        response = "";
        resp_leng= BUFFERSIZE;
        http_leng= 381;
        while(resp_leng==BUFFERSIZE||http_leng>0)
        {
            resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
            http_leng= http_leng - resp_leng;
            if (resp_leng>0)
                response+= string(buffer).substr(0,resp_leng);
            //note: download lag is not handled in this code
        }

        ::QueryPerformanceCounter(&httpResponseGot);

        //display response
        cout << response << endl;

        // Display the HTTP duration
        httpDuration = (double)(httpResponseGot.QuadPart - httpRequestSent.QuadPart) / (double)frequency.QuadPart;
        printf("The HTTP duration is %lf\n", httpDuration);

The code works nicely except one situation: HTTP Retransmition. I used wireshark to monitor packages and found out once there is a retransmition the code seems block on recv(), but cannot get any data from the socket buffer. I wonder why would this happen. Could somebody explain the reasons?
Any help will be appreciated.

  • 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-17T06:43:49+00:00Added an answer on May 17, 2026 at 6:43 am

    Here is a second answer with more dynamic buffer handling and more error checking:

    void send_data(SOCKET sock, void *data, unsigned int data_len)
    {
        unsigned char *ptr = (unsigned char*) data;
    
        while (data_len > 0)
        {
            int num_to_send = (int) std::min(1024*1024, data_len);
    
            int num_sent = send(sock, ptr, num_to_send, 0);
            if (num_sent < 0)
            {
                if ((num_sent == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK))
                    continue;
    
                die_with_error("send() failed");
            } 
    
            if (num_sent == 0)
                die_with_error("socket disconnected");
    
            ptr += num_sent;
            data_len -= num_sent;
        } 
    }
    
    unsigned int recv_data(SOCKET sock, void *data, unsigned int data_len, bool error_on_disconnect = true)
    {
        unsigned char *ptr = (unsigned char*) data;
        unsigned int total = 0;
    
        while (data_len > 0)
        {
            int num_to_recv = (int) std::min(1024*1024, data_len);
    
            int num_recvd = recv(sock, ptr, num_to_recv, 0);
            if (num_recvd < 0)
            {
                if ((num_recvd == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK))
                    continue;
    
                die_with_error("recv() failed");
            }
    
            if (num_recvd == 0)
            {
                if (error_on_disconnect)
                    die_with_error("socket disconnected");
    
                break;
            }
    
            ptr += num_recvd;
            datalen -= num_recvd;
            total += num_recvd;
        }
        while (true);
    
        return total;
    }
    
    std::string recv_line(SOCKET sock)
    {
        std::string line;
        char c;
    
        do
        {
            recv_data(sock, &c, 1);
    
            if (c == '\r')
            {
                recv_data(sock, &c, 1);
    
                if (c == '\n')
                    break;
    
                line += '\r';
            }
    
            else if (c == '\n')
                break;
    
            line += c;
        }
    
        return line;
    }
    
    void recv_headers(SOCKET sock, std::vector<std::string> *hdrs)
    {
        do
        {  
            std::string line = recv_line(sock);
            if (line.length() == 0)
              return;
    
            if (hdrs)
                hdrs->push_back(line);
        }
        while (true);
    }
    
    unsigned int recv_chunk_size(SOCKET sock)
    {
        std::string line = recv_line(sock);
        size_t pos = line.find(";");
        if (pos != std::string::npos)
          line.erase(pos);
        char *endptr;
        unsigned int value = strtoul(line.c_str(), &endptr, 16);
        if (*endptr != '\0')
            die_with_error("bad Chunk Size received");
        return value;
    }
    
    std::string find_header(const std::vector<std::string> &hrds, const std::string &hdr_name)
    {
        std::string value;
    
        for(size_t i = 0; i < hdrs.size(); ++i)
        {
            const std::string hdr = hdrs[i];
    
            size_t pos = hdr.find(":");
            if (pos != std::string::npos)
            {
                if (hdr.compare(0, pos-1, name) == 0)
                {
                    pos = hdr.find_first_not_of(" ", pos+1);
                    if (pos != std::string::npos)
                        return hdr.substr(pos);
    
                    break;
                }
            }
        }
    
        return "";
    }
    
    {
        // send request ...
    
        std::string request = ...;
    
        send_data(sock, request.c_str(), request.length());
    
        // Record the time of httpRequestSent
        ::QueryPerformanceCounter(&httpRequestSent);
        ::QueryPerformanceFrequency(&frequency);  
    
        // get response ...
    
        std::vector<std::string> resp_headers;
        std::vector<unsigned char> resp_data;
    
        recv_headers(sock, &resp_headers);
    
        std::string transfer_encoding = find_header(resp_headers, "Transfer-Encoding");
        if (transfer_encoding.find("chunked") != std::string::npos)
        {
            unsigned int chunk_len = recv_chunk_size(sock);
            while (chunk_len != 0)
            {
                size_t offset = resp_data.size();
                resp_data.resize(offset + chunk_len);
                recv_data(sock, &resp_data[offset], chunk_len);
    
                recv_line(sock);
                chunk_len = recv_chunk_size(sock);
            }
    
            recv_headers(sock, NULL);
        }
        else
        {
            std::string content_length = find_header(resp_headers, "Content-Length");
            if (content_length.length() != 0)
            {
                char *endptr;
                unsigned int content_length_value = strtoul(content_length.c_str(), &endptr, 10);
    
                if (*endptr != '\0')
                    die_with_error("bad Content-Length value received");
    
                if (content_length_value > 0)
                {
                    resp_data.resize(content_length_value);
                    recv_data(sock, &resp_data[0], content_length_value);
                }
            }
            else
            {
                unsigned char buffer[BUFFERSIZE];
                do
                {
                    unsigned int buffer_len = recv_data(sock, buffer, BUFFERSIZE, false);
                    if (buffer_len == 0)
                        break;
    
                    size_t offset = resp_data.size();
                    resp_data.resize(offset + buffer_len);
                    memcpy(&resp_data[offset], buffer, buffer_len);
                }
                while (true)
            }
        }
    
        ::QueryPerformanceCounter(&httpResponseGot);  
    
        // process resp_data as needed
        // may be compressed, encoded, etc...
    
        // Display the HTTP duration  
        httpDuration = (double)(httpResponseGot.QuadPart - httpRequestSent.QuadPart) / (double)frequency.QuadPart;  
        printf("The HTTP duration is %lf\n", httpDuration); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're trying to record video to a Flash Media Server using a NetConnection object
I am trying to record using the iphones microphone: This is my code: NSArray
I am trying to select the most recent record between 2 users based on
I am trying to record audio by MediaRecorder, and I get an error, I
I was trying to record a voice in my app and then store it
I am trying to record the success or failure of a number of copy
I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**
I am trying to record whenever a user clicks on Facebook Like button. Facebook
I'm trying to record sound by creating an android application. here is the code:
I've tried to overcome this for a while. I'm trying to record sound, but

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.