views:

56

answers:

1
+1  Q: 

select() function

in socket between server and many clients we need select().i want to know where shoude be the select() function?server or client? if it should be in server so what changes we should make in client

+3  A: 

The use of select() doesn't affect the semantics of socket communication at all. It just provides a way to wait on multiple sockets (or files) simultaneously, and to subsequently find out which ones are ready to operate on. Since it's the server that typically has many connections, it's the server that calls select(). Nothing special has to be done on the client.

Marcelo Cantos
Unless the client waits on a reply from the server, in which case the client will be using select().
Blank Xavier
@Blank: No, the client just calls `send()` and `recv()`, which will naturally block if the client end of the socket is not ready to perform the requested operation. The `select()` function exists solely for the case when you want to wait on multiple file descriptor events, and you don't know which will be ready first. It is a waste of effort to use it for just one descriptor (unless you want to do some kind of full-duplex asynchronous protocol as opposed to the far more common request-response pattern).
Marcelo Cantos
Your program design may be such that you do not wish / cannot afford to block on send/recv; you need them to occur only when they can occur, so the functions return immediately. The sockets may not be the only thing you are paying attention to.
Blank Xavier
@Blank: sure, but it doesn't follow that, "the client *will* be using select()". If you had written, "the client *may* be using select()," I'd have no issue with that.
Marcelo Cantos
Mmm - I think I get away with it - I wrote "unless the client waits on a reply" - admittedly I make an unstated assumption the client has other things to do -and- is waiting for a reply (e.g. requires select()) - but to be fair I've never, not once, in twenty years, written a program which does a blocking wait on a socket!
Blank Xavier
@Blank: If that was your intended meaning, then well and good. But most people equate "wait" with "block". Even in the literature, the term "wait-free" implies non-blocking semantics. Also, are you saying that in the very first socket program you ever wrote, you called `select()` first, and then `recv()` or `send()`? If so, you should probably contact Guinness World Records, or something.
Marcelo Cantos