Microsoft KB Archive/192570

= Message-Oriented TCP and Multithreaded Client/Server =

Article ID: 192570

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft Foundation Class Library 4.2

-



This article was previously published under Q192570



SUMMARY
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.

Here are some highlights of this sample:


 * MFCAsync.exe follows KB article 175668, showing the use of Transmission Control Protocol (TCP) socket communication in a separate thread other than the Graphics User Interface (GUI) thread; as such, the blocking of the GUI message pump does not affect the delivery of socket messages. Multiple instances of AsyncClient can talk to the same instance of AsyncServer.
 * MFCAsync.exe also complies with KB Article 185728 in issuing of socket Send and Receive calls.
 * MFCAsync.exe adds message boundary on top of the stream-oriented TCP. Each message is sent with a 4-byte-length packet header followed by the body of the message. Users can append more customized packet header fields to this length field.



MORE INFORMATION
The following files are available for download from the Microsoft Download Center:

MFCAsync.exe

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

The flow of logic:


 * 1) AsyncServer listens on port 9898.
 * 2) AsyncClient spawns a thread, which connects to the AsyncServer.
 * 3) AsyncServer accepts the connection and spawns a thread to handle the socket communication.
 * 4) AsyncClient sends data packet to AsyncServer.
 * 5) AsyncServer receives and sends the same data packet back to AsyncClient.
 * 6) AsyncClient receives data packet, reverses the data packet and sends back to AsyncServer.
 * 7) Repeat steps 5 and 6 until being disconnected either by the AsyncClient or AsyncServer.

Steps to run the sample:


 * 1) Run AsyncServer.exe on machine A.
 * 2) Run AsyncClient.exe either on machine A or machine B.
 * 3) On the AsyncClient side, enter the server machine name where AsyncServer is running. Click Connect. Enter text in the Send edit box. Click Send.
 * 4) When you are done with the test, click Disconnect on the AsyncClient side.

