1

In python, tcp connect returns success even though the connect request is in queue at server end. Is there any way to know at client whether accept happened or in queue at server?

5
  • 1
    Possible duplicate of stackoverflow.com/questions/177389/… Commented Jan 4, 2018 at 6:27
  • No.. that thread isn't related to my question @jwpfox Commented Jan 4, 2018 at 6:31
  • In blocking mode, when connect(2) returns, you have an established connection. (Whether or not accept(2) has been executed on the remote-side has no significance.) Commented Jan 4, 2018 at 10:49
  • Yes it is. Think about it a bit more. Commented Jan 4, 2018 at 11:01
  • @LorinczyZsigmond Only after accept executes on server side, complete connection is established and data transfer is possible. While socket is in queue, we can't send data from client to server. Commented Jan 5, 2018 at 6:34

2 Answers 2

0

The problem is not related to Python but is caused by the underlying socket machinery that does its best to hide low level network events from the program. The best I can imagine would be to try a higher level protocol handshake (send a hello string and set a timeout for receiving the answer) but it would make no difference between the following problem:

  • connection is queued on peer and still not accepted
  • connection has been accepted, but for any other reason the server could not process it in allocated time
  • (only if timeout is very short) congestion on machines (including sender) and network added a delay greater that the timeout

My advice is simply that you do not even want to worry with such low level details. As problems can arise server side after the connection has been accepted, you will have to deal with possible higher level protocol errors, timeouts or connection loss. Just say that there is no difference between a timeout after connection has been accepted and a timeout to accept the connection.

Sign up to request clarification or add additional context in comments.

Comments

0

If connect returns and there is no error, the TCP 3-Way Handshake has taken place successfully.

  1. Client: connect sends a SYN (and blocks)
  2. Server: (blocking on accept) sends a SYN,ACK
  3. Client: connect sends an ACK

After 3, connectgives control back to you on the client side and accept also gives control back to the caller on the server side.

Of course, if the server is fully loaded, there is no guarantee that the wake-up of accept means actual processing of the request, but the fact that connect has woken up and returned with no error is a guarantee of having successfully set-up the TCP connection.

Packets can be sent.

For a good explanation see for example:

And head to the The 3-way TCP handshake section

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.