谁能帮助我解释一下这段程序:
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;
}