设为首页 加入收藏

TOP

3.4.2 具体实现(1)
2013-10-07 15:55:00 来源: 作者: 【 】 浏览:84
Tags:3.4.2 具体 实现

3.4.2  具体实现(1)

1. 窗体IDD_HOST

(1) 首先设计窗体IDD_HOST,设置其Caption属性的值为"Telnet服务器",如图3-14所示

 
图3-14  窗体IDD_HOST
(2) 为此窗体添加实现代码,首先在文件ClientSocket.h中定义需要的类和函数。具体实现代码如下:
  1. class CTelnetView;                  //定义类CTelnetView  
  2. class CClientSocket : public CAsyncSocket  
  3. {  
  4. // 属性  
  5. public:  
  6.  
  7. public:  
  8. CClientSocket(CTelnetView *cView);      //类CClientSocket的构造函数  
  9. virtual ~CClientSocket();  
  10. // 虚函数  
  11. public:  
  12. CTelnetView *cView;  
  13. //{{AFX_VIRTUAL(CClientSocket)  
  14. public:  
  15. virtual void OnClose(int nErrorCode);               //声明关闭连接方法  
  16. virtual void OnConnect(int nErrorCode);                 //声明连接方法  
  17. virtual void OnOutOfBandData(int nErrorCode);       //声明带外数据处理方法  
  18. virtual void OnReceive(int nErrorCode);                 //声明接收数据的方法  
  19. virtual void OnSend(int nErrorCode);                    //声明发送数据的方法  
  20. //}}AFX_VIRTUAL  
  21. protected:  
  22. };  

(3) 在文件ClientSocket.cpp中,实现了文件ClientSocket.h中定义的各个方法。具体代码如下:
  1. //定义关闭连接方法  
  2. void CClientSocket::OnClose(int nErrorCode)   
  3. {  
  4. CAsyncSocket::OnClose(nErrorCode);  
  5. if(!IsWindow(cView->m_hWnd)) return;  
  6. if(!IsWindowVisible(cView->m_hWnd)) return;  
  7. cView->GetDocument()->OnCloseDocument();  
  8.  
  9. }  
  10. //定义连接方法  
  11. void CClientSocket::OnConnect(int nErrorCode)   
  12. {  
  13. CAsyncSocket::OnConnect(nErrorCode);  
  14. }  
  15. //定义带外数据处理方法  
  16. void CClientSocket::OnOutOfBandData(int nErrorCode)   
  17. {  
  18. ASSERT(FALSE); //Telnet should not have OOB data  
  19. CAsyncSocket::OnOutOfBandData(nErrorCode);  
  20. }  
  21.  
  22. //定义接收数据方法  
  23. void CClientSocket::OnReceive(int nErrorCode)   
  24. {  
  25. cView->ProcessMessage(this);  
  26. }  
  27.  
  28. //定义发送数据方法  
  29. void CClientSocket::OnSend(int nErrorCode)   
  30. {  
  31. CAsyncSocket::OnSend(nErrorCode);  
  32. }  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇3.4.1 规划类 下一篇3.4.2 具体实现(2)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++中智能指针的性能 (2025-12-25 03:49:29)
·如何用智能指针实现c (2025-12-25 03:49:27)
·如何在 C 语言中管理 (2025-12-25 03:20:14)
·C语言和内存管理有什 (2025-12-25 03:20:11)
·为什么C语言从不被淘 (2025-12-25 03:20:08)