Skip to contentSkip to conversation

connect() to localhost non-blocking.

DISCUSSION Dan Kegel 2 participants 2 messages Oct 21, 2001 – Oct 22, 2001

lore
Share & tools
lore

Dan Kegel

author
#0 lore raw
Quoted 14 lines
Quoted 12 lines · Patrick Mau

I wrote a little test program to do some poll() benchmarks. I changed the host address to localhost and observed that connect() always returns EINPROGRESS when used with non-blocking sockets.

Quoted 1 line

From the man page:

EINPROGRESS
The socket is non-blocking and the connection cannot be completed immediately. It is possible to select(2) or poll(2) for completion by selecting the socket for writing.

Quoted 8 lines
Quoted 5 lines

So my question is:

What is meant by 'cannot be completed immediately' ? I thought that connections to localhost would complete without any delay when the application listens ?

Probably the accept()ing process hasn't been scheduled yet. EINPROGRESS is a perfectly reasonable response in such a case.

You have to be prepared to handle both immediate and delayed connection, especially if you want to be portable. (Solaris behaves a bit differently than Linux in this regard.) See
http://www.kegel.com/dkftpbench/dkftpbench-0.37/ftp_client_pipe.cc
for an example of how to handle nonblocking connects more or less portably. (You have to wade through quite a bit of code, tabstops 4, to find all the connect-handling stuff -- sorry.)
- Dan

#1 lore raw
Quoted 2 lines · Dan Kegel
Quoted 2 lines

Patrick Mau <[email protected]> writes:

[Snip]

Quoted 8 lines

You have to be prepared to handle both immediate and delayed connection, especially if you want to be portable. (Solaris behaves a bit differently than Linux in this regard.) See
http://www.kegel.com/dkftpbench/dkftpbench-0.37/ftp_client_pipe.cc
for an example of how to handle nonblocking connects more or less portably. (You have to wade through quite a bit of code, tabstops 4, to find all the connect-handling stuff -- sorry.)
- Dan

Hallo Dan,

thnaks for the pointer and your remarks, but I really wondered if linux _never_ immediatly accept()'s connections.

I never saw a connect() call returning 0 (connected) on a non-blocking socket. Always EINPROGRESS. I guess I have to look at the kernel to see scheduling effects ...

thanks,
Patrick