Thread: Download file from my webserver?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    Download file from my webserver?

    I cant seem to get it to write out the correct size to the .gif file,the original is 3kb but the new one i write to finishs with 1kb, can anyone help

    Code:
    cout << "Connected"<< endl;
    
    		 std::string sendbuffer = "GET /apache_pb.gif HTTP/1.1\r\n" 
                                  "Host: 192.168.1.4\r\n\r\n";
    
    		// std::string buffer2 = "GET /apache_pb.gif HTTP/1.1\r\n Host: 192.168.1.4\r\n\r\n";
    
    		cout << sendbuffer.c_str() << endl;
    
    		nret = send(theSocket, sendbuffer.c_str(), sendbuffer.size(), 0);
    
    		std::string rbuffer;
    		rbuffer.resize(1024,0);
    		size_t sfind;
    		std::fstream fs;
    		fs.open ("apache_pb.gif", std::ifstream::binary,std::fstream::out);
    		 
    
    		nret = recv(theSocket, &rbuffer[0],1024,0);
    		if(nret > 0)
    		{
    			sfind = rbuffer.find("\r\n\r\n");
    			if(sfind != string::npos)
    			{
    				cout << nret << endl;
    				cout << "found" << endl;
    				fs.write (rbuffer.c_str()+sfind+4,((nret)-(sfind+4)));
    			}
    		}
    
    		while(1)
    		{
    			
    			nret = recv(theSocket, &rbuffer[0],1024,0);
    		    if(nret > 0)
    			{
    				cout << nret << endl;
    				fs.write (rbuffer.c_str(),nret);
    			}
    			else
    			{
    				break;
    			}
    		}
    
    		
    
    		fs.close();
    	}
    
    	system("pause");

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    try to use std::vector<char> for buffer instead of string, I'm not sure c_str() works good with binary data
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You should also consider the possibility that the server will return 1024 characters of header data, with out a "\r\n\r\n" sequence. in which case, you need to read again, and append to the first 1024 bytes, then look for your "\r\n\r\n" again, and repeat until you find it. You should also look for the "Content-length" header, so you know how many bytes to expect. Checking the HTTP return code (200, 404, etc.) would also be a good idea.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would use curl library in this case - which will handle all the parsing for me

    libcurl - getinmemory.c
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Writing a basic HTTP client is not too difficult, but you have to do more than just read from a socket. It's a good exercise for an intermediate programmer. cURL is great, but if you don't want the overhead, writing your own is good too.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-03-2013, 02:23 AM
  2. Webserver - Detect Type of File
    By Siphon in forum C Programming
    Replies: 4
    Last Post: 09-26-2007, 02:52 AM
  3. download a file
    By munna_dude in forum C Programming
    Replies: 1
    Last Post: 05-18-2007, 05:29 AM
  4. Download File
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-24-2005, 07:30 AM