MFC Guide: Handling Multiple Data Streams
There are two common methods for receiving two streams of data in MFC.
- To receive data using two different socket objects: You can create two separate CSocket objects, connect them to different data sources, and then use the corresponding Receive function to receive the data.
CSocket socket1, socket2;
socket1.Connect(server1);
socket2.Connect(server2);
char buffer1[1024];
char buffer2[1024];
socket1.Receive(buffer1, sizeof(buffer1));
socket2.Receive(buffer2, sizeof(buffer2));
- To receive data simultaneously using multiple threads, one option is to create two separate threads dedicated to receiving data from different sources. In each thread, a CSocket object can be created and the corresponding Receive function can be used to retrieve the data.
UINT ThreadFunc1(LPVOID pParam)
{
CSocket socket1;
socket1.Connect(server1);
char buffer1[1024];
socket1.Receive(buffer1, sizeof(buffer1));
return 0;
}
UINT ThreadFunc2(LPVOID pParam)
{
CSocket socket2;
socket2.Connect(server2);
char buffer2[1024];
socket2.Receive(buffer2, sizeof(buffer2));
return 0;
}
AfxBeginThread(ThreadFunc1, NULL);
AfxBeginThread(ThreadFunc2, NULL);
The two common methods mentioned above can be chosen based on specific needs and situations. It is important to note the synchronization between threads and data handling when dealing with receiving data from multiple threads.