The AppWizard and the Class Wizard
The AppWizard is an application framework generator. That is, its function is to generate working, skeletal GUI applications. The developer is prompted through a sequence of wizard dialog boxes that present choices relating to menu items, toolbars, database support, and other commonly required application components and decisions.
When the AppWizard has completed its interview of the programmer, it creates a Windows GUI application that embodies the requested features. That application can be compiled and run without the developer writing a single line of code.
However, the skeletal application does not really do much; it is up to the programmer to customize the application to fulfill its functionality.
Multithreaded Programming Issues
As mentioned earlier, a synchronous WinSock 2 program contains a function call that blocks further execution in the program until a connection has been made. In MFC GUI programs, this is particularly problematic; while execution is blocked, the user cannot communicate with the user interface. For example, if a WinSock program is blocked, the user can click on the “x” in the upper-right corner of the main program window to close it, but the program cannot and does not respond to the user interaction.
An Example WinSock Server
The best way to learn to use WinSock with MFC is by doing. In this section and the following section, an example client–server application is presented, step by step, to illustrate how one can use the CSocket MFC class. In this case, the client contacts the server, which then sends to the client, once every second, a string that indicates the current date and time on the server computer
Create the Server Project
Click on “New” from the IDE menu bar, and then select the “Project” tab in the dialog box. Choose “MFC AppWizard (exe)” as the type of project, and choose a project directory for the project name. (For the duration of this description, it will be assumed that the project is named MFCServer.) Click on the “OK” button to begin the AppWizard.
Create the Worker Thread Procedure
Insert the implementation of the global function ServerThreadProc into the file MFCServerView.cpp just above the implementation of OnInitialUpdate. The purpose of this function is to wait for a client connection, and once the connection has been made, to serve date/time strings to the client once each second. Further, after the connection has been made the function starts another thread (using the same thread function) so that other clients can be served simultaneously.
The last modification to the project is to add a #include and to declare global variables that will be used by OnInitialUpdate and ServerThreadProc.
The client program presented communicates with the server program described above. It uses many of the same ideas and techniques as the server. Before beginning work on the client program, make sure to close the server program workspace.