views:

220

answers:

2

I call connect() on the client. The client enters the SYN_SENT state and sends a SYN. Now it gets a SYN with no ACK in it, so the client enters the SYN_RCVD state.

Does connect() return at this point? Technically you have enough information to be able to call send() and recv() on the socket. The RFC itself says, if you call SEND on a socket in the SYN_RCVD state, to:

SYN-RECEIVED STATE

  Queue the data for transmission after entering ESTABLISHED state.

And, if you call RECEIVE:

LISTEN STATE
SYN-SENT STATE
SYN-RECEIVED STATE

  Queue for processing after entering ESTABLISHED state.  If there
  is no room to queue this request, respond with "error:
  insufficient resources".

So my question is: does connect() return after getting the SYN, and then a call to recv() would block, or does connect() itself block until the connection is established fully?

+3  A: 

As a point of reference, the implementation of connect() in TCP/IP Illustrated, Volume 2 by Wright and Stevens will block until the connection is fully established (if the connect() call is set to block).

Michael Burr
A: 

The client will only receive a SYN with no ACK in the case of a simultaneous connect. The much more common sequence of events, with a normal blocking socket:

  • Client application calls connect(), client sends SYN and enters state SYN-SENT
  • Client receives SYN+ACK from server; client sends ACK and enters state ESTABLISHED; the application's call to connect() returns.
Miles
Yeah, I realize this is really uncommon, I'm just trying to cover corner cases
Claudiu