How to set socket connect timeout at TCP level for non blocking socket in ASIO?

I have an application that uses non-blocking Boost ASIO socket to connect to a remote TCP server.
I wanted to set a timeout on TCP connect and I see the only way to do it is use an application
level timer.

However this is not a reliable way as this is prone to issues:

  1. In a CPU starved scenario, the timer might start way before the actual SYN packet is sent.

I want to rely on OS’s TCP timers to see if actually the connect timeout at TCP level.
I could not use non blocking sockets.

Is there a way to do it reliably in asio?

I tried looking into the official examples

Read more here: Source link