ChatSvrDlg.cpp
上传用户:qzzxgm
上传日期:2009-12-14
资源大小:1882k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // ChatSvrDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ChatSvr.h"
  5. #include "ChatSvrDlg.h"
  6. #include "ClientSocket.h"
  7. #include "ListeningSocket.h"
  8. #include "InitDlg.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16. class CAboutDlg : public CDialog
  17. {
  18. public:
  19. CAboutDlg();
  20. // Dialog Data
  21. //{{AFX_DATA(CAboutDlg)
  22. enum { IDD = IDD_ABOUTBOX };
  23. //}}AFX_DATA
  24. // ClassWizard generated virtual function overrides
  25. //{{AFX_VIRTUAL(CAboutDlg)
  26. protected:
  27. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  28. //}}AFX_VIRTUAL
  29. // Implementation
  30. protected:
  31. //{{AFX_MSG(CAboutDlg)
  32. //}}AFX_MSG
  33. DECLARE_MESSAGE_MAP()
  34. };
  35. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  36. {
  37. //{{AFX_DATA_INIT(CAboutDlg)
  38. //}}AFX_DATA_INIT
  39. }
  40. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  41. {
  42. CDialog::DoDataExchange(pDX);
  43. //{{AFX_DATA_MAP(CAboutDlg)
  44. //}}AFX_DATA_MAP
  45. }
  46. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  47. //{{AFX_MSG_MAP(CAboutDlg)
  48. // No message handlers
  49. //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CChatSvrDlg dialog
  53. CChatSvrDlg::CChatSvrDlg(CWnd* pParent /*=NULL*/)
  54. : CDialog(CChatSvrDlg::IDD, pParent)
  55. {
  56. //{{AFX_DATA_INIT(CChatSvrDlg)
  57. // NOTE: the ClassWizard will add member initialization here
  58. //}}AFX_DATA_INIT
  59. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  60. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  61. m_pSocket = NULL;
  62. }
  63. void CChatSvrDlg::DoDataExchange(CDataExchange* pDX)
  64. {
  65. CDialog::DoDataExchange(pDX);
  66. //{{AFX_DATA_MAP(CChatSvrDlg)
  67. DDX_Control(pDX, IDC_LIST_MSGS, m_ctrlMsgs);
  68. //}}AFX_DATA_MAP
  69. }
  70. BEGIN_MESSAGE_MAP(CChatSvrDlg, CDialog)
  71. //{{AFX_MSG_MAP(CChatSvrDlg)
  72. ON_WM_SYSCOMMAND()
  73. ON_WM_PAINT()
  74. ON_WM_QUERYDRAGICON()
  75. ON_WM_DESTROY()
  76. //}}AFX_MSG_MAP
  77. END_MESSAGE_MAP()
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CChatSvrDlg message handlers
  80. BOOL CChatSvrDlg::OnInitDialog()
  81. {
  82. CDialog::OnInitDialog();
  83. // Add "About..." menu item to system menu.
  84. // IDM_ABOUTBOX must be in the system command range.
  85. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  86. ASSERT(IDM_ABOUTBOX < 0xF000);
  87. CMenu* pSysMenu = GetSystemMenu(FALSE);
  88. if (pSysMenu != NULL)
  89. {
  90. CString strAboutMenu;
  91. strAboutMenu.LoadString(IDS_ABOUTBOX);
  92. if (!strAboutMenu.IsEmpty())
  93. {
  94. pSysMenu->AppendMenu(MF_SEPARATOR);
  95. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  96. }
  97. }
  98. // Set the icon for this dialog.  The framework does this automatically
  99. //  when the application's main window is not a dialog
  100. SetIcon(m_hIcon, TRUE); // Set big icon
  101. SetIcon(m_hIcon, FALSE); // Set small icon
  102. // TODO: Add extra initialization here
  103. //弹出初始话对话框
  104. CInitDlg dlg;
  105. if (dlg.DoModal() == IDOK)
  106. {
  107. //创建监听套节字
  108. m_pSocket = new CListeningSocket(this);
  109. if (m_pSocket->Create(dlg.m_nPort+700))
  110. {
  111. if (m_pSocket->Listen())
  112. return TRUE;
  113. }
  114. else
  115. return FALSE;
  116. }
  117. else
  118. return FALSE;
  119. return TRUE;  // return TRUE  unless you set the focus to a control
  120. }
  121. void CChatSvrDlg::OnSysCommand(UINT nID, LPARAM lParam)
  122. {
  123. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  124. {
  125. CAboutDlg dlgAbout;
  126. dlgAbout.DoModal();
  127. }
  128. else
  129. {
  130. CDialog::OnSysCommand(nID, lParam);
  131. }
  132. }
  133. // If you add a minimize button to your dialog, you will need the code below
  134. //  to draw the icon.  For MFC applications using the document/view model,
  135. //  this is automatically done for you by the framework.
  136. void CChatSvrDlg::OnPaint() 
  137. {
  138. if (IsIconic())
  139. {
  140. CPaintDC dc(this); // device context for painting
  141. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  142. // Center icon in client rectangle
  143. int cxIcon = GetSystemMetrics(SM_CXICON);
  144. int cyIcon = GetSystemMetrics(SM_CYICON);
  145. CRect rect;
  146. GetClientRect(&rect);
  147. int x = (rect.Width() - cxIcon + 1) / 2;
  148. int y = (rect.Height() - cyIcon + 1) / 2;
  149. // Draw the icon
  150. dc.DrawIcon(x, y, m_hIcon);
  151. }
  152. else
  153. {
  154. CDialog::OnPaint();
  155. }
  156. }
  157. // The system calls this to obtain the cursor to display while the user drags
  158. //  the minimized window.
  159. HCURSOR CChatSvrDlg::OnQueryDragIcon()
  160. {
  161. return (HCURSOR) m_hIcon;
  162. }
  163. //更新所有客户端
  164. void CChatSvrDlg::UpdateClients(char* buffer,int nBufferSize)
  165. {
  166. for(POSITION pos = m_connectionList.GetHeadPosition(); pos != NULL;)
  167. {
  168. //将新信息发送给每个客户端
  169. CClientSocket* pSocket = (CClientSocket*)m_connectionList.GetNext(pos);
  170. if (buffer != NULL)
  171. pSocket->Send(buffer,nBufferSize,0);
  172. }
  173. }
  174. //处理客户端的连接信息
  175. void CChatSvrDlg::ProcessPendingAccept() 
  176. {
  177. //生成一个新的套节字与客户端通讯
  178. CClientSocket* pSocket = new CClientSocket(this);
  179. if (m_pSocket->Accept(*pSocket))
  180. {
  181. //将该套节字保存起来
  182. m_connectionList.AddTail(pSocket);
  183. }
  184. else
  185. delete pSocket;
  186. }
  187. //接收数据
  188. void CChatSvrDlg::ProcessPendingRead(CClientSocket* pSocket)
  189. {
  190. //定义缓冲区
  191. char buffer[BUFFER_SIZE];
  192. //接收数据
  193. int nReceived = pSocket->Receive(buffer,BUFFER_SIZE,0);
  194. buffer[nReceived] = 0;
  195. //将数据在列表框中显示出来
  196. m_ctrlMsgs.AddString(buffer);
  197. //将数据发送给每个客户
  198. UpdateClients(buffer,nReceived);
  199. }
  200. void CChatSvrDlg::OnDestroy() 
  201. {
  202. delete m_pSocket;
  203. CDialog::OnDestroy();
  204. }