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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:13:51+00:00 2026-06-13T14:13:51+00:00

I have been writing a simple web server (http 1.0) for a class, but

  • 0

I have been writing a simple web server (http 1.0) for a class, but whenever I try to get a file (wget 127.0.0.1 /filename) is is short a few bytes. The confusing thing is when I sum the number of sent bytes it matches the file size, but not the amount wget receives.

Why is wget not getting all of the data I write to the socket?

some wget output

wget:

    --2012-10-27 19:02:00--  (try: 4)  http://127.0.0.1:5555/
    Connecting to 127.0.0.1:5555... connected.
    HTTP request sent, awaiting response... 200 Document follows
    Length: 5777 (5.6K) [text/html]
    Saving to: `index.html.6'

    99% [=====================================> ] 5,776       --.-K/s   in 0s      

    2012-10-27 19:02:00 (322 MB/s) - Read error at byte 5776/5777 (Connection reset by peer).  Retrying.


    --2012-10-27 19:03:52--  (try: 4)  http://127.0.0.1:5555/ZoEY8.jpg
    Connecting to 127.0.0.1:5555... connected.
    HTTP request sent, awaiting response... 200 Document follows
    Length: 163972 (160K) [image/jpeg]
    Saving to: `ZoEY8.jpg.4'

    91% [==================================>    ] 149,449     --.-K/s   in 0.001s  

    2012-10-27 19:03:52 (98.8 MB/s) - Read error at byte 163917/163972 (Connection reset by peer). Retrying.

Get method:

void *
processGetRequest(requestParser request)
{

  string resp= "HTTP/1.0 200 Document follows\r\nServer: lab5 \r\nContent-Length: ";
  string path="";
  path =request.path;

  //find file
  int page= open (path.c_str(),O_RDONLY);
  FILE * pageF= fdopen(page,"rb"); 


  //get size
  fseek(pageF, 0L, SEEK_END);
  int sz = ftell(pageF);
  fseek(pageF, 0L, SEEK_SET);

  //form content length
  stringstream ss;
  ss<<resp<<sz<<"\r\n";
  resp=ss.str(); 

  //make response
  if(page<0){
      cout<<"404 \n";
    resp = "HTTP/1.0 404 File Not Found\r\nServer: lab5 \r\nContent-type: text/html \r\n \r\n";

    write( request.fd, resp.c_str(), resp.length());

  return 0; 
  }
  if(path.find(".gif")!=string::npos)
   resp += "Content-type: image/gif\r\n \r\n";
  else if(path.find(".png")!=string::npos)
   resp += "Content-type: image/png\r\n \r\n";
  else if(path.find(".jpg")!=string::npos)
   resp += "Content-type: image/jpeg\r\n \r\n";
  else
   resp += "Content-type: text/html \r\n \r\n";

  //write response
  write( request.fd, resp.c_str(), resp.length());

  int total=0;      
  char buff[1024];
  int readBytes = 0;
  int er;

  //send file
  do{

  readBytes= read(page, buff, 1024);
  cout<<"read bytes "<<readBytes<<"\n";

  if(readBytes<0){
    perror("read");

    break;
  }
  total+=readBytes;
  er=  send( request.fd, buff, readBytes,0 );   
  cout<<"sent bytes "<<er<<"\n";
  if (er==-1){
    perror("send");
  }
  else if( er != readBytes){
    cout<<"Read write miss match\n";
  }

 }while(readBytes>0);

 close(page);
 return 0;
}

Edit:

I have been working at this while and I’m wondering if Im doing my sockets wrong

// Set the IP address and port for this server
struct sockaddr_in serverIPAddress; 
memset( &serverIPAddress, 0, sizeof(serverIPAddress) );
serverIPAddress.sin_family = AF_INET;
serverIPAddress.sin_addr.s_addr = INADDR_ANY;
serverIPAddress.sin_port = htons((u_short) port);

// Allocate a socket
int masterSocket =  socket(PF_INET, SOCK_STREAM, 0);
if ( masterSocket < 0) {
  perror("socket");
  exit( -1 );
}

while ( 1 ) {

// Accept incoming connections
struct sockaddr_in clientIPAddress;
int alen = sizeof( clientIPAddress );
int slaveSocket = accept( masterSocket,
              (struct sockaddr *)&clientIPAddress,
              (socklen_t*)&alen);
// send slaveSocket to get method 
}
  • 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-13T14:13:52+00:00Added an answer on June 13, 2026 at 2:13 pm

    My first answer is below, but i just noticed something..

    "Content-type: text/html \r\n \r\n";
    

    The headers must be separated from the content with two CR/LF. It looks like you have space in there

    you can try this:

     "Content-type: text/html\r\n\r\n";
    

    Is the output buffer being correctly flushed and closed after the last write? Try changing the size of your 1024 byte read buffer to something larger than your gif file. This isnt a fix, but you may get different results, and this may help track down the cause of the problem. Maybe also put some logging into the read write loop. See if the size of the last buffer write equals the number of bytes the response is missing.

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

Sidebar

Related Questions

I have been writing a small web application in C# .NET4.0 to try and
I have been writing web applications for quite sometime in PHP with MySQL. I
I'm just getting started writing a simple web crawler to get info on links
I have been having a query regarding writing unit tests for web methods which
I have been writing a project who acts as a server and have services
lately I have been writing a lot of tiny plugins for my web projects
I've been doing some simple web programming using python, and I have a basic
Guys, I’ve been writing code for 15+ years, but managed to avoid Web Development
I have been writing linear winforms for couple of months and now I am
I have been writing a SOAP client application in C++ on Ubuntu using OpenSSL

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.