ex126SvrDlg.cpp
上传用户:qdhmjx
上传日期:2022-07-11
资源大小:2226k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // ex126SvrDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ex126Svr.h"
  5. #include "ex126SvrDlg.h"
  6. #include "InitDlg.h"
  7. #include "ListeningSocket.h"
  8. #include "ClientSocket.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. // CEx126SvrDlg dialog
  53. CEx126SvrDlg::CEx126SvrDlg(CWnd* pParent /*=NULL*/)
  54. : CDialog(CEx126SvrDlg::IDD, pParent)
  55. {
  56. //{{AFX_DATA_INIT(CEx126SvrDlg)
  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. }
  62. void CEx126SvrDlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CDialog::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CEx126SvrDlg)
  66. DDX_Control(pDX, IDC_LISTBOX, m_ctrlMsg);
  67. //}}AFX_DATA_MAP
  68. }
  69. BEGIN_MESSAGE_MAP(CEx126SvrDlg, CDialog)
  70. //{{AFX_MSG_MAP(CEx126SvrDlg)
  71. ON_WM_SYSCOMMAND()
  72. ON_WM_PAINT()
  73. ON_WM_QUERYDRAGICON()
  74. //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CEx126SvrDlg message handlers
  78. BOOL CEx126SvrDlg::OnInitDialog()
  79. {
  80. CDialog::OnInitDialog();
  81. // Add "About..." menu item to system menu.
  82. // IDM_ABOUTBOX must be in the system command range.
  83. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  84. ASSERT(IDM_ABOUTBOX < 0xF000);
  85. CMenu* pSysMenu = GetSystemMenu(FALSE);
  86. if (pSysMenu != NULL)
  87. {
  88. CString strAboutMenu;
  89. strAboutMenu.LoadString(IDS_ABOUTBOX);
  90. if (!strAboutMenu.IsEmpty())
  91. {
  92. pSysMenu->AppendMenu(MF_SEPARATOR);
  93. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  94. }
  95. }
  96. // Set the icon for this dialog.  The framework does this automatically
  97. //  when the application's main window is not a dialog
  98. SetIcon(m_hIcon, TRUE); // Set big icon
  99. SetIcon(m_hIcon, FALSE); // Set small icon
  100. // TODO: Add extra initialization here
  101. CInitDlg dlg;
  102. if(dlg.DoModal()==IDOK)
  103. {
  104. m_pSocket=new CListeningSocket(this);
  105. if(m_pSocket->Create(dlg.m_nPort+700))
  106. {
  107. if(m_pSocket->Listen())
  108. return TRUE;
  109. }
  110. else
  111. return FALSE;
  112. }
  113. else
  114. return FALSE;
  115. return TRUE;  // return TRUE  unless you set the focus to a control
  116. }
  117. void CEx126SvrDlg::OnSysCommand(UINT nID, LPARAM lParam)
  118. {
  119. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  120. {
  121. CAboutDlg dlgAbout;
  122. dlgAbout.DoModal();
  123. }
  124. else
  125. {
  126. CDialog::OnSysCommand(nID, lParam);
  127. }
  128. }
  129. // If you add a minimize button to your dialog, you will need the code below
  130. //  to draw the icon.  For MFC applications using the document/view model,
  131. //  this is automatically done for you by the framework.
  132. void CEx126SvrDlg::OnPaint() 
  133. {
  134. if (IsIconic())
  135. {
  136. CPaintDC dc(this); // device context for painting
  137. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  138. // Center icon in client rectangle
  139. int cxIcon = GetSystemMetrics(SM_CXICON);
  140. int cyIcon = GetSystemMetrics(SM_CYICON);
  141. CRect rect;
  142. GetClientRect(&rect);
  143. int x = (rect.Width() - cxIcon + 1) / 2;
  144. int y = (rect.Height() - cyIcon + 1) / 2;
  145. // Draw the icon
  146. dc.DrawIcon(x, y, m_hIcon);
  147. }
  148. else
  149. {
  150. CDialog::OnPaint();
  151. }
  152. }
  153. // The system calls this to obtain the cursor to display while the user drags
  154. //  the minimized window.
  155. HCURSOR CEx126SvrDlg::OnQueryDragIcon()
  156. {
  157. return (HCURSOR) m_hIcon;
  158. }
  159. void CEx126SvrDlg::ProcessPendingAccept()
  160. {
  161. //生成一个新socket
  162. CClientSocket* pSocket=new CClientSocket(this);
  163. if(m_pSocket->Accept(*pSocket))
  164. m_connectionList.AddTail(pSocket);
  165. else
  166. delete pSocket;
  167. }
  168. void CEx126SvrDlg::UpdateClients(char *buffer, int nBufferSize)
  169. {
  170. for(POSITION pos=m_connectionList.GetHeadPosition();pos!=NULL;)
  171. {//将信息发送到每个客户端
  172. CClientSocket* pSocket=(CClientSocket*)m_connectionList.GetNext(pos);
  173. if(buffer!=NULL)
  174. pSocket->Send(buffer,nBufferSize,0);
  175. }
  176. }
  177. void CEx126SvrDlg::ProcessPendingRead(CClientSocket *pSocket)
  178. {
  179. char buffer[BUFFER_SIZE];
  180. int nReceived=pSocket->Receive(buffer,BUFFER_SIZE,0);
  181. buffer[nReceived]=0;
  182. m_ctrlMsg.AddString(buffer);
  183. UpdateClients(buffer,nReceived);
  184. }