Re: socket select send

From: Maxim Yegorushkin (maxim.yegorushkin_at_gmail.com)
Date: 07/29/05


Date: 29 Jul 2005 11:05:33 -0700

Wenfei wrote:
> Thanks for your quick reply, Maxim.
>
> >if (FD_ISSET(j, &master)) {
> >// except the listener and ourselves
> > if (j != listener && j != i) {
> I am wondering, besides i and listener, whoelse is in the master set?

There are listening and accepted sockets (i.e. client connections) in
the master set.

> The following is my code( not completed), do you think it is right? if
> not, could you please correct it?
> Thanks,
>
> //for receiving:
> void receiving(){
> ...
> while(1){
> FD_SET(sockfd, &fdwrite);
> FD_SET(sockfd, &fdread);
> listen(sockfd, NET_TCP_MAX_SYN_BACKLOG);
> select(maxfd+1, &fdread, &fdwrite, ...);
> if ( FD_ISSET(sockfd, &readFDList) )
> connectionfd = accept(sockfd, ...);

You should check accept() result because it may fail.

> if (connectionfd > maxfd)
> connectionfd = maxfd;
> FD_SET(connectionfd, &fdread);
> FD_SET(connectionfd, &fdwrite);

As you just set connectionfd it will be processed in the following loop
as ready for read, although it may not.

> // run through the existing connections to recvive
> for( i = 0; i <= maxfd; i++ )
> if ( (FD_ISSET(connectionfd, &fdread)) && (i != sockfd) )
> recv(connectionfd, ...);
> }
> ...
> }
>
> //for sending:
> void sending(){
> ...
> for( i = 0; i <= maxfd; i++ )
> if ( (FD_ISSET(connectionfd, &fdwrite)) && (i != sockfd) )
> send(connectionfd, ...);
> ...
> }
>
> Here fdwrite and connectionfd are global variables.

Man, get yourself a book, they discuss in details all those things.