Alex Rivera | Logout

Check if socket is listening in C

Asked 2012-04-21T15:59:31.360
16

While iterating through socket file descriptors, how can I check if one of them is from a passive socket (listening for connections)?

Edit
Report

1 Answer

0

Strictly speaking, you could try performing an operation on the socket which would incidentally determine what type of socket it is, like trying to accept a connection from it. If accept() fails with EINVAL, that's a pretty good sign that it isn't listening. :)

Keeping track of which sockets are which is a better solution overall, though. Unless you're building a really trivial application, chances are that you'll need to keep some sort of additional data on each socket anyway.

answered 2012-04-21T17:58:09.970

Your Answer