[ee122] socket no longer in use

vern at cs.berkeley.edu vern at cs.berkeley.edu
Sun Sep 23 00:29:46 PDT 2007

> I was wondering if there is a function that can tell you that a socket is no
> longer used. For example, if we use select, how do we know when we are going
> to close the connection and take the connection out of the list of
> connections.

When the remote end closes the connection, select() will indicate that it
is available for reading, but when you do so, read() will return 0 bytes
indicating that the connection is now closed.  At that point, you need
to stop using the descriptor in any future select() calls.

> I was wondering if the read set and write set for the select() call can be
> the same set. Also when do we need the except set? Thanks!

They should not be.  Furthermore, you should only be using a read set.
Write sets are for use with non-blocking I/O (which we're not using in
this project), and except sets for exceptional conditions, which we
likewise to do not ask you to handle in this project.

> Also can we use fork instead of select to select connections.

No, you need to use select().  The reason for this is that select() is
*the* bread-and-butter way under Unix to handle multiple input streams in
C/C++.  It is heavily used in practice, so you will benefit from being
familiar with it.


More information about the ee122 mailing list