The information in this article applies to:
SUMMARY
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.
http://www.microsoft.com/downloads/search.aspand then click How to use the Microsoft Download Center. MORE INFORMATIONServerThe server illustrates using sockets in multiple threads in an MFC application. The server listens for connections. When a new connection is requested, the server accepts the connection and then creates the thread to handle the connection. When the server receives a message, it reverses the message and sends it back to the client.In MFC when you have a new connection it is necessary to accept the connection in the thread that the listening socket is in. The Accept call requires that you pass in a CAsyncSocket object. MFC then sets up everything correctly so that the connection can be handle in the thread. If you want to handle this connection in a different thread, just passing the MFC object to the thread will not work correctly. To correctly set up everything for the connection to be handled in a different thread, the following steps are required:
The following code from the Server project illustrates this: OnAccept for the listening socket.
InitInstance of the thread.
The above code makes sure that the socket is set up correctly in the
secondary thread.
ClientThe client accepts a host name to connect to. Once the connection is made, the client allows you to send messages to the server. The client then displays the message returned from the server.The server listens on port 9000. The client tries to connect to this port on the specified host. The projects were created with Visual C++ version 5.0. However the code in the samples should apply to the versions of MFC mentioned above. (c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Sridhar S Madhugiri, Microsoft Corporation © Microsoft Corporation 1997, All Rights Reserved. Additional query words: socket thread multithreaded
Keywords : kbfile kbnetwork kbsample kbAPI kbMFC kbSDKPlatform kbThread KbUIDesign kbVC kbVC210 kbVC220 kbVC400 kbVC410 kbVC420 kbVC500 kbVC600 kbWinsock kbfaq kbGrpNet |
Last Reviewed: January 12, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |