| 
Message-Oriented TCP and Multithreaded Client/Server
ID: Q192570
 
 | 
The information in this article applies to:
- 
The Microsoft Foundation Classes (MFC)
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 Q175668, 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 Q185728 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. Click the file names below to download the files:
MFCAsync.exe
For more information about how to download files from the Microsoft
Download Center, please visit the Download Center at the following Web
address
http://www.microsoft.com/downloads/search.asp
and then click How to use the Microsoft Download Center.
The flow of logic:
- AsyncServer listens on port 9898.
- AsyncClient spawns a thread, which connects to the AsyncServer.
- AsyncServer accepts the connection and spawns a thread to handle the
   socket communication.
- AsyncClient sends data packet to AsyncServer.
- AsyncServer receives and sends the same data packet back to AsyncClient.
- AsyncClient receives data packet, reverses the data packet and sends
   back to AsyncServer.
- Repeat steps 5 and 6 until being disconnected either by the AsyncClient
   or AsyncServer.
Steps to run the sample:
- Run AsyncServer.exe on machine A.
- Run AsyncClient.exe either on machine A or machine B.
- On the AsyncClient side, enter the server machine name where AsyncServer
   is running. Click Connect. Enter text in the Send edit box. Click Send.
- When you are done with the test, click Disconnect on the AsyncClient
   side.
REFERENCES
For more information, please see the following articles in the Microsoft
Knowledge Base:
Q175668
FILE: MultiSoc: Illustrates Using Sockets in Multiple Threads
For more information about downloading files from the Microsoft Software
Library, please see the following article in the Microsoft Knowledge Base:
Q185728
SAMPLE: MFCSocs.exe Avoids Two Common MFC Socket Mistakes
Additional query words: 
CAsyncSocket multithreaded socket thread message 
Keywords          : kbfile kbnetwork kbAPI kbMFC kbWinOS95 kbWinOS98 kbWinsock kbGrpNet 
Version           : winnt:
Platform          : winnt 
Issue type        :