0

I am developing an MFC dialog application in VS2019 c++. Application requires multiple android devices to be connected as client to a windows application server through USB. Connecting multiple devices one by one at the starting of the windows application is working fine. But once an device is disconnected from USB, and reconnected again, the server is not accepting the client.

I tried using a thread detach() only to accept() clients in background.

UINT CCheckDlg::bindAndListen(LPVOID Param)
{
    while (true)
    {
        ClientSocket = accept(ListenSocket, NULL, NULL);
    }
    return 0;
}

But this loop breaks out after 2 iterations, and while this loop runs, other UI operations of the Application are blocked also.

For the android client side, as soon as the android application catches socketExceptions, it tries to reconnect to server. When the USB is connected again, the try continues.

How can i have a thread in windows application which is runs in background accepting clients, without blocking the UI operations?

Jamiul alam khan
  • 159
  • 1
  • 10
  • You should never have long loops in an event-driver program (like a GUI program). That makes the program run only your loop and not the event-handling loop. On Windows using MFC there are better and event-driven ways to handle sockets, where you can handle "new connections" the same way as any other GUI event. – Some programmer dude Feb 26 '20 at 07:01
  • CreateThread API should do that for you – Sanjeev Feb 26 '20 at 07:01
  • Please extract a [mcve] from your code for posting here. What you posted here is unclear, because the loop can not and will not "break out after 2 iterations". Concerning that this is a blocking operation, that's a known issue, you will need a background thread for that. Also, consider using a higher-level framework like ZeroMQ instead of raw sockets, it simplifies a few things. – Ulrich Eckhardt Feb 26 '20 at 07:01
  • You should have a thread which will get blocked in accept call and as soon as client get connected then again it should get blocked in accept call to accept another connection. – Sanjeev Feb 26 '20 at 07:03
  • These articles may be helpful. [How to connect multiple clients to a single server in c++ on Windows - Visual Studio?](https://stackoverflow.com/q/42643276/9014308), [Single TCP/IP server that handles multiple clients (in C++)?](https://stackoverflow.com/q/25091148/9014308) – kunif Feb 26 '20 at 11:19

0 Answers0