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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:01:24+00:00 2026-05-30T05:01:24+00:00

I am trying to use the http protocol GET to ask the server to

  • 0

I am trying to use the http protocol GET to ask the server to send me a picture. It compiles and will ask for the picture but the server only will send me a small part of it.

My code is:

#include <iostream>
#include <string>
#include <Windows.h>
#include <fstream>

#include <stdlib.h>
#include <winsock.h>

#pragma comment(lib, "wsock32.lib")

using namespace std;

#define BUFFERSIZE 1024
void die_with_error(char *errorMessage);
void die_with_wserror(char *errorMessage);

int main(int argc, char *argv[])
{
string request;
string response;
int resp_leng;

char buffer[BUFFERSIZE];
struct sockaddr_in serveraddr;
int sock;

WSADATA wsaData;
char *ipaddress = "210.125.167.240";
int port = 80;

//http://cwc.ghc.ac.kr/skin/base/board/view_brit2.gif
request+="GET /skin/base/board/view_brit2.gif HTTP/1.0\r\n";
request+="Host: cwc.ghc.ac.kr\r\n";
request+="\r\n";

//init winsock
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
    die_with_wserror("WSAStartup() failed");

//open socket
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    die_with_wserror("socket() failed");

//connect
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family      = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(ipaddress);
serveraddr.sin_port        = htons((unsigned short) port);
if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
    die_with_wserror("connect() failed");

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

//get response
response = "";
resp_leng= BUFFERSIZE;
while (resp_leng == BUFFERSIZE)
{
    resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
    if (resp_leng>0)
        response+= string(buffer).substr(0,resp_leng);
}

ofstream myfile;
myfile.open ("C:\\a\\example.gif");

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

//get response
response = "";
resp_leng= BUFFERSIZE;
while (resp_leng == BUFFERSIZE)
{
    resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
    if (resp_leng>0)
        response+= string(buffer).substr(0,resp_leng);
}


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


myfile << response;
myfile.close();

//disconnect
closesocket(sock);

//cleanup
WSACleanup();
return 0;
}

void die_with_error(char *errorMessage)
{
cerr << errorMessage << endl;
   exit(1);
}

void die_with_wserror(char *errorMessage)
{
    cerr << errorMessage << ": " << WSAGetLastError() << endl;
    exit(1);
} 

/*

It compiles and runs fine the output is: 

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
ETag: W/"60-1144808550000"
Last-Modified: Wed, 12 Apr 2006 02:22:30 GMT
Content-Type: image/gif
Content-Length: 60
Date: Wed, 22 Feb 2012 04:29:04 GMT
Connection: close

GIF89a
*/

What is wrong, why does it not download the full picture?

  • 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-30T05:01:26+00:00Added an answer on May 30, 2026 at 5:01 am

    It looks like this loop is the problem:

    while (resp_leng == BUFFERSIZE)
    {
        resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
        if (resp_leng>0)
            response+= string(buffer).substr(0,resp_leng);
    }
    

    Calling recv() will give you some number of bytes from the socket, that may be less than what you ask for. So unlike reading from a file, asking for BUFFERSIZE bytes may return anywhere from 1 to BUFFERSIZE bytes.

    Instead, loop until you get to the end of the socket:

    while (true)
    {
        resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
        if (resp_leng <= 0) {
            break;
        }
        response+= string(buffer,resp_leng);
    }
    

    Notice that I’ve also changed the call to the string() constructor on the last line of the loop (yours would work, but would do more effort than is necessary).

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

Sidebar

Related Questions

I'm trying to use OpenID on my site but I get this error: Protocol
I am trying to use the http_get function. But I get a undefined reference
I'm trying to to use this class http://robbyonrails.com/articles/2005/05/11/parsing-a-rss-feed but am not sure where to
What I am doing : Trying to get familiar with the HTTP protocol and
I am trying to implement a protocol which I will use for my application
I'm trying to use HTTPClient to get a page, but it seems to return
I was trying to use http://www.jongsma.org/gc/scripts/ofx-ba.py to grab my bank account information from wachovia.
We're trying to use Axis2 to call a web service that cannot use HTTP/1.1
I am trying to use the focusout( http://api.jquery.com/focusout/ ) function in jQuery, to perform
I am trying to use django-socialauth ( http://github.com/uswaretech/Django-Socialauth ) for authenticating users for my

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.