Winsock Programmer’s FAQ
Basic Winsock Examples: CAsyncSocket-based Client
This example is based on the "MFCConsole" framework. Between that base and the FAQ-specific extensions, there are 1800 lines of generic framework code, neatly separated from the specific parts of each example based on it. You do not need to understand this framework to learn from the examples, but if you are interested, you can find information about the framework on the MFCConsole page.
The example framework has two main menu items: "Start Winsock Handler"
(Ctrl-W), and a generic "Action" item (Ctrl-A). When you give the
Start command, it calls
DoWinsock() — this is similar to
the console programs'
DoWinsock() mechanism. The main difference
in the GUI version is that
DoWinsock() doesn't call the network
handling functions directly: it just creates the object that does the
real work. Then you give the Action command to do whatever specific
thing the example does.
All the following comparisons are against the Winsock API-based asynchronous I/O client. That phrase is a mouthful, so we'll just call it the "pure asynch" client below.
The unique parts of this example are in the CCASClientSocket class.
Aside from the CNetworkDriver interface, this class derives from
CAsyncSocket so we can override functions like
tried to derive from CWnd as well so we could have timeout timers like
CAsyncClientWnd does, but that caused a multiple symbol problem that
even virtual inheritance couldn't help. (VC++ bug?)
Start() function in this example is much more substantial
than in the pure async client, because it actually will initiate the
connection to the server. That's because unlike my name lookup code,
Microsoft's code in CAsyncSocket uses blocking DNS lookup functions. This
makes the calling code simpler, but it does put your program at the
mercy of your DNS server's speed. Anyway, it means we don't have to put
off attempting the connection until Winsock calls us back with a window
message telling us that the DNS lookup succeeded.
The rest of the program is pretty much identical to the pure async client, because CAsyncClient (like the rest of MFC) maps pretty closely to the underlying API. Nevertheless, the several small savings of the wrapper class did add up: this program is only 230 lines of code, a savings of 100 lines over the pure API version. To be fair, though, adding on your own asynchronous lookup function and timeout timers would eat into that 100 line savings.
The project package (33 KB) is a complete Visual C++ 5.0 project. It includes everything you need to build the sample.
Note that this example program is released under a different license than the other example programs. It's a BSD-style license, which means you can do anything you want with the example so long as you don't sue me or my employer. Not even if it prints 500 pages of personal insults on your customer's high-speed laser printer.
<< Asynchronous I/O Client
||Basic Blocking Server >>|
|Updated Sun Jan 18 2015 04:24 MST||Go to my home page|