| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 463 人关注过本帖
标题:[求助]一段网络编程程序
取消只看楼主 加入收藏
色者
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-6-5
收藏
 问题点数:0 回复次数:0 
[求助]一段网络编程程序

谁能帮助我解释一下这段程序:
unsigned int WINAPI getandsave(void* param){

char* url = (char*)param;

if( strlen(url)<=0 ||strlen(url)>254){

return false;
}

char filename[128];
for(int i=strlen(url);i>0;i--){
if(url[i-1]=='/')
break;
}
memset(filename,0,128);
memcpy(filename,url+i,strlen(url)-i);

while( g_Threadnum>20 )
::Sleep(20);
InterlockedIncrement(&g_Threadnum);

struct sockaddr_in sin;
SOCKET sClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sClient==INVALID_SOCKET)
{
printf("Socket Error !\r\n");
InterlockedDecrement(&g_Threadnum);
return false;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(80);
sin.sin_addr.S_un.S_addr = inet_addr("61.131.68.12");

if( connect(sClient,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR){
printf("Connect to [%s] error, errorcode=%d.", "61.131.68.12",GetLastError());
InterlockedDecrement(&g_Threadnum);
return false;
}

const int MLEN=192;
char message[MLEN];

strcpy(message,"GET ");
if(url[0]=='/')
strcat(message,"http://www.yahoo.com.cn");
strcat(message,url);
strcat(message," HTTP/1.0\r\n");


send(sClient,message,strlen(message),0);

strcpy(message,"accept: www/source; text/html; image/jpeg; */*\r\n");
send(sClient,message,strlen(message),0);

strcpy(message,"\r\n");
send(sClient,message,strlen(message),0);

char rec[MLEN];
recvline(sClient,rec);
if( strcmp(rec,"HTTP/1.1 200 OK")){
printf("getfile [%s] head error.\r\n",url);
InterlockedDecrement(&g_Threadnum);
return false;
}

int mlen=0;
while( recvline(sClient,rec) ){
if( strlen(rec)<=0 )
break;
if(strstr(rec,"Content-Length:")==rec){
char length[32];
memset(length,0,32);
memcpy(length,rec+15,strlen(rec)-15);
mlen=atoi(length);
}
}

char* data = new char[mlen];
int len=0;

while(len<mlen){
int add=::recv(sClient,data+len,mlen-len,0);
len += add;
}

savetodisk(filename,data,mlen);

closesocket(sClient);

printf("savefile \"%s\" OK.\r\n", url);

InterlockedDecrement(&g_Threadnum);

delete []url;
return 0;
}

搜索更多相关主题的帖子: 网络 
2007-06-08 01:53
快速回复:[求助]一段网络编程程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033276 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved