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?
-
1Possible duplicate of stackoverflow.com/questions/177389/…jwpfox– jwpfox2018-01-04 06:27:56 +00:00Commented Jan 4, 2018 at 6:27
-
No.. that thread isn't related to my question @jwpfoxAnand– Anand2018-01-04 06:31:05 +00:00Commented 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.)Lorinczy Zsigmond– Lorinczy Zsigmond2018-01-04 10:49:03 +00:00Commented Jan 4, 2018 at 10:49
-
Yes it is. Think about it a bit more.jwpfox– jwpfox2018-01-04 11:01:04 +00:00Commented 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.Anand– Anand2018-01-05 06:34:55 +00:00Commented Jan 5, 2018 at 6:34
2 Answers
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.
Comments
If connect returns and there is no error, the TCP 3-Way Handshake has taken place successfully.
- Client:
connectsends aSYN(and blocks) - Server: (blocking on
accept) sends aSYN,ACK - Client:
connectsends anACK
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