3.4.2 具体实现(1)

2013-10-07 15:55:00 · 作者: · 浏览: 85

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. }