tags:

views:

99

answers:

1

Hello folks,

I recently began learning network programming under Linux and it seems I can't figure out the right way to detect if a connection to remote host has been established. ATM all non-established connections are registered in a epoll instance. Once an event with the EPOLLOUT flag set to 1 arrives, the connection is marked as established. As easy as this, but I'm doing this right ...

Is there a better way of doing this? And if not, does waiting on a socket to become writeable is a good way to guarantee that connection has been established?

A: 

poll/epoll/select should all work. The man-page for connect(2) says you get EINPROGRESS, and that you can detect completion by using select or poll to check for it to become writable. Then use getsockopt to check the SOL_SOCKET, SO_ERROR status, to see if connect succeeded or failed.

If you retry connect while it's still trying, you get EALREADY.

So this is the documented way to do it, and sounds right to me.

Peter Cordes