Anyone know how to use socket select() function?

hello socket programming expert,

I having difficulties in understanding how select() function in socket programming work.... I'm trying to create my own peer-to-peer chat or file transfer program by using the select() function.... Therefore does anyone had any tutorial or source code that related to either one of the program, which I'm trying to create?

My problem is how to get the "write" in select() function working?
Let say:

//server.c, which is the server side source code

........

select(highsocket+1, NULL, &writeset, NULL, (timeval*)0);

............

if (FD_ISSET(socket,&writeset)) 
{
    printf("Server is writing.......\n");
}

.............

and now, how do I get the server side to print the word "Server is writing......." ? I hope you guy understand what I mean.......

btw, I'm using NetBDS and using C programming....... hoping someone can help me out :smiley:

Thank you

I've attached a hasty, single process server (without any signal handling) example that works on my FreeBSD 6.0 host. Hopefully it will be of assistance. I'd suggest running indent against it. There are also very good arguments for not using select in favor of pselect. Also for high capacity servers maybe kqueue would be better.

HTH

Thank you for reply and your example is very good :smiley:

btw, I will come back later in time to report my progress on how is the development is going on :smiley:

My 'liedentd' is a simple select(2)-based server program that is a good example of how to write a high-performance select loop. It is available in the FreeBSD ports system, under security/liedentd, or you can fetch the source from:

ftp://ftp.freebsd.org/pub/FreeBSD/ports/local-distfiles/wes/liedentd-1.1.tar.gz

The entire source code is in liedent.d, about 300 lines of C source.

Thank you :smiley: