linux下载http 文件大小,linux c++下载http文件并显示进度<转>

#include #include#include#include#include#include#include#include#include#include#include#include

using namespacestd;

typedefstring::size_type (string::*find_t)(const string&delim,string::size_type offset) const;

vector Split(const string&s,const string&match,bool removeEmpty=false,bool fullMatch=false)

{

vector result; //return container for tokens

string::size_type start = 0, //starting position for searches

skip = 1; //positions to skip after a match

find_t pfind = &string::find_first_of; //search algorithm for matches

if(fullMatch)

{

skip=match.length();

pfind= &string::find;

}while (start != string::npos)

{string::size_type end = (s.*pfind)(match, start);if (skip == 0) end = string::npos;string token = s.substr(start, end -start);if (!(removeEmpty &&token.empty()))

{

result.push_back(token);

}if ((start = end) != string::npos) start +=skip;

}returnresult;

}void SplitProperty(vector property,string* name,string *value)

{

vector::iterator it=property.begin();if(it!=property.end())

{

name->clear();

name->append(*it);

}

it++;if(it!=property.end())

{

value->clear();

value->append(*it);

}

}int GetFileSize(char* host,char* file,string* error,int&headersize)

{int size=-1;structsockaddr_in servaddr;struct hostent *hp;string splitline="";stringPName;stringPValue;string splittagbalue=":";stringinfo;

vectorproperties;

vectorproperty;intsock_id;char message[1024*1024] = {0};intmsglen;stringrequest;

request.append("HEAD");

request.append(file);

request.append("HTTP/1.1");

request.append("Host:");

request.append(host);

request.append("");//Get a socket

if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

error->append("Couldn't get a socket!");returnsize;

}//book uses bzero which my man pages say is deprecated//the man page said to use memset instead. :-)

memset(&servaddr,0,sizeof(servaddr));//get address for google.com

if((hp = gethostbyname(host)) ==NULL) {

error->append("Couldn't access network.");

error->append(host);returnsize;

}//bcopy is deprecated also, using memcpy instead

memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length);//fill int port number and type

servaddr.sin_port = htons(80);

servaddr.sin_family=AF_INET;//make the connection

if(connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {

error->append("Couldn't connect!");returnsize;

}

write(sock_id,request.c_str(),request.length());//read the response

msglen = read(sock_id,message,1024*1024);

headersize=msglen;

info.append(message,0,msglen);

close(sock_id);

properties=Split(info,splitline,true);

vector::iterator it;for (it=properties.begin(); it

{

property= Split(*it,splittagbalue,true);

SplitProperty(property,&PName,&PValue);if(PName=="Content-Length")

{

size=atoi(PValue.c_str());break;

}

}if(size==-1)

{

error->append("Resource Not Found!");

}returnsize;

}void DownloadFile(char* host,char* file,char * savefile,float size,int& progress,inthsize)

{structsockaddr_in servaddr;struct hostent *hp;stringinfo;intsock_id;char message[18000] = {0};char messagetop[18000]={0};intmsglen;float readcount=0;stringrequest;

request.append("GET");

request.append(file);

request.append("HTTP/1.1");

request.append("Host:");

request.append(host);

request.append("");//Get a socket

if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) == -1) {return;

}//book uses bzero which my man pages say is deprecated//the man page said to use memset instead. :-)

memset(&servaddr,0,sizeof(servaddr));//get address for google.com

if((hp = gethostbyname(host)) ==NULL) {return;

}//bcopy is deprecated also, using memcpy instead

memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length);//fill int port number and type

servaddr.sin_port = htons(80);

servaddr.sin_family=AF_INET;//make the connection

if(connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {return;

}//NOW THE HTTP PART!!!//send the request

write(sock_id,request.c_str(),request.length());

ofstream outfile (savefile,ofstream::binary);do{

msglen= read(sock_id,message,18000);if(msglen==0)break;if(readcount==0)

{int tempindex=0;for(int i =hsize-1;i

{

messagetop[tempindex]=message[i];

tempindex=tempindex+1;

}

outfile.write (messagetop,tempindex);

}else{

outfile.write (message,msglen);

}

readcount=readcount+msglen;

progress=readcount/size*100;

}while(readcount<=(size+hsize));

outfile.close();

close(sock_id);//read the response

}floatfilesize;int loadprogress=0;inthsize;

pthread_t tUpdateWork;void* UpdateWorCoping(void*data)

{

DownloadFile("192.168.2.128","/games.data","/usr/games.data",filesize,loadprogress,hsize);

}int main(int argc, char**argv)

{stringerror;

filesize= GetFileSize("192.168.2.128","/games.data",&error,hsize);

pthread_create(&tUpdateWork,NULL,UpdateWorCoping,0);while(loadprogress<99)

{

printf("Progress %d", loadprogress);

usleep(1000000);

}return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值