Advertisement

listen() and accept() question

Started by January 06, 2003 09:02 PM
1 comment, last by Pfhorseti 22 years, 1 month ago
I am not quite sure how listen() works. Once you call listen() on a listening socket, does it KEEP listening forever until you, say, close() the socket? Or does it listen only when listen() is called? Which in that case I would need to put it in a loop to keep listening. if the aforementioned is the case, then would this structure work:

// init stuff.
.
.
.
listen();
// now the listener socket is listening.
for(; {
accept();
// accept() will block until there is a new connection.
// when new connection arrives, notify the main thread
// and pass it the fd # for the new connection.
}
 
Thanks, -yuki
-forseti
You only need to listen() once. listen() puts the socket into a listening state, ready for connections. You can then do accept() to accept a connection (or wait if theres no connection ready). If you want to test for a connection, use select(), and test for a read operation with a timeout of zero seconds, 0 miliseconds.

Edit: Yep, that code will work fine - so long as you remove the smiley



[edited by - Evil Bill on January 6, 2003 10:11:20 PM]
Member of the Unban Mindwipe Society (UMWS)
Advertisement
Ahh, thanks for clearing things up.
-forseti

This topic is closed to new replies.

Advertisement