ServerDlg.cpp
上传用户:xsxdsb
上传日期:2009-12-14
资源大小:672k
文件大小:7k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // ServerDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Server.h"
  5. #include "ServerDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. SOCKET g_hSocket = NULL;
  12. UINT ServerThreadProc(LPVOID pParam)
  13. {
  14. sockaddr_in saClient;
  15. SOCKET sockTemp = NULL;
  16. ASSERT(g_hSocket != NULL);
  17. int nLengthAddr = sizeof(SOCKADDR);
  18. //成功连接后返回实际连接的SOCKET句柄
  19. sockTemp = accept(g_hSocket, (sockaddr*)&saClient, &nLengthAddr);
  20. if(sockTemp == INVALID_SOCKET )
  21. {
  22. if(WSAGetLastError() != WSAEINTR)
  23. {
  24. AfxMessageBox("调用Accept函数失败");
  25. }
  26. return 1;
  27. }
  28. // 接受了客户端的连接请求后,立即启动一线程重新开始监听
  29. AfxBeginThread(ServerThreadProc,pParam);
  30. char sCommand[256];
  31. memset(sCommand,0,256);
  32. int nBytesReceived;
  33. if((nBytesReceived = recv(sockTemp, sCommand, 255, 0)) == SOCKET_ERROR)
  34. {
  35. AfxMessageBox("接受数据失败");
  36. return 1 ;
  37. }
  38. if (nBytesReceived == 0) return 1;
  39. //查找是否含有目标命令串
  40.     if (strcmp(sCommand,"Get Time") == 0)
  41. {
  42. char sBuff[256];
  43. memset(sBuff,0,256);
  44. //读取服务器当前时间
  45. SYSTEMTIME time;
  46. GetLocalTime(&time);
  47. sprintf(sBuff,"Set Time%4d%2d%2d%2d%2d%2d",
  48. time.wYear,time.wMonth,time.wDay,
  49. time.wHour,time.wMinute,time.wSecond);                
  50. //发送数据
  51. int nBytesSent;
  52. if((nBytesSent = send(sockTemp, sBuff, strlen(sBuff), 0)) == SOCKET_ERROR) 
  53. {
  54. AfxMessageBox("发送数据失败");
  55. return 1;
  56. }
  57. }
  58. // 关闭Socket
  59. if(closesocket(sockTemp) == SOCKET_ERROR)
  60. {
  61. AfxMessageBox("关闭连接失败");
  62. sockTemp = NULL;
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CAboutDlg dialog used for App About
  69. class CAboutDlg : public CDialog
  70. {
  71. public:
  72. CAboutDlg();
  73. // Dialog Data
  74. //{{AFX_DATA(CAboutDlg)
  75. enum { IDD = IDD_ABOUTBOX };
  76. //}}AFX_DATA
  77. // ClassWizard generated virtual function overrides
  78. //{{AFX_VIRTUAL(CAboutDlg)
  79. protected:
  80. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  81. //}}AFX_VIRTUAL
  82. // Implementation
  83. protected:
  84. //{{AFX_MSG(CAboutDlg)
  85. //}}AFX_MSG
  86. DECLARE_MESSAGE_MAP()
  87. };
  88. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  89. {
  90. //{{AFX_DATA_INIT(CAboutDlg)
  91. //}}AFX_DATA_INIT
  92. }
  93. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  94. {
  95. CDialog::DoDataExchange(pDX);
  96. //{{AFX_DATA_MAP(CAboutDlg)
  97. //}}AFX_DATA_MAP
  98. }
  99. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  100. //{{AFX_MSG_MAP(CAboutDlg)
  101. // No message handlers
  102. //}}AFX_MSG_MAP
  103. END_MESSAGE_MAP()
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CServerDlg dialog
  106. CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
  107. : CDialog(CServerDlg::IDD, pParent)
  108. {
  109. //{{AFX_DATA_INIT(CServerDlg)
  110. // NOTE: the ClassWizard will add member initialization here
  111. //}}AFX_DATA_INIT
  112. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  113. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  114. }
  115. void CServerDlg::DoDataExchange(CDataExchange* pDX)
  116. {
  117. CDialog::DoDataExchange(pDX);
  118. //{{AFX_DATA_MAP(CServerDlg)
  119. // NOTE: the ClassWizard will add DDX and DDV calls here
  120. //}}AFX_DATA_MAP
  121. }
  122. BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
  123. //{{AFX_MSG_MAP(CServerDlg)
  124. ON_WM_SYSCOMMAND()
  125. ON_WM_PAINT()
  126. ON_WM_QUERYDRAGICON()
  127. ON_BN_CLICKED(IDC_START, OnStart)
  128. ON_BN_CLICKED(IDC_END, OnEnd)
  129. //}}AFX_MSG_MAP
  130. END_MESSAGE_MAP()
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CServerDlg message handlers
  133. BOOL CServerDlg::OnInitDialog()
  134. {
  135. CDialog::OnInitDialog();
  136. // Add "About..." menu item to system menu.
  137. // IDM_ABOUTBOX must be in the system command range.
  138. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  139. ASSERT(IDM_ABOUTBOX < 0xF000);
  140. CMenu* pSysMenu = GetSystemMenu(FALSE);
  141. if (pSysMenu != NULL)
  142. {
  143. CString strAboutMenu;
  144. strAboutMenu.LoadString(IDS_ABOUTBOX);
  145. if (!strAboutMenu.IsEmpty())
  146. {
  147. pSysMenu->AppendMenu(MF_SEPARATOR);
  148. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  149. }
  150. }
  151. // Set the icon for this dialog.  The framework does this automatically
  152. //  when the application's main window is not a dialog
  153. SetIcon(m_hIcon, TRUE); // Set big icon
  154. SetIcon(m_hIcon, FALSE); // Set small icon
  155. // TODO: Add extra initialization here
  156. return TRUE;  // return TRUE  unless you set the focus to a control
  157. }
  158. void CServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
  159. {
  160. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  161. {
  162. CAboutDlg dlgAbout;
  163. dlgAbout.DoModal();
  164. }
  165. else
  166. {
  167. CDialog::OnSysCommand(nID, lParam);
  168. }
  169. }
  170. // If you add a minimize button to your dialog, you will need the code below
  171. //  to draw the icon.  For MFC applications using the document/view model,
  172. //  this is automatically done for you by the framework.
  173. void CServerDlg::OnPaint() 
  174. {
  175. if (IsIconic())
  176. {
  177. CPaintDC dc(this); // device context for painting
  178. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  179. // Center icon in client rectangle
  180. int cxIcon = GetSystemMetrics(SM_CXICON);
  181. int cyIcon = GetSystemMetrics(SM_CYICON);
  182. CRect rect;
  183. GetClientRect(&rect);
  184. int x = (rect.Width() - cxIcon + 1) / 2;
  185. int y = (rect.Height() - cyIcon + 1) / 2;
  186. // Draw the icon
  187. dc.DrawIcon(x, y, m_hIcon);
  188. }
  189. else
  190. {
  191. CDialog::OnPaint();
  192. }
  193. }
  194. // The system calls this to obtain the cursor to display while the user drags
  195. //  the minimized window.
  196. HCURSOR CServerDlg::OnQueryDragIcon()
  197. {
  198. return (HCURSOR) m_hIcon;
  199. }
  200. void CServerDlg::OnStart() 
  201. {
  202. sockaddr_in saServer;
  203. saServer.sin_family = AF_INET;
  204. saServer.sin_port = htons(CONNECE_PORT);
  205. saServer.sin_addr.s_addr = htonl(INADDR_ANY); 
  206.     ASSERT(g_hSocket == NULL);
  207. WORD wVersionRequested;
  208. WSADATA wsaData;
  209. int nErr;
  210. wVersionRequested = MAKEWORD( 2, 0 );
  211. //加载所需的Winsock dll版本
  212. nErr = WSAStartup( wVersionRequested, &wsaData );
  213. if(nErr)
  214. {
  215. AfxMessageBox("加载Winsock DLL 出错");
  216. return;
  217. }
  218. if((g_hSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) 
  219. {
  220. AfxMessageBox("创建Socket失败");
  221. return;
  222. }
  223. //绑定地址
  224. if(bind(g_hSocket, (sockaddr*)&saServer, sizeof(SOCKADDR)) == SOCKET_ERROR)
  225. {
  226. AfxMessageBox("绑定地址失败");
  227. return;
  228. }
  229. //监听客户端请求
  230. if(listen(g_hSocket, 5) == SOCKET_ERROR) 
  231. {
  232. AfxMessageBox("监听客户端请求失败");
  233. return;
  234. }
  235. //启动一线程来处理客户端请求
  236. AfxBeginThread(ServerThreadProc,0);
  237. //使开始按钮变灰
  238. GetDlgItem(IDC_START)->EnableWindow(FALSE);
  239. }
  240. void CServerDlg::OnEnd() 
  241. {
  242. //关闭Socket
  243. if(g_hSocket == NULL) return;
  244. VERIFY(closesocket(g_hSocket) != SOCKET_ERROR);
  245. g_hSocket = NULL;
  246. //使开始按钮有效
  247. GetDlgItem(IDC_START)->EnableWindow(TRUE);
  248. }