博客日历
2025年06月 | ||||||
一 | 二 | 三 | 四 | 五 | 六 | 七 |
26 | 27 | 28 | 29 | 30 | 31 | 1 |
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
存档
2025年03月 2024年
03月 04月 05月 2021年
01月 02月 11月 12月 2020年
02月 03月 04月 05月 06月 07月
09月 2018年
09月 2017年
01月 02月 07月 2016年
01月 04月 07月 08月 11月 12月
2015年
01月 02月 03月 05月 09月 10月
11月 2014年
01月 02月 03月 04月 05月 06月
07月 08月 09月 10月 11月 12月
2013年
01月 02月 03月 04月 05月 06月
07月 08月 09月 10月 11月 12月
2012年
01月 02月 03月 04月 05月 06月
07月 08月 09月 10月 11月 12月
2011年
01月 02月 03月 04月 05月 06月
07月 08月 09月 10月 11月 12月
2010年
01月 02月 03月 04月 05月 06月
07月 08月 09月 10月 11月 12月
2009年
03月 04月 05月 06月 07月 08月
09月 10月 11月 12月
CAsyncSocket的一个简单应用
对于任意的类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");
}
上一篇: VBScript 编程控制 Photoshop
下一篇: 将迅雷狗狗搜索添加到Google搜索工具栏上的方法
分类:Win32/C++ 查看次数:10002 发布时间:2009/4/22 21:37:15