The Internet Encyclopedia (Volume 3)

(coco) #1

P1: 50


Mayfield WL040/Bidgoli-Vol III-Ch-52 June 23, 2003 16:35 Char Count= 0


640 VISUALC++ (MICROSOFT)

Figure 5: The ClassWizard.

from the dropdown list of view base class choices (in step
6). Accept the defaults for all other AppWizard choices.

Create a Message Handler
Invoke the ClassWizard by clicking on “View” from the
IDE menu bar, and then on “ClassWizard.” From the
“Class name” dropdown list chooseCMFCServerView,
from the “Object IDs” list chooseCMFCServerView, and
from the “Messages” list chooseOnInitialUpdate. The
ClassWizard window should now look something like that
shown in Figure 5.
Click on the “Add Function” button, and OnIni-
tialUpdateappears in the “Member functions” list at
the bottom of the ClassWizard window. The ClassWizard
has added a declaration for this method to the view class
interface, and it also has created a skeletal method imple-
mentation. Double-click the nameOnInitialUpdatein
the “Member functions” list, and the ClassWizard disap-
pears and the cursor is placed at the newly created method
implementation in the source editor. Insert the statements
shown in Listing 1 after the other statements that are al-
ready inOnInitialUpdate. (Do not type the line num-
bers that are shown in the left margin of each statement;
these are for discussion only.)

Listing 1:OnInitalUpdate body for example WinSock server
program.
(1) // Put initial message to view.
(2) SetWindowText("Number of clients: 0");
(3)
(4) CSocket serverSock;
(5)
(6) // Create the listening socket.
(7) if (!serverSock.Create(5555)) {
(8) MessageBox("Create Error");
(9) return;
(10) }
(11)

(12) // Activate the listening socket.
(13) if (!serverSock.Listen()) {
(14) MessageBox("Listen Error");
(15) return;
(16) }
(17)
(18) // This is needed to make
multithreading work.
(19) hSocket = serverSock.Detach();
(20)
(21) // Begin the server thread,
passing a handle to the view.
(22) AfxBeginThread(ServerThreadProc,
GetSafeHwnd());

OnInitialUpdateis a message handler that is called
automatically just before the main frame window of an
application is displayed for the first time. The server dis-
plays in the application view the number of clients that it
is serving currently; line 2 in Listing 1 displays zero as the
initial number of clients.
Lines 4–16 set up aCSocketobject that listens for
connections from clients. The argument of theCreate
method is a port number. (A value different from the
one shown can be used.) Because no host IP address is
specified in the call, a value of NULL is assumed; this in-
structs the system to choose and assign to the socket the
IP address of any available network interface on the server
machine. Line 19 detaches the socket from theCSocket
object in preparation of beginning a new thread and then
stores the socket handle in the global variable hSocket
for use by the worker threads. (As mentioned earlier,
this is required because complex MFC objects cannot be
shared between threads.) Finally, in line 22, a new worker
thread is begun so that the main thread can continue
to process Windows messages. The new thread serves
the date/time strings to the clients. The first argument of
AfxBeginThreadis the name of the worker thread func-
tion; the second argument is a value to be passed as an
Free download pdf