Using select:
assuming you have a valid return from
accept stroed in
cli, you will want to set up a watch on that file descriptor.
To do that you create a variable of type
fd_set, call it
read (since you will be reading from it).
Provided for you are macros/functions (they could be either, you dont care) for assigning
cli to
read.
Call in this order:
FD_ZERO (&read); /* clear the current contents of read */
FD_SET (cli, &read); /* add cli to the fd_set read */
/* alert select that it should watch for status change in the read fd_set
* the three NULLs indicate:
* no watch on a write fd_set,
* no watch on an exception fd_set,
* no timeout (this is a blocking call)
*/
err = select (cli + 1, &read, NULL, NULL, NULL);
/* this can return errors, check for them */
if (!(FD_ISSET(cli, &read))) {
/* do something appropriate here */
}
/* Ok, so now select let us know that there was a change in one of the fd_sets in read
* and we have checked to see that it is the one we are looking for
*/
/* usually you would do something like:
* nread = recv (cli, &msg, target_read, MSG_WAITALL);
* at this point
*/
That is the function of select.
Although it can be used with the timeout feature to provide a fairly decent timer function.
In response to your chat program (IM, whatever) - google around for the Windows applications of threads (I provided some links earlier in the post.
If you still cant get it, and others here cant make it clear, I'll give a last ditch effort.
Try it yourself first though, I think you might suprise yourself.