Thread: custom winsock http downloader help

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

    custom winsock http downloader help

    i am not sure why but everytime the file downloads, it seems to get corrupted, can anyone help?

    Code:
    #include <iostream>
    #include <winsock.h>
    #include <stdio.h>
    #pragma comment( lib, "ws2_32.lib" ) 
    using namespace std;
    
    int main()
    {
    	
    	cout << "Setting Up Winsock" << endl;
    	WSADATA wsaData;
    	int Result = WSAStartup( MAKEWORD( 1,1 ), &wsaData );
    	if(Result == 0)
    	{
    		cout << "Winsock Setup Success" << endl;
    	}
    	else
    	{
    		cout << "Winsock Setup Failed" << endl;
    	}
    
    	int RecvCount = 0;
    
    Start:
    
    	SOCKET Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    	if(Socket != INVALID_SOCKET)
    	{
    		cout << "Socket Setup Success" << endl;
    	}
    	else
    	{
    		cout << "Socket Setup Failed" << endl;
    	}
    
    	cout << "Enter Host Name" << endl;
    	//char Hostname[64];
    	//cin >> Hostname;
    	char* Hostname = "127.0.0.1";
    	LPHOSTENT hostEntry;
    	hostEntry = gethostbyname(Hostname);//Get ip from Server by hostname
    
    	sockaddr_in addr;
    	addr.sin_family = AF_INET;
    	addr.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
    
    	cout << "Enter Port Number" << endl;
    	int Port = 80;
    	//cin >> Port;
    	addr.sin_port = htons( Port ); //Set Port
    
    	if(connect( Socket, (LPSOCKADDR) &addr, sizeof(struct sockaddr) ) == SOCKET_ERROR)
    	{
    		cout << "Not Connected" << endl;
    	}
    	else
    	{
    		cout << "Connected" << endl;
    
    		cout << "Enter Packet To Send" << endl;
    
    		char SendBuffer[512];
    		char SendPacket[512];
    		char RecvPacket[5000];
    		int BytesRecv;
    		int TotalBytesRecv = 0;
    		DWORD RecvWritten;
    		
    		
    		//cin >> SendBuffer;
    		sprintf(SendPacket,"GET /bf3ext_1_2_BETA.exe HTTP/1.1\r\n"
    			                "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
    							"Connection: keep-alive\r\n"
    			               "Host: 127.0.0.1\r\n"
    						   "Connection: keep-alive\r\n\r\n");
    
    		int BytesSent = send(Socket,SendPacket,strlen(SendPacket),0);
    
    		cout << "BytesSent" << " " << BytesSent << endl;
    
    	
    	
    		//cout << RecvPacket << endl;
    		//cout << "BytesRecv" << " " << BytesRecv << endl;
    		HANDLE hFile = 
    			CreateFileA("c:\\bf3ext_1_2_BETA.exe",
    		GENERIC_READ | GENERIC_WRITE,
    		FILE_SHARE_READ | FILE_SHARE_WRITE,
    		NULL,
    		CREATE_ALWAYS,
    		FILE_ATTRIBUTE_NORMAL,
    		NULL);
    
    		while(1)
    		{
    			 memset(&RecvPacket, 0, sizeof(RecvPacket));
    			 BytesRecv = recv(Socket,RecvPacket,sizeof(RecvPacket),0);
    			 cout << "BytesRecv" << " " << BytesRecv << endl;
    			 RecvCount++; //keep count because the first packet has the header
    			 cout << "RecvCount" << " " << RecvCount << endl;
    			 if(BytesRecv > 0)
    			 {
    				 if(RecvCount == 1) //This is the packet we need to cut ,if i didnt do this other packets would be cut
    				 {
    					 MessageBox(NULL,RecvPacket,"",0);
    					 cout << RecvPacket << endl;
    					 cout << strlen(RecvPacket) << endl;
    					 cout << 5000 - strlen(RecvPacket) << endl;
    					 
    					 
    					 //MessageBox(NULL,"Packet To Parse","",0);
    					 char *CutHeaderOff = strstr(RecvPacket,"\r\n\r\n"); //Need to find start of Raw
    					 if(!CutHeaderOff)
    					 {
    						 cout <<"Its NULL" << endl;
    					 }
    					 else
    					 {
    						 cout << CutHeaderOff << endl;
    						 //MessageBox(NULL,CutHeaderOff,"",0);
    						 WriteFile(hFile,CutHeaderOff,sizeof(CutHeaderOff)+4,&RecvWritten,0);
    					 }
    				}
    				else
    				{
    					WriteFile(hFile,RecvPacket,BytesRecv,&RecvWritten,0); //The other packets contain no headers
    				}
    			
    				 cout << "continuing" << endl;
    				 continue;
    			 }
    			 else
    			 {
    				 cout << "TotalBytesRecv" << " " << TotalBytesRecv << endl;
    				 cout << "Breaking" << endl;
    				 closesocket(Socket);
    				 break;
    			 }
    
    		}
    
    	
    		goto Start;
    	
    	}
    
    	return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're getting too fancy dealing with the header.
    Try
    Code:
    				if(RecvCount == 1) //This is the packet that needs to be cut
    					cout << RecvPacket << endl;
    				else
    					WriteFile(hFile,RecvPacket,BytesRecv,&RecvWritten,0); //The other packets contain no headers
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    that dosent fix it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HTTP get with winsock on rapidshare links???
    By Anddos in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2009, 03:21 PM
  2. HTTP Downloads :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 6
    Last Post: 01-13-2003, 11:12 PM
  3. Standard HTTP Characters :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 01-12-2003, 12:21 PM
  4. HTTP Protocol :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 9
    Last Post: 06-21-2002, 03:07 PM
  5. http protocols through winsock
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 02-21-2002, 08:29 AM