Does connect() block for TCP socket?

There’s hardly any “immediately” regarding networking, stuff can be lost on the way, and an operation that should be performed immediately in theory might not do so in practice, and in any case there’s the end to end transmission time.

However

  • connect() on a TCP socket is a blocking operation unless the socket descriptor is put into non-blocking mode.
  • The OS takes care of the TCP handshake, when the handshake is finished, connect() returns. (that is, connect() does not block until the other end calls accept())
  • A successful TCP handshake will be queued to the server application, and can be accept()’ed any time later.

Leave a Comment