Jump to content
  • Advertisement
Sign in to follow this  
Anddos

Need help with custom http downloader

This topic is 3037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having a problem with cutting off the \r\n\r\n off the header packet and writing that to a file , notepads file size is 189kb but only 185kb is written to the file.
#include <iostream>
#include <winsock.h>
#include <stdio.h>
#pragma comment( lib, "ws2_32.lib" ) 
using namespace std;

int main()
{
	cout << "Http Sender 1.0" << endl;
	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;

	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;
	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 /notepad.exe HTTP/1.1\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 = 
			CreateFileW(L"Notepad-downloaded.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
				 {
					 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;
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!