The TimeClient Sample Application

[Previous] [Next]

The TimeClient sample application ("03 TimeClient.exe"), shown in Listing 3-2, tests the TimeService service. The source code and resource files for the application are in the 03-TimeClient directory on the companion CD. When you start the program, the dialog box in Figure 3-8 appears.

Figure 3-8. Initial dialog box in the TimeClient sample application

To see the client/server communication work, you must type the server name in the edit control at the top of the dialog box. If you are running the client and the server process on the same machine, type a period as the server name (as shown in Figure 3-8). When you click the Request Server's Time button, the client application calls CreateFile, which connects the client to the server, causing the server to wake up and process the client's request. If the server is not running, CreateFile fails and a message box similar to the one shown in Figure 3-9 appears.

click to view at full size.

Figure 3-9. Message box displayed by TimeClient when the TimeService service isn't running

If the service is up and running, CreateFile returns a valid handle, and the client waits for the time data to come across the pipe by placing a synchronous call to ReadFile. After the client has the data, the client's pipe handle is closed, the time from the server (which came across in universal time) is converted to the client's local time, and the initial dialog box is updated to look like Figure 3-10.

Figure 3-10. The updated dialog box for the TimeClient sample application

Listing 3-2. The TimeClient sample application

 

TimeClient.cpp

/****************************************************************************** Module: TimeClient.cpp Notices: Copyright (c) 2000 Jeffrey Richter ******************************************************************************/ #include "..\CmnHdr.h" // See Appendix A #include <WindowsX.h> #include "..\ClassLib\EnsureCleanup.h" // See Appendix B #include "Resource.h" ////////////////////////////////////////////////////////////////////////////// BOOL Dlg_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) { chSETDLGICONS(hwnd, IDI_TIMECLIENT); // Assume that the server is on the same machine as the client SetDlgItemText(hwnd, IDC_SERVER, TEXT(".")); return(TRUE); } ////////////////////////////////////////////////////////////////////////////// void Dlg_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) { switch (id) { case IDCANCEL: EndDialog(hwnd, id); break; case IDOK: // Construct the pathname of the pipe TCHAR sz[500]; sz[0] = sz[1] = TEXT(`\\'); GetDlgItemText(hwnd, IDC_SERVER, &sz[2], chDIMOF(sz) - 2); lstrcat(sz, TEXT("\\pipe\\TimeService")); // Attempt to connect to the pipe // Get a handle to use to talk to the pipe CEnsureCloseFile hpipe = CreateFile(sz, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); if (hpipe.IsValid()) { // Valid handle, read time from pipe SYSTEMTIME st; DWORD cbRead = 0; ReadFile(hpipe, &st, sizeof(st), &cbRead, NULL); // Convert UTC time to client machine's local time and display it SystemTimeToTzSpecificLocalTime(NULL, &st, &st); GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, sz, chDIMOF(sz)); SetDlgItemText(hwnd, IDC_DATE, sz); GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE, &st, NULL, sz, chDIMOF(sz)); SetDlgItemText(hwnd, IDC_TIME, sz); } else { // Invalid handle, report an error SetDlgItemText(hwnd, IDC_DATE, TEXT("Error")); SetDlgItemText(hwnd, IDC_TIME, TEXT("Error")); // Get the error code's textual description HLOCAL hlocal = NULL; // Buffer that gets the error message string FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, GetLastError(), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (PSTR) &hlocal, 0, NULL); if (hlocal != NULL) { chMB((PCSTR) LocalLock(hlocal)); LocalFree(hlocal); } } break; } } ////////////////////////////////////////////////////////////////////////////// INT_PTR WINAPI Dlg_Proc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog); chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand); } return(FALSE); } ////////////////////////////////////////////////////////////////////////////// int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) { DialogBox(hinstExe, MAKEINTRESOURCE(IDD_TIMECLIENT), NULL, Dlg_Proc); return(0); } //////////////////////////////// End Of File /////////////////////////////////



Programming Server-Side Applications for Microsoft Windows 2000
Programming Server-Side Applications for Microsoft Windows 2000 (Microsoft Programming)
ISBN: 0735607532
EAN: 2147483647
Year: 2000
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net