I am developing an application that uses TCP/IP to communicate with a custom device. In one loop (test result loop) I am constantly using a TCP read to monitor incoming information from my device. This aspect of my program works as designed. I encountered problems when I tried to set up a handshake with the device to monitor whether the device was still connected. To do this I set up another loop (handshake loop) in which I am sending a command that asks for the current version of my device. If it returns the correct string, I know I am still connected.
However there seems to be timing issues where I will sometimes read the handshake information in the loop designed to wait for test results, and vice versa. The incoming test results are received at completely random intervals so I need to make sure my test result reads are uninterrupted.
What's the best way to code this?