Visual C++ Network: Where can I find examples of socket programs?
Q: Where can I find examples of socket programs?
A:MSDN has a few good examples. Although these all are based on the MFC 'CAsyncSocket' classes, they should serve as guidance even if you are not using these classes. The URLs are current as of January 2005:
'MFCAsync.exe' contains a Visual C++ 5.0 project sample that shows the communication techniques between a client ('AsyncClient') and a server ('AsyncServer') application using MFC 'CAsyncSocket' class on each sides.
The 'MSocUdp' sample is a minimal dialog box-based MFC application that demonstrates how to use the CAsyncSocket class to send and receive directed datagram or broadcast datagram over User Datagram Protocol (UDP).
This sample illustrates how to pass a socket connection between threads in an MFC application. The sample consists of two projects, the Server and the Client. The server creates a new thread for each connection to communicate with the client.
The 'MFCSocs' sample is a minimal MFC application that demonstrates how to use the 'CSocket' and 'CAsyncSocket' classes to communicate in a Transmission Control Protocol (TCP) connection. This sample is a dialog-based application. Depending on the selection in the user interface, the application can run either as a TCP server that listens on a certain TCP port, or as a client that connects to a server on the same TCP port. This sample also addresses two common mistakes that MFC socket programmers can make:
Issuing more than one 'Receive' call in the 'OnReceive' notification function and
Not issuing enough 'Send' calls to make sure all data has been sent when using the 'CAsyncSocket::Send' function
'CHATTER' is a Windows Sockets Client sample application. It is a single document interface (SDI) application with a splitter window that lets the user send messages to a discussion server ('CHATSRVR', next below), which in turn sends them to multiple other 'CHATTER' users simultaneously.
'Webster' is the Yugo of Web servers. It offers only minimal HTTP support with no frills. 'Webster' only handles GET requests, and it doesn't do anything with most of the extra client information it receives, like the If-Modified-Since field. But 'Webster' does illustrate the basic features common to every Web server, including very powerful servers like Microsoft Internet Information Server. 'Webster' is good enough to present your very own real live Web home page, and it does run on both Windows 95 and Windows NT. (From the February 1996 issue of Microsoft Systems Journal (predecessor of MSDN Magazine))
If you are looking for examples that are mostly based on pure Berkeley sockets (which includes Winsock and Unix/Linux flavors of that library), then here are some good examples. All use basic functionality of a much older version of Winsock (version 1.0) and therefore none rely on advanced functionality (like event-based triggers) provided by the current version of Winsock (version 2.2.2). All are obtained from the excellent "Winsock Programmer's FAQ", under Section 6.1 "Basic Example Programs":