#include <iostream>
using namespace std;
#define MAXBLOCKSIZE 1024
#include <WinSock2.h>
#include <windows.h>
#include <wininet.h>
#include <string>
#include <fstream>
#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
#pragma comment(lib, "Wininet.lib")
#pragma comment(lib, "Ws2_32.lib")
bool downLoadFile(const TCHAR *Url, const TCHAR *ptFilePath)
{
bool bret = false;
HINTERNET hSession = InternetOpen(TEXT("RookIE/1.0"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET handle2 = InternetOpenUrl(hSession, Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (handle2 != NULL)
{
byte Temp[MAXBLOCKSIZE] = {0};
ULONG Number = 1;
DWORD dwBytesWritten ;
HANDLE hFile;
cout<<"开始下载文件..."<<endl;
if (INVALID_HANDLE_VALUE !=
(hFile = CreateFile (ptFilePath, GENERIC_WRITE, 0,
NULL, CREATE_ALWAYS, 0, NULL)))
{
while (Number > 0)
{
BOOL bRet = InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);
WriteFile (hFile, Temp, Number * sizeof (byte),
&dwBytesWritten, NULL);
}
CloseHandle(hFile);
bret = true;
}
else
cout<<"下载文件失败..."<<endl;
cout<<"下载文件结束..."<<endl;
InternetCloseHandle(handle2);
handle2 = NULL;
}
else
cout<<"打开下载地址失败"<<endl;
InternetCloseHandle(hSession);
hSession = NULL;
}
else
cout<<"打开本地浏览器失败"<<endl;
return bret;
}
bool downTcpFile(const std::string &strurl, const std::string &strfilepath)
{
bool bret = false;
std::string strip("");
int nport = 0;
std::string strsenddata("");
if (!strurl.empty() && !strfilepath.empty())
{
std::string strsuburl(""), strhost(""), strip(""), strport("80");
int npos = strurl.find("http://");
if (npos != -1)
strhost = strurl.substr(npos + strlen("http://"));
else {
cout<<"ipmap.txt下载地址http非法"<<endl;
return bret;
}
npos = strhost.find("/");
if (npos != -1) {
strsuburl = strhost.substr(npos);
strhost = strhost.substr(0, npos);
} else {
cout<<"ipmap.txt下载地址子域名非法"<<endl;
return bret;
}
npos = strhost.find(":");
if (npos != -1) {
strip = strhost.substr(0, npos);
strport = strhost.substr(npos + 1);
}else
strip = strhost;
char szbuf[1024] = {0};
sprintf_s(szbuf, 1024, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", strsuburl.c_str(), strhost.c_str());
SOCKET Socket;
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if( Socket == INVALID_SOCKET )
{
cout<<"socket 报错"<<endl;
return bret;
}
int nTvlen = 2000;
setsockopt(Socket,SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTvlen,sizeof(nTvlen));
setsockopt(Socket,SOL_SOCKET, SO_SNDTIMEO, (const char*)&nTvlen,sizeof(nTvlen));
cout<<"下载模块:开始下载..."<<endl;
try
{
struct hostent *host;
host = gethostbyname(strip.c_str());
if (host != NULL)
{
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(atoi(strport.c_str()));
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
closesocket(Socket);
cout<<"下载模块:无法连接服务器"<<endl;
return bret;
}
send(Socket, szbuf,strlen(szbuf), 0);
std::string strdata("");
int nrecv = 0;
do
{
char szdata[4096] = {0};
nrecv = recv(Socket, szdata, 4095, 0);
if (nrecv > 0)
strdata.append(szdata, nrecv);
} while (nrecv > 0);
if (!strdata.empty())
{
//这里需要你字节过滤下接收到的数据
std::fstream ffile;
ffile.open(strfilepath, std::ios_base::out);
if (ffile.is_open())
{
ffile<< strdata;
ffile.close();
}
cout<<"下载成功"<<endl;
bret = true;
}
else
cout<<"下载模块:获取的数据为空"<<endl;
}
else
cout<<""<<endl;
closesocket(Socket);
} catch (...) {
cout<<"下载模块:发送数据发生异常"<<endl;
}
}
else
cout<<"下载模块:下载地址或存放路径为空"<<endl;
return bret;
}
int main(int argc, char *argv[])
{
//使用windows中的internet系列函数实现
TCHAR *ptUrl = TEXT("http://cdn1.908uc.com/ipmap.txt");
TCHAR ptTextPath[256] = {0};
GetModuleFileName(NULL, ptTextPath, MAX_PATH);
PathRemoveFileSpec(ptTextPath);
lstrcat(ptTextPath, TEXT("\\internet_test.txt"));
downLoadFile(ptUrl, ptTextPath);
//使用windows中的socket函数实现
WSADATA wsd;
WORD sockVersion = MAKEWORD(2, 2);
WSAStartup(sockVersion, &wsd);
std::string strurl = "http://cdn1.908uc.com/ipmap.txt";
char szpath[256] = {0};
GetModuleFileNameA(NULL, szpath, MAX_PATH);
PathRemoveFileSpecA(szpath);
strcat(szpath, "\\socket_test.txt");
downTcpFile(strurl, szpath);
WSACleanup();
system("pause");
return 0;
}
评论1
最新资源