P1: 50
Mayfield WL040/Bidgoli-Vol III-Ch-52 June 23, 2003 16:35 Char Count= 0
WININETCLIENTPROGRAMMING 643Figure 6: The MFCServer app window.(4) // Retrieve view ptr so messages
can be displayed.
(5) CWnd *pView = CWnd::FromHandle((HWND)
pParam);
(6)
(7) // Attach the global socket handle
to the local
(8) // server socket object.
(9) serverSock.Attach(hSocket);
(10)
(11) // Create a socket file and a
loading archive.
(12) CSocketFile sockFile(&serverSock);
(13) CArchive archiveRecv(&sockFile,
CArchive::load);
(14)
(15) // Retrieve the time repeatedly
until the server
(16) // connection terminates.
(17) TRY {
(20) char buffer[64];
(18) while (1) {
(19) // Retrieve the time from the
server.
(21) sockFile.Read(buffer, 64);
(22) // Display the string in the view.
(23) pView->SetWindowText(buffer);
(24) }
(25) }
(26) CATCH (CFileException, pEx) {
(27) // Communication was lost
with the server;
(28) // close the server socket.
(29) serverSock.Close();
(30) }
(31) END_CATCH(32)
(33) // Return success code.
(34) return 0;
(35) }The important differences between this function and
ServerThreadProcseen earlier are in lines 21 and 23 of
ClientThreadProc. The loop body reads a string from
the server connection and then displays it in the view win-
dow.Final Changes
Only one global variable is needed to pass socket handle
information betweenOnInitialUpdateandServer-
ThreadProc. Insert the following statement after the
#include statements inMFCClientView.cpp: SOCKET
hSocket;Build the Project and Test the Client–Server
Application
Build the client project. To test the application, first
start the server program namedMFCServer.exe,which
is found in theDebugdirectory of the server project.
Then start one or more copies of the client program
namedMFCClient.exe,which is found in theDebug
directory of the client project. One should see the num-
ber of clients displayed in the server window, as shown
in Figure 6, and the current time—updated once per
second—displayed in each client window, as shown in
Figure 7.WININET CLIENT PROGRAMMING
An Alternative to WinSock?
WinInet (Win32 Internet) is an Internet API from
Microsoft written specifically for their Windows operatingFigure 7: The MFCClient app window.