Click here to Skip to main content
15,921,530 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: WriteFile / CreateThread Pin
enhzflep5-May-10 1:20
enhzflep5-May-10 1:20 
GeneralRe: WriteFile / CreateThread Pin
Rajesh R Subramanian5-May-10 1:37
professionalRajesh R Subramanian5-May-10 1:37 
GeneralRe: WriteFile / CreateThread Pin
enhzflep5-May-10 1:57
enhzflep5-May-10 1:57 
AnswerRe: Error 87, ERROR_INVALID_PARAMETER , has to do with the volume's sector size. Pin
Software_Developer5-May-10 1:45
Software_Developer5-May-10 1:45 
GeneralRe: Error 87, ERROR_INVALID_PARAMETER , has to do with the volume's sector size. Pin
Richard MacCutchan5-May-10 3:30
mveRichard MacCutchan5-May-10 3:30 
GeneralRe: Error 87, ERROR_INVALID_PARAMETER , has to do with the volume's sector size. Pin
Joe Woodbury5-May-10 18:52
professionalJoe Woodbury5-May-10 18:52 
GeneralRe: Error 87, ERROR_INVALID_PARAMETER , has to do with the volume's sector size. Pin
Richard MacCutchan5-May-10 21:19
mveRichard MacCutchan5-May-10 21:19 
GeneralRe: Error 87, ERROR_INVALID_PARAMETER , has to do with the volume's sector size. Pin
Fareed Rizkalla5-May-10 9:05
Fareed Rizkalla5-May-10 9:05 
DWORD DownloadThread(LPVOID lpdwThreadParam)
{
CDownload* Download = (CDownload*)lpdwThreadParam;

WSADATA WS2Info;
if (!WSAStartup(MAKEWORD(2,2), &WS2Info))
{
SOCKET IPv4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (IPv4 != INVALID_SOCKET)
	{
		hostent *ResolveHost;

		char *HostName = new char[wcslen(Download->HostName)+1];
		wcstombs(HostName, Download->HostName, wcslen(Download->HostName)+1);
		HostName[wcslen(Download->HostName)] = '\0';

		ResolveHost = gethostbyname(HostName);

		if (ResolveHost) // ->h_addr_list[0]
		{
		
			SOCKADDR_IN HostService;
			HostService.sin_family = AF_INET;
			HostService.sin_addr.s_addr=*((unsigned long*)ResolveHost->h_addr); 
			HostService.sin_port = htons(Download->Port);


			if (connect(IPv4, (SOCKADDR*) &HostService, sizeof(HostService)) != SOCKET_ERROR)
			{

				//********************************
				//			HTTP Request
				//********************************


		char *HTTPrequest = new char[DEFAULT_BUFLEN];
		char *RequestURI = new char[wcslen(Download->HostFileLocation)+1];
		wcstombs(RequestURI, Download->HostFileLocation, wcslen(Download->HostFileLocation)+1);
		RequestURI[wcslen(Download->HostFileLocation)] = '\0';
					
		char *Host = new char[wcslen(Download->HostName)+1];
		wcstombs(Host, Download->HostName, wcslen(Download->HostName)+1);
		Host[wcslen(Download->HostName)] = '\0';

		char *UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.9 Safari/533.2";

		sprintf(HTTPrequest,"GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: */*\r\nAccept-Encoding:\r\nRange: bytes=%lld-\r\n\r\n",
		RequestURI, Host, UserAgent, Download->BytesDownloaded);
		send(IPv4,HTTPrequest,strlen(HTTPrequest),0);
					
		delete[] HTTPrequest;
		delete[] RequestURI, Host, UserAgent;

		//********************************
		//			HTTP Response
		//********************************


		char *HTTPresponse = new char[DEFAULT_BUFLEN];
		SecureZeroMemory(HTTPresponse, sizeof(HTTPresponse));

		recv(IPv4, HTTPresponse, sizeof(HTTPresponse), 0);

                ********
                ********  HTTP parsing code
                ********

		//	n-2	|	n-1	|	n	|	n+1	|	n+2
		//	\r	|	\n	|	\r	|	\n	|	PFDB

		int PFDB = 0; // PositionFirstDataByte

		for (int n = 0; n < DEFAULT_BUFLEN; n++)
		{
			if (HTTPresponse[n] == '\r' && HTTPresponse[n-2] == '\r')
			if (HTTPresponse[n-1] == '\n' && HTTPresponse[n+1] == '\n')
				PFDB = n+2;
		}


		DWORD BytesReceived = sizeof(HTTPresponse)-PFDB;

		int SPEED = 512;

		char *Data = new char[SPEED];
		SecureZeroMemory(Data, sizeof(Data));

		// Copy from HTTPresponse to Data Buffer
		for (int n1 = 0, n2 = PFDB; n2 < DEFAULT_BUFLEN; n1++, n2++)
			Data[n1] = HTTPresponse[n2];

		Data[DEFAULT_BUFLEN-PFDB] = '\0';

		delete[] HTTPresponse;

		int DestinationLength = (wcslen(Download->FileLocation)+wcslen(Download->FileName));
		wchar_t *LocalPathPFile = new wchar_t[DestinationLength+1];
		wcscpy(LocalPathPFile, Download->FileLocation);
		wcscat(LocalPathPFile, Download->FileName);
		LocalPathPFile[DestinationLength] = '\0';

		HANDLE File;

		if (Download->BytesDownloaded == 0)
		{
			File = CreateFile(LocalPathPFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
				FILE_FLAG_NO_BUFFERING, NULL)

			if (File == INVALID_HANDLE_VALUE)
				Tu(L"File Error!");
		}
		else
		{
			File = CreateFile(LocalPathPFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
				FILE_FLAG_NO_BUFFERING, NULL);

			if (File == INVALID_HANDLE_VALUE)
				Tu(L"File Error!");
			
			SetFilePointer(File, 0, NULL, FILE_END);
		}

		OVERLAPPED *Struc;

		do
		{
			Download->BytesDownloaded += BytesReceived;
			TotalBytesDownloaded += BytesReceived;
						
			if (!WriteFile(File, Data, BytesReceived, NULL, NULL))
			{
				wchar_t Error[100];
				swprintf(Error, L"Write Error: %d", GetLastError());
				Tu(Error);

				//Tu(L"Write Error!");
			}
						

			BytesReceived = recv(IPv4, Data, sizeof(Data), 0);


		}
		while (Download->DownloadState == Download->ACTIVE
			&& Download->BytesDownloaded < Download->BytesToDownload);

		delete[] Data;

		shutdown(IPv4, SD_BOTH);

		CloseHandle(File);
										

		if (Download->BytesToDownload == Download->BytesDownloaded)
		{
			TotalBytesDownloaded -= Download->BytesToDownload;
			TotalBytesToDownload -= Download->BytesToDownload;
			Download->DownloadState = Download->FINISHED;

		}
		else
		{
			TotalBytesDownloaded -= Download->BytesDownloaded;
			TotalBytesToDownload -= Download->BytesToDownload;
			Download->DownloadState = Download->PAUSED;
		}
	}
      }
    }
    else
	closesocket(IPv4);
    }
WSACleanup();

ExitThread(0);
}

AnswerRe: WriteFile / CreateThread Pin
Joe Woodbury5-May-10 18:43
professionalJoe Woodbury5-May-10 18:43 
QuestionMemory read - write error Pin
İsmail Kurnaz4-May-10 22:33
İsmail Kurnaz4-May-10 22:33 
AnswerRe: Memory read - write error Pin
Code-o-mat4-May-10 22:47
Code-o-mat4-May-10 22:47 
GeneralRe: Memory read - write error Pin
İsmail Kurnaz4-May-10 23:13
İsmail Kurnaz4-May-10 23:13 
GeneralRe: Memory read - write error Pin
Code-o-mat4-May-10 23:18
Code-o-mat4-May-10 23:18 
QuestionIntel Compiler IA-32 11.0.061 installation question Pin
Chesnokov Yuriy4-May-10 20:15
professionalChesnokov Yuriy4-May-10 20:15 
AnswerRe: Intel Compiler IA-32 11.0.061 installation question Pin
CPallini4-May-10 21:17
mveCPallini4-May-10 21:17 
AnswerRe: Intel Compiler IA-32 11.0.061 installation question Pin
Richard MacCutchan4-May-10 21:19
mveRichard MacCutchan4-May-10 21:19 
GeneralRe: Intel Compiler IA-32 11.0.061 installation question Pin
Fareed Rizkalla5-May-10 0:10
Fareed Rizkalla5-May-10 0:10 
GeneralRe: Intel Compiler IA-32 11.0.061 installation question Pin
Richard MacCutchan5-May-10 0:47
mveRichard MacCutchan5-May-10 0:47 
Questionhow to create dialogs in Console application Pin
manoharbalu4-May-10 19:16
manoharbalu4-May-10 19:16 
AnswerRe: how to create dialogs in Console application Pin
Cedric Moonen4-May-10 20:14
Cedric Moonen4-May-10 20:14 
GeneralRe: how to create dialogs in Console application Pin
manoharbalu4-May-10 20:42
manoharbalu4-May-10 20:42 
GeneralRe: how to create dialogs in Console application Pin
Fareed Rizkalla5-May-10 0:11
Fareed Rizkalla5-May-10 0:11 
AnswerRe: how to create dialogs in Console application Pin
Stephen Hewitt4-May-10 20:30
Stephen Hewitt4-May-10 20:30 
GeneralRe: how to create dialogs in Console application Pin
manoharbalu4-May-10 20:46
manoharbalu4-May-10 20:46 
GeneralRe: how to create dialogs in Console application Pin
Software_Developer4-May-10 22:18
Software_Developer4-May-10 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.