Re: TCP connect in Non Blocking Mode



Rainer Temme <Rainer_Temme@xxxxxxxxxxxxxxxxxxxxxx> writes:
David Schwartz wrote:

[...]

If the connection succeeds, it will be writable but not readable,
so checking for 'read'ability won't detect connection success.
This is why the typical pattern is to check an asynchronous 'connect'
attempt only for writability.

Whether a socket will become readable after connect
also depends on the protocol used ... SMTP and FTP
are exaples of protocols where the serverside
will send an immediate message to the client
after successful connection.
Anyhow, my suggestion was to set the bit in the readset
as well as in the writeset. You might regard this as redundant
but (as even you had to admit) it will not harm.

The to-be-expected behaviour is actually standardized:

If the connection cannot be established immediately and
O_NONBLOCK is set for the file descriptor for the socket,
connect() shall fail and set errno to [EINPROGRESS], but the
connection request shall not be aborted, and the connection
shall be established asynchronously. Subsequent calls to
connect() for the same socket, before the connection is
established, shall fail and set errno to [EALREADY].

When the connection has been established asynchronously,
select() and poll() shall indicate that the file descriptor
for the socket is ready for writing.

http://www.opengroup.org/onlinepubs/009695399/functions/connect.html

Additionally, 'cargo cult programming'

http://catb.org/jargon/html/C/cargo-cult-programming.html

does harm. It causes the technical damage of keeping the machine busy
with doing useless instead of useful things. But that's the less
serious problem. The much more serious one is that code is
harder to understand and change meaningfully when it is more
complicated and the most complicated thing to understand is something
which defies understanding because it is inherently without meaning.
The latter becomes especially nasty when someone different from the
original author inherits the code.

.



Relevant Pages

  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation [try #3]
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... kernel socket family. ... presentation side is left to the client. ... Each connection goes to a particular "service". ...
    (Linux-Kernel)
  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... Make it possible for the client socket to be used to go to more than one ... Each connection goes to a particular "service". ...
    (Linux-Kernel)
  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation [try #2]
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... Make it possible for the client socket to be used to go to more than one ... Each connection goes to a particular "service". ...
    (Linux-Kernel)
  • Re: Socket Exception
    ... In this case the server most likely rejected your ... connection. ... Each command is a new socket connection that is opened and closed ... /// Required designer variable. ...
    (microsoft.public.win32.programmer.networks)
  • Re: attn:MVPs
    ... winsock dll to the managed .net environment. ... I have an asynchronous TCP socket that listens for data from ... The Slave should not initiate any connection requests/ send other messages ... EXCEPT the acknowledgement for the incoming messages from master, ...
    (microsoft.public.dotnet.framework)