VirtualClient.cpp
上传用户:szopptop
上传日期:2013-04-23
资源大小:1047k
文件大小:6k
源码类别:

模拟服务器

开发平台:

Visual C++

  1. // VirtualClient.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. // **************************************************************************************
  6. BOOL InitApplication(HANDLE hInstance);
  7. BOOL InitInstance(HANDLE hInstance, int nCmdShow);
  8. LPARAM APIENTRY MainWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
  9. BOOL ConnectServer(SOCKET &s, SOCKADDR_IN* addr, UINT nMsgID, LPCTSTR lpServerIP, DWORD dwIP, int nPort, long lEvent);
  10. LRESULT OnSockMsg(WPARAM wParam, LPARAM lParam);
  11. // **************************************************************************************
  12. //
  13. // Global Variables Definition
  14. //
  15. // **************************************************************************************
  16. HINSTANCE g_hInst = NULL; // Application instance
  17. HWND g_hMainWnd = NULL; // Main window handle
  18. HWND g_hLogMsgWnd = NULL;
  19. static WSADATA g_wsd;
  20. char szLvsLable[3][10] = { "Date", "Time", "Message" };
  21. char g_szServerIP[15];
  22. SOCKET g_sock = INVALID_SOCKET;
  23. SOCKADDR_IN g_addr;
  24. int g_nCode = 0;
  25. // **************************************************************************************
  26. int AddNewLogMsg()
  27. {
  28. LV_ITEM lvi;
  29. TCHAR szText[64];
  30. int nCount = ListView_GetItemCount(g_hLogMsgWnd);
  31. if (nCount >= 100)
  32. {
  33. ListView_DeleteItem(g_hLogMsgWnd, 0);
  34. nCount--;
  35. }
  36. lvi.mask = LVIF_TEXT;
  37. lvi.iItem = nCount;
  38. lvi.iSubItem = 0;
  39. _tstrdate(szText);
  40. lvi.pszText = szText;
  41. ListView_InsertItem(g_hLogMsgWnd, &lvi);
  42. _tstrtime(szText);
  43. ListView_SetItemText(g_hLogMsgWnd, nCount, 1, szText);
  44. return nCount;
  45. }
  46. void InsertLogMsg(LPTSTR lpszMsg)
  47. {
  48. int nCount = AddNewLogMsg();
  49. ListView_SetItemText(g_hLogMsgWnd, nCount, 2, lpszMsg);
  50. #ifdef _DEBUG
  51. _RPT1(_CRT_WARN, "%sn", lpszMsg);
  52. #endif
  53. }
  54. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  55. {
  56.     MSG msg;
  57. lstrcpy(g_szServerIP, lpCmdLine);
  58. if (!InitApplication(hInstance))
  59. return (FALSE);
  60. if (!InitInstance(hInstance, nCmdShow))
  61. return (FALSE);
  62. while (GetMessage(&msg, NULL, 0, 0))
  63. {
  64. TranslateMessage(&msg);
  65. DispatchMessage(&msg);
  66. }
  67.     return (msg.wParam);
  68. }
  69. // **************************************************************************************
  70. //
  71. //
  72. //
  73. // **************************************************************************************
  74. BOOL InitApplication(HANDLE hInstance)
  75. {
  76.     WNDCLASS  wc;
  77.     wc.style            = CS_GLOBALCLASS|CS_HREDRAW|CS_VREDRAW;
  78.     wc.lpfnWndProc      = (WNDPROC)MainWndProc;
  79.     wc.cbClsExtra       = 0;
  80.     wc.cbWndExtra       = 0;
  81.     wc.hIcon            = NULL;
  82.     wc.hInstance        = (HINSTANCE)hInstance;
  83.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  84.     wc.hbrBackground    = (HBRUSH)(COLOR_WINDOW + 1);
  85.     wc.lpszMenuName     = MAKEINTRESOURCE(IDR_MAINMENU);
  86.     wc.lpszClassName    = _T("VirtualClient");
  87. return RegisterClass(&wc);
  88. }
  89. // **************************************************************************************
  90. //
  91. //
  92. //
  93. // **************************************************************************************
  94. BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  95. {
  96. g_hInst = (HINSTANCE)hInstance;
  97. INITCOMMONCONTROLSEX icex;
  98. icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  99. icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES | ICC_INTERNET_CLASSES | ICC_TAB_CLASSES;
  100. InitCommonControlsEx(&icex);
  101.     g_hMainWnd = CreateWindowEx(0, _T("VirtualClient"), _T("固福狼 傈汲 2 - 啊惑 努扼捞攫飘"), 
  102. WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  103. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,                 
  104. NULL, NULL, (HINSTANCE)hInstance, NULL);
  105. RECT rcMainWnd;
  106. GetClientRect(g_hMainWnd, &rcMainWnd);
  107.     g_hLogMsgWnd = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, "", 
  108. WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_EDITLABELS,
  109. 0, 0, (rcMainWnd.right - rcMainWnd.left), 
  110. (rcMainWnd.bottom - rcMainWnd.top),
  111. g_hMainWnd, NULL, (HINSTANCE)hInstance, NULL);
  112. ListView_SetExtendedListViewStyleEx(g_hLogMsgWnd, 0, LVS_EX_FULLROWSELECT);
  113. LV_COLUMN lvc;
  114. TCHAR szText[64];
  115. lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  116. lvc.fmt = LVCFMT_LEFT;
  117. lvc.cx = 150;
  118. lvc.pszText = szText;
  119. for (int i = 0; i < 3; i++)
  120. {
  121. lvc.iSubItem = i;
  122. lvc.pszText = szLvsLable[i];
  123. ListView_InsertColumn(g_hLogMsgWnd, i, &lvc);
  124. }
  125. ShowWindow(g_hMainWnd, SW_SHOW);
  126. UpdateWindow(g_hMainWnd);
  127. if (WSAStartup(MAKEWORD(2, 2), &g_wsd) != 0)
  128. return (FALSE);
  129. ConnectServer(g_sock, &g_addr, _IDM_SOCKMSG, g_szServerIP, 0, 7000, FD_CONNECT|FD_READ|FD_CLOSE);
  130. return TRUE;
  131. }
  132. BOOL CALLBACK LoginFunc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  133. BOOL CALLBACK NewUserFunc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  134. LPARAM APIENTRY MainWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  135. {
  136. switch (nMsg)
  137. {
  138. case _IDM_SOCKMSG:
  139. return OnSockMsg(wParam, lParam);
  140. /* case _IDM_CLIENTSOCK_MSG:
  141. return OnClientSockMsg(wParam, lParam);
  142. */ case WM_COMMAND:
  143. {
  144. switch (LOWORD(wParam))
  145. {
  146. case IDM_CM_IDPASSWORD:
  147. DialogBox(g_hInst, MAKEINTRESOURCE(IDD_LOGIN), NULL, (DLGPROC)LoginFunc);
  148. break;
  149. case IDM_CM_ADDUSER:
  150. DialogBox(g_hInst, MAKEINTRESOURCE(IDD_NEWUSER), NULL, (DLGPROC)NewUserFunc);
  151. break;
  152. }
  153. break;
  154. }
  155. case WM_SIZE:
  156. {
  157. RECT rcMain;
  158. GetClientRect(g_hMainWnd, &rcMain);
  159. MoveWindow(g_hLogMsgWnd, 0, 0, (rcMain.right - rcMain.left), (rcMain.bottom - rcMain.top), TRUE);
  160. break;
  161. }
  162. case WM_CLOSE:
  163. {
  164. WSACleanup();
  165. PostQuitMessage(0);
  166. return 0L;
  167. }
  168. }
  169. return (DefWindowProc(hWnd, nMsg, wParam, lParam));
  170. }