📄 myclientsocketthread.cpp
字号:
#include "stdafx.h"
#include "MyClientSocketThread.h"
#include "MyClientSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//
// constructor
//
CMyClientSocketThread::CMyClientSocketThread(
HWND hwndMsgTarget,
UINT uiMsgEndThread,
const CString& strUrl,
UINT uiPort)
: m_pThread(NULL)
, m_pSocket(NULL)
, m_hwndMsgTarget(hwndMsgTarget)
, m_uiMsgEndThread(uiMsgEndThread)
, m_strUrl(strUrl)
, m_uiPort(uiPort)
, m_strAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
, m_errInfo()
{
m_pSocket = new CMyClientSocket;
m_pThread = AfxBeginThread(myThreadProc, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
m_pThread->m_bAutoDelete = FALSE;
m_pThread->ResumeThread();
}
CMyClientSocketThread::~CMyClientSocketThread()
{
delete m_pSocket;
m_pSocket = NULL;
delete m_pThread;
m_pThread = NULL;
}
UINT CMyClientSocketThread::myThreadProc(LPVOID pParam)
{
CMyClientSocketThread* pThis = reinterpret_cast<CMyClientSocketThread*>(pParam);
// 僗儗僢僪偛偲偵 MFC socket 偺弶婜壔偑昁梫
if (AfxSocketInit()) {
// 捠怣僗儗僢僪杮懱
pThis->myThreadProcSub();
}
::PostMessage(pThis->m_hwndMsgTarget, pThis->m_uiMsgEndThread, 0, 0);
// 偙傟仾傪億僗僩偟偨屻偼丄儊僀儞僗儗僢僪偵傛偭偰杮僆僽僕僃僋僩偑嶍彍偝傟偰
// 偄傞壜擻惈偑偁傞偨傔丄埲崀丄pThis偺儊儞僶曄悢偵偼堦愗傾僋僙僗偟側偄偙偲両
return 0;
}
bool CMyClientSocketThread::myThreadProcSub()
{
CUrl url;
if (!url.CrackUrl(m_strUrl)) {
m_errInfo = CErrInfo(CErrInfo::errInvalidUrl);
return false;
}
CString strHostName = url.GetHostName();
CString strUrlPath = url.GetUrlPath();
CString strUrlExtra;
if (strUrlPath.IsEmpty()) {
strUrlPath = "/";
} else {
strUrlExtra = url.GetExtraInfo();
}
if (!m_pSocket->Create()) {
m_errInfo = CErrInfo(CErrInfo::errCreateSocket);
return false;
}
const bool bSuccess = httpGet(strHostName, strUrlPath, strUrlExtra);
if (!bSuccess) {
m_errInfo = *m_pSocket->GetErrInfo();
}
m_pSocket->ShutDown();
m_pSocket->Close();
return bSuccess;
}
bool CMyClientSocketThread::httpGet(
const CString& strHostName,
const CString& strUrlPath,
const CString& strUrlExtra)
{
//
// 僒乕僶乕偵愙懕
//
if (!m_pSocket->SyncConnect(strHostName, m_uiPort)) {
return false;
}
//
// 儕僋僄僗僩偺憲怣
//
static const char szRequest[] =
"GET %s HTTP/1.0\r\n"
"Accept: */*\r\n"
"Accept-Language: ja\r\n"
"User-Agent: %s\r\n"
"Host: %s\r\n"
"\r\n"
;
CString strSendData;
strSendData.Format(szRequest,
(LPCTSTR)(strUrlPath + strUrlExtra),
(LPCTSTR)m_strAgent,
(LPCTSTR)strHostName);
if (!m_pSocket->SyncSend((LPCTSTR)strSendData, strSendData.GetLength())) {
return false;
}
//
// 儗僗億儞僗偺庴怣
//
if (!m_pSocket->SyncReceive()) {
return false;
}
return true;
}
int CMyClientSocketThread::GetReceivedBytes()
{
return m_pSocket->GetReceivedBytes();
}
void CMyClientSocketThread::SetCancel()
{
m_pSocket->SetCancel();
}
bool CMyClientSocketThread::IsCanceled() const
{
// 杮僗儗僢僪廔椆屻偵偟偐屇偽傟側偄乮偮傑傝儊僀儞僗儗僢僪偐傜偟偐屇偽傟側偄乯
ASSERT(::WaitForSingleObject(m_pThread->m_hThread, 0) == WAIT_OBJECT_0);
return m_pSocket->IsCanceled();
}
const CErrInfo* CMyClientSocketThread::GetErrInfo() const
{
// 杮僗儗僢僪廔椆屻偵偟偐屇偽傟側偄乮偮傑傝儊僀儞僗儗僢僪偐傜偟偐屇偽傟側偄乯
ASSERT(::WaitForSingleObject(m_pThread->m_hThread, 0) == WAIT_OBJECT_0);
return &m_errInfo;
}
void CMyClientSocketThread::GetRecvData(CByteArray& recvData) const
{
// 杮僗儗僢僪廔椆屻偵偟偐屇偽傟側偄乮偮傑傝儊僀儞僗儗僢僪偐傜偟偐屇偽傟側偄乯
ASSERT(::WaitForSingleObject(m_pThread->m_hThread, 0) == WAIT_OBJECT_0);
m_pSocket->RetrieveRecvData(recvData);
}
//
// 捠怣僗儗僢僪廔椆傑偱懸偮
//
void CMyClientSocketThread::WaitForEndThread() const
{
::WaitForSingleObject(m_pThread->m_hThread, INFINITE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -