During the interaction between my server and the lower computer (hardware device, no operating system), I need to configure commands for the lower computer.
If the logic is normal, the lower computer opens the server, and my program connects and sends operation commands, the logic will be very simple. However, it is very difficult to switch between the master and slave computers. It can be used only as a host or only as a slave machine. As a result, the software sacrifices and writes a lot of disgusting code. Cry ..............
First, I created a message queue in the program. The content in the queue is the command that the program needs to actively call to the lower computer.
Then, enable the listener. After receiving the connection to the CMD port, I set up a thread in which I constantly retrieve data from the message queue and decide what command to send to the lower computer based on the message content.
If a new connection comes when a connection exists, I will push the close thread command to the queue. When the old thread obtains the message from the close command, it will exit the thread safely.At the same time, in my listening thread, I have been waiting for the exit of the old connectionWhen waitforsingleobject waits for the desired signal, I start a new thread and push a set info command to the Message Queue after it is enabled successfully.
The symptom is that in some cases, after setting info, the program disconnects the socket. I really suspect it has something to do with this usage mode.
To be continued .....