对于任意的类A,都只需要继承自CClientSocketEvent,并实现CClientSocketEvent类中的 
virtual void OnClose(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnReceive(int nErrorCode);

这三个方法即可简单方便的使用CAsyncSocket类。

类A就可以通过CClientSocketEvent的成员m_pClientSocket来进行Socket操作。
示例代码片段:

//CMainDlg同时继承CDialog和CClientSocketEvent
class CMainDlg : public CDialog,CClientSocketEvent
{...}

//在CMainDlg的方法中可以这样开始连接服务器
m_pClientSocket->StartConnect("www.baidu.com",80);

完整的源代码如下:


//ClientSocket.h头文件   
#pragma once   
 
class CClientSocketEvent;   
class CClientSocket : public CAsyncSocket   
{   
public:   
    CClientSocket(CClientSocketEvent* _pClientSocketEvent);   
    virtual ~CClientSocket();   
    virtual void OnClose(int nErrorCode);   
    virtual void OnConnect(int nErrorCode);   
    virtual void OnReceive(int nErrorCode);   
private:   
    CClientSocketEvent* m_pClientSocketEvent;   
    static BOOL m_bInitSocket;   
    BOOL m_bCreate;   
public:   
 
    /**  
    * 启动来连接指定的服务器  
    */  
    BOOL StartConnect(LPCTSTR lpszHostAddress,UINT nHostPort);   
 
    /**  
    * 关闭Socket    
    */  
    void CloseSocket(void);   
};   
 

// ClientSocket.cpp : 实现文件   
 
#include "stdafx.h"   
#include "ClientSocket.h"   
#include "ClientSocketEvent.h"   
 
BOOL CClientSocket::m_bInitSocket=FALSE;   
 
CClientSocket::CClientSocket(CClientSocketEvent* _pClientSocketEvent)   
{   
    ASSERT(_pClientSocketEvent!=NULL);   
    m_pClientSocketEvent=_pClientSocketEvent;   
    m_bCreate=FALSE;   
}   
 
CClientSocket::~CClientSocket()   
{   
}   
 
void CClientSocket::OnClose(int nErrorCode)   
{   
    m_pClientSocketEvent->OnClose(nErrorCode);   
}   
 
void CClientSocket::OnConnect(int nErrorCode)   
{   
    m_pClientSocketEvent->OnConnect(nErrorCode);   
}   
 
void CClientSocket::OnReceive(int nErrorCode)   
{   
    m_pClientSocketEvent->OnReceive(nErrorCode);   
}   
 
BOOL CClientSocket::StartConnect(LPCTSTR lpszHostAddress,UINT nHostPort)   
{   
    if (!CClientSocket::m_bInitSocket)   
    {   
        //在整个应用程序中,这里应该只会执行一次。   
        CClientSocket::m_bInitSocket = AfxSocketInit();   
        if (!CClientSocket::m_bInitSocket)   
        {   
            return FALSE;   
        }   
    }   
 
    if (!m_bCreate)   
    {   
        m_bCreate=Create();   
        //TRACE("Create();\n");   
        if (!m_bCreate)   
        {   
            return FALSE;   
        }   
    }   
 
    LINGER Linger;   
    Linger.l_linger=0;   
    Linger.l_onoff=0;   
    SetSockOpt(SO_LINGER,&Linger,sizeof(LINGER));   
 
    if (!Connect(lpszHostAddress,nHostPort))   
    {   
        return FALSE;   
    }   
    return TRUE;   
}   
 
void CClientSocket::CloseSocket(void)   
{   
    ShutDown(2);   
    Close();   
    m_bCreate=FALSE;   
}   
 
 

//ClientSocketEvent.h头文件   
#pragma once   
#include "ClientSocket.h"   
 
/**  
*异步Socket\n  
*任意窗体,只要继承自该类(如同时继承本类和CWnd),即可通过重写OnClose等函数\n  
*来响应Socket。通过成员m_pClientSocket,可以对Socket进行控制  
*    
*/  
class CClientSocketEvent   
{   
public:   
    CClientSocketEvent(void);   
    ~CClientSocketEvent(void);   
    virtual void OnClose(int nErrorCode);   
    virtual void OnConnect(int nErrorCode);   
    virtual void OnReceive(int nErrorCode);   
public:   
    CClientSocket* m_pClientSocket;   
};   
 
 

//ClientSocketEvent.cpp实现文件   
#include "StdAfx.h"   
#include "ClientSocket.h"   
#include "ClientSocketEvent.h"   
 
CClientSocketEvent::CClientSocketEvent(void)   
{   
    //TRACE("CClientSocketEvent::CClientSocketEvent(void)\n");   
    m_pClientSocket=new CClientSocket(this);   
}   
 
CClientSocketEvent::~CClientSocketEvent(void)   
{   
    //TRACE("CClientSocketEvent::~CClientSocketEvent(void)\n");   
    delete m_pClientSocket;   
    m_pClientSocket=NULL;   
}   
 
void CClientSocketEvent::OnClose(int nErrorCode)   
{   
    //TRACE("CClientSocketEvent::OnClose(int nErrorCode)\n");   
}   
 
void CClientSocketEvent::OnConnect(int nErrorCode)   
{   
    //TRACE("CClientSocketEvent::OnConnect(int nErrorCode)\n");   
}   
 
void CClientSocketEvent::OnReceive(int nErrorCode)   
{   
    //TRACE("CClientSocketEvent::OnReceive(int nErrorCode)\n");   
}   
 

本文链接地址: CAsyncSocket的一个简单应用
http://qingfengju.com/index.asp?id=8

分类:Win32/C++ 查看次数:8495 发布时间:2009-4-22 21:37:15