謝欣倫 - OpenDev原創教程 - 客戶端套接字類CxClientSocket

  這是一個精練的客戶端套接字類,類名、函數名和變量名均採用匈牙利命名法。小寫的x表明個人姓氏首字母(謝欣倫),我的習慣而已,若有雷同,純屬巧合。函數

CxClientSocket的使用以下(以某個叫作CSomeClass的類的相關代碼爲例):this

1、聲明客戶端套接字對象實例。spa

CxClientSocket m_csRemoteHost;

2、設置客戶端套接字事件通知方式。消息響應或回調函數,任選其一。客戶端套接字事件有:緩衝區有數據可讀取、與遠程主機的鏈接被斷開。指針

void CSomeClass::PreSubclassWindow() 
{
    // TODO: Add your specialized code here and/or call the base class
    HWND hwnd = GetSafeHwnd();
    MSG msg = {hwnd, WM_SOCKET_EVENT};
    m_usRemoteHost.SetWindowMessage(&msg);
    m_csRemoteHost.SetWindowMessage(&msg);     m_ssLocalHost.SetWindowMessage(&msg);

    if (m_btCSMode == CSM_SERVER)
        ListenRemoteHost();

    CStatic::PreSubclassWindow();
}

3、鏈接遠程主機。code

BOOL CSomeClass::ConnectToRemoteHost()
{
    if (m_csRemoteHost.IsConnected())
        m_csRemoteHost.DisConnect();

    BOOL bRet = m_csRemoteHost.Connect(m_ipapRemoteHost.szAddr,
                                        m_ipapRemoteHost.nPort);

    return bRet;
}

4、處理消息響應或函數回調。
1.消息響應對象

BEGIN_MESSAGE_MAP(CSomeClass, CStatic)
    //{{AFX_MSG_MAP(CSomeClass)
    ON_WM_DESTROY()
    //}}AFX_MSG_MAP
    ON_MESSAGE(WM_SOCKET_EVENT, OnSocketEvent)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSomeClass message handlers

LRESULT CSomeClass::OnSocketEvent(WPARAM wParam, LPARAM lParam)
{
    if (WSAGETSELECTERROR(lParam) != 0)
        return 1;
    
    int nEvent = WSAGETSELECTEVENT(lParam);
    switch (nEvent)
    {
      case FD_ACCEPT: OnSocketAccept(wParam); break;
      
case FD_CLOSE: OnSocketClose(wParam); break;
      
case FD_READ: OnSocketRead(wParam); break;
  
default: break; } return 1; }
void
CSomeClass::OnSocketRead(WPARAM wParam) { if (m_csRemoteHost.GetSocket() == (SOCKET)wParam) { m_csRemoteHost.SelectEvent(FD_CLOSE); SOME_DATA_STRUCT data = {0}; if (m_csRemoteHost.Recv((LPBYTE)&data, sizeof(data))) {
      //do something with data
;
     } m_csRemoteHost.SelectEvent(FD_READ
| FD_CLOSE); } } void CSomeClass::OnSocketClose(WPARAM wParam) { if (m_csRemoteHost.GetSocket() == (SOCKET)wParam) { m_csRemoteHost.DisConnect(); char szDebug[MAX_PATH]; sprintf(szDebug, "%s disconnect.\n", m_szRemoteHostName); OutputDebugString(szDebug); GetParent()->PostMessage(WM_SOCKET_EVENT, (WPARAM)this, FD_CLOSE); } }

2.回調函數blog

LRESULT CALLBACK CSomeClass::SocketProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    LRESULT lRet = 0;
    CSomeClass* pThis = (CSomeClass*)hwnd;
    if (pThis != 0)
        lRet = pThis->OnSocketEvent(wParam, lParam);

    return lRet;
}

注意,在使用回調函數時應設置客戶端套接字事件通知方式爲回調函數,而且設法把this指針傳入設置函數,方便在回調函數中調用成員函數。事件

MSG msg = {(HWND)this, WM_SOCKET_EVENT};
m_csRemoteHost.SetWindowMessage(&msg, SocketProc);

5、斷開與遠程主機的鏈接。ip

void CSomeClass::OnDestroy() 
{
    CStatic::OnDestroy();
    
    // TODO: Add your message handler code here
    TerminateWaitComm();

    m_csRemoteHost.DisConnect();
    m_ssLocalHost.DisListen();
    m_usRemoteHost.Disbind();
}

  至於客戶端套接字數據發送,很簡單:在須要處調用如下函數:ci

m_csRemoteHost.Send((LPBYTE)pData, sizeof(SOME_DATA_STRUCT));

  精練的代碼不須要過多解釋,大家懂的。To be continued...

下載

libComm - v1.2 For WinXP/Win7

相關文章
相關標籤/搜索