Moisés Navarro Marín wrote:
>
> Hi.
>
> I'm using 'select' to detect which client is sending information to
> the server. So I got the following code:
>
> ==============================
>
> while (TRUE)
> {
> FD_ZERO (&clients_fds); /* This call to FD_ZERO is here to update
> the info about active clients in every loop*/
> for (i=0; i < MAX_CONNECTIONS; i++)
> if (client_sockets[i].socket > 0)
> FD_SET (client_sockets[i].socket, &clients_fds);
>
> numSelected = select (max_fd (client_sockets)+1, &clients_fds,
> (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
>
> ...
> }
>
> ===============================
>
> Well, the question is what am I doing wrong because 'select' never
> returns even though client is calling 'sock_write' in an infinite loop
> without errors!!! :?
First, I think you want to start your select with:
select(max_fd (i), ...)
Secondly, I don't understand the first part. Why zero out the clients
and then re-set them? A better idea is to create a "pristine" fd struct
that you can just copy over the returned one every time. Your loop
apparently figures out which sockets are still active, but select can do
that for you because a file descripter will come back as readable with 0
bytes and you can close it (marking your "pristine" structure at the
same time). This will cut out a loop from your server.
I have a very short (200 lines) IRC-like server/client written to teach
myself about select (and sockets) that illustrates this that I'd be
happy to share with you.