|
沙发
楼主 |
发表于 2008-8-12 14:34:44
|
只看该作者
// 準備socket
HTTPSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (HTTPSocket==INVALID_SOCKET)
{
AfxMessageBox("Error when socket");
return 0;
}
//設置超時
struct linger zeroLinger;
zeroLinger.l_onoff = 1;
zeroLinger.l_linger = 0;
if(setsockopt(HTTPSocket,SOL_SOCKET,SO_LINGER
,(const char *)&zeroLinger
,sizeof(zeroLinger))!=0)
{
closesocket(HTTPSocket);
AfxMessageBox("Error when setscokopt(LINGER)");
return 0;
}
//設置接收超時
if(setsockopt(HTTPSocket,SOL_SOCKET,SO_RCVTIMEO
,(const char *)&m_nRecvTimeout
,sizeof(m_nRecvTimeout))!=0)
{
closesocket(HTTPSocket);
AfxMessageBox("Error when setsockopt(RCVTIME).");
return 0;
}
//設置發送超時
if(setsockopt(HTTPSocket,SOL_SOCKET,SO_SNDTIMEO
,(const char *)&m_nSendTimeout
,sizeof(m_nSendTimeout))!=0)
{
closesocket(HTTPSocket);
AfxMessageBox("Error when setsockopt(SNDTIMEO).");
return 0;
}
SocketAddr.sin_addr.s_addr = htonl (INADDR_ANY);
SocketAddr.sin_family=AF_INET;
// 進行端口綁定
if (bind (HTTPSocket,
(const struct sockaddr FAR *)&SocketAddr,
sizeof(SocketAddr))==SOCKET_ERROR)
{
closesocket(HTTPSocket);
AfxMessageBox("Error when bind socket.");
return 0;
}
//準備連接
/// 準備連接信息
BindSocket.sin_addr.s_addr = inet_addr (strHost);
BindSocket.sin_family=AF_INET;
BindSocket.sin_port=htons(HttpPort);
struct fd_set fdSet;
struct timeval tmvTimeout={0L,0L};
FD_ZERO(&fdSet);
FD_SET(HTTPSocket, &fdSet);
if (select(0,&fdSet,NULL,NULL,&tmvTimeout)==SOCKET_ERROR)
{
closesocket(HTTPSocket);
AfxMessageBox("Error when select.");
return 0;
}
// 連接
if (connect(HTTPSocket, (const struct sockaddr *)&BindSocket, sizeof(BindSocket))==SOCKET_ERROR)
{
AfxMessageBox("第一次連接失敗,準備第二次連接");
if (connect(HTTPSocket
,(const struct sockaddr *)&BindSocket
,sizeof(BindSocket))==SOCKET_ERROR)
{
closesocket(HTTPSocket);
AfxMessageBox("連接失敗");
return 0;
}
}
message="連接完成";
return 1;
}
(5)發送http請求,為下載數據進行準備
int CDLAngelDlg::SendHttpHeader()
{
//進行下載
CString temp;
BOOL bReturn;
char tmpBuffer[MAXBLOCKSIZE];
///第1行:方法,請求的路徑,版本
temp="GET "+DownLoadAddress+hostFile+" HTTP/1.0\r";
bReturn=SocketSend(HTTPSocket,temp);
if(!bReturn)
{
message="發送請求失敗";
return 0;
}
///第2行:主機
temp="Host "+strHost+"\r";
bReturn=SocketSend(HTTPSocket,temp);
if(!bReturn)
{
message="發送請求失敗";
return 0;
}
///第3行:接收的數據類型
bReturn=SocketSend(HTTPSocket,"Accept: */*\r");
if(!bReturn)
{
message="發送請求失敗";
return 0;
}
///第4行:
temp=DownLoadAddress;
temp.Insert(0,"Referer ");
temp+="\r";
bReturn=SocketSend(HTTPSocket,temp);
if(!bReturn)
{
message="發送請求失敗";
return 0;
}
///第5行:瀏覽器類型
bReturn=SocketSend(HTTPSocket,"User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt; DTS Agent;)\r");
if(!bReturn)
{
message="發送請求失敗";
return 0;
}
///第6行:連接設置,保持
// SocketSend(HTTPSocket,"Connection:Keep-Alive\r");
///第7行:Cookie.
bReturn=SocketSend(HTTPSocket,"Cache-Control: no-cache\r");
if(!bReturn)
{
message="發送請求失敗";
return 0;
}
bReturn=SocketSend(HTTPSocket,"Proxy-Connection: Keep-Alive\r");
if(!bReturn)
{
message="發送請求失敗";
return 0;
}
/// 續傳
Range是要下載的數據范圍,對續傳很重要
if(continueFlag)
{
temp.Format("Range: bytes=%d- \r",conLength);
bReturn=SocketSend(HTTPSocket,temp);
if(!bReturn)
{
message="發送請求失敗";
return 0;
}
}
///最后一行:空行
bReturn=SocketSend(HTTPSocket,"\r");
if(!bReturn)
{
message="發送請求失敗";
return 0;
}
///取得http頭
int i;
i=GetHttpHeader(HTTPSocket,tmpBuffer);
if(!i)
{
message="獲取HTTP頭出錯";
return 0;
}
//如果取得的http頭含有404等字樣,則表示連接出問題
temp=tmpBuffer;
if(temp.Find("404")!=-1)
{
return 0;
}
// 得到待下載文件的大小
filelength=GetFileLength(tmpBuffer);
return 1;
}
這樣,就連接到網上的另一臺機了,如何下載數據,不用多說了吧
while((num!=SOCKET_ERROR) && (num!=0))
{
num=recv (HTTPSocket
,(char FAR *)tmpBuffer
,(MAXBLOCKSIZE-1)
,0);
file.Write(tmpBuffer,num);
if(ExitFlag)
{
file.Close();
closesocket(HTTPSocket);
DownComplete=1;
m_message.CleanText();
m_message.ShowColorText(RGB(128,128,0),DLCompleteMes);
m_progress.ShowWindow(SW_HIDE);
m_stopDownload.ShowWindow(SW_HIDE);
_endthread();
}
}
基本就是這樣了,本人寫程序水平也不是很高,這個程序還是可以用的。 |
|