How to thread a TCP connection ?

Hello,

My project is going well and the interface is now finished but there is always a major issue to solve : the application freezes when trying to connect to an invalid IP address.

So i would like to put this connection in a dedicated thread. BUT : i think i can see how threads seems to work but i can’t evaluate how to implement it as i need to pass arguments to the thread and return arguments to the main thread.

In my class i have these usefull variables :

  • bool connected // connected or not
  • string ip_add // IP address of the target device
  • int port // port of target device
  • string rep // device response
  • string command // command sent to the device

My synoptic is as follow :

  • if not connected, try connecting to ip_addr:port
  • if connected = false, print “connection error”
  • else print “connected to ip_addr:port”
  • send command to the device
  • read device response
  • close session
  • return response rep

I tried to thread the connection trial but :

  1. i can’t manage to make it work !
  2. I don’t think it is sufficient. Should i put ALL network requests in thread ?!

I can post the entire code if needed, it would be great if someone can help me !

thanks a lot