<<

Winsock Programmer's FAQ
Basic Winsock Examples

>>

These examples show you how to implement several basic kinds of client and server programs. All the client programs simply connect to an echo server, send a packet of data, read the echoed reply back from the server, check that the data in the reply packet is what was sent, and exit. The server programs implement various forms of the echo server idea. (You can also use a standard "port 7" echo server available as an option on many Unix boxes and Windows NT/2000.) The echo server idea lets us focus more on the structure of the program and less on its function, and yet still provide a fairly functional program.

Although these programs are "minimal" in the sense that they don't do much, they are still complete in that they do all the proper error checking, shut their connections down gracefully, are fully commented, etc. Some thought has been given to making it easy to swipe pieces of code from these examples.

These programs all follow the same form as much as possible. Any time you find two functions named the same in two different examples, you can be sure that they have the same function, so you can study them comparatively. For example, you can study EstablishConnection() in the basic and asynchronous clients to see how "going asynch" changes the way you handle a connection attempt.

Right now, I only have one simple client and one simple server here, but more are soon to come, as I find the time. The next program will be a multi-connection multithreaded server, and then a select()-based server. After that, I'll implement asynchronous clients and servers with straight Win32 API code. Next will come overlapped I/O clients and servers. After that, reader comments will guide me. Two other ideas I have in mind are examples that use I/O completion ports and the MFC CSocket/CAsyncSocket classes. I may even present an example that uses my Endpoint class library.

Support Files

You should probably download all of these before you begin, because all examples require at least one of these files.

    Makefile - The Makefile (Borland and Microsoft C++-compatible) that builds the examples. (Not required, but makes building the examples easier.)

    main.cpp - The main() function for the console-mode example programs. Parses the command line, initializes Winsock, calls the example's driver function, and then shuts Winsock back down.

    ws-util.cpp - A set of Winsock utility functions used by more than one example program.

    ws-util.h - Header file for ws-util.cpp.

The Examples

These are the main example files. Each example comes in a single file; the header comment in that file tells you which other modules it requires.

Each of these examples illustrates a particular design choice. You can find answers on "which choice is better" for a given application in Tutorial 1. Obviously, no one design choice is best for all situations, or we would not have so many choices. Between the Tutorial's advice and these concrete examples, you should be able to make your decision fairly easily.


<< Example Programs Basic Blocking Client >>
Last modified on 29 April 2000 at 15:52 UTC-7 Please send corrections to tangent@cyberport.com.
< Go to the main FAQ page
<< Go to my Programming pages
<<< Go to my Home Page