3.4.2 具体实现(1)
1. 窗体IDD_HOST
(1) 首先设计窗体IDD_HOST,设置其Caption属性的值为"Telnet服务器",如图3-14所示
|
| 图3-14 窗体IDD_HOST |
(2) 为此窗体添加实现代码,首先在文件ClientSocket.h中定义需要的类和函数。具体实现代码如下:
- class CTelnetView; //定义类CTelnetView
- class CClientSocket : public CAsyncSocket
- {
- // 属性
- public:
-
- public:
- CClientSocket(CTelnetView *cView); //类CClientSocket的构造函数
- virtual ~CClientSocket();
- // 虚函数
- public:
- CTelnetView *cView;
- //{{AFX_VIRTUAL(CClientSocket)
- public:
- virtual void OnClose(int nErrorCode); //声明关闭连接方法
- virtual void OnConnect(int nErrorCode); //声明连接方法
- virtual void OnOutOfBandData(int nErrorCode); //声明带外数据处理方法
- virtual void OnReceive(int nErrorCode); //声明接收数据的方法
- virtual void OnSend(int nErrorCode); //声明发送数据的方法
- //}}AFX_VIRTUAL
- protected:
- };
(3) 在文件ClientSocket.cpp中,实现了文件ClientSocket.h中定义的各个方法。具体代码如下: - //定义关闭连接方法
- void CClientSocket::OnClose(int nErrorCode)
- {
- CAsyncSocket::OnClose(nErrorCode);
- if(!IsWindow(cView->m_hWnd)) return;
- if(!IsWindowVisible(cView->m_hWnd)) return;
- cView->GetDocument()->OnCloseDocument();
-
- }
- //定义连接方法
- void CClientSocket::OnConnect(int nErrorCode)
- {
- CAsyncSocket::OnConnect(nErrorCode);
- }
- //定义带外数据处理方法
- void CClientSocket::OnOutOfBandData(int nErrorCode)
- {
- ASSERT(FALSE); //Telnet should not have OOB data
- CAsyncSocket::OnOutOfBandData(nErrorCode);
- }
-
- //定义接收数据方法
- void CClientSocket::OnReceive(int nErrorCode)
- {
- cView->ProcessMessage(this);
- }
-
- //定义发送数据方法
- void CClientSocket::OnSend(int nErrorCode)
- {
- CAsyncSocket::OnSend(nErrorCode);
- }