January 29th, 2005, 11:44 PM
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:
- "Message-Oriented TCP and Multithreaded Client/Server"
'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.
- "MSocUdp.exe Implement UDP Using CAsyncSocket"
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).
- "MultiSoc: Illustrates Using Sockets in Multiple Threads"
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.
- "MFCSocs.exe Avoids Two Common MFC Socket Mistakes"
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 Sample: Demonstrates a Windows Sockets Client Application"
'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.
- "CHATSRVR Sample: Demonstrates a Windows Sockets Server"
'CHATSRVR', a Windows Sockets Server sample application, is a single document interface (SDI) application that implements a discussion server for clients of the 'CHATTER' sample (directly above).
- "Write a Simple HTTP-based Server Using MFC and Windows Sockets"
'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":
- "Basic Blocking Client"
A client that uses blocking sockets. This is the simplest of the programs here
- "Asynchronous I/O Client"
A client that uses asynchronous sockets. This program is a GUI MFC program, but it does not use 'CAsyncSocket' or its derivatives.
- "CAsyncSocket-based Client"
A client that uses MFC's asynchronous sockets wrapper class: 'CAsyncSocket'
- "Basic Blocking Server"
A simple server that uses blocking sockets.
- "Multithreaded Server"
A server that sends each new connection off to its own thread to be handled while the main thread sits in a loop accepting connections.
- "select()-based Server"
A server that handles many connections using the 'select()' function to manage them all within a single thread.
Last edited by Andreas Masur; July 25th, 2005 at 02:31 PM.
Click Here to Expand Forum to Full Width