Re: socket select send

From: Wenfei (ye_wenfei_at_hotmail.com)
Date: 07/29/05


Date: 29 Jul 2005 11:42:39 -0700


Maxim Yegorushkin wrote:
> 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.

"listener" is the listening socket, and "i" are accepted sockets.
Besides those, anything else?
 I mean (j != listener && j != i) will give you no sockets.

>
> > 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.

This is the same logic as your link
http://www.ecst.csuchico.edu/~beej/guide/net/examples/selectserver.c
which set newfd after select() and accept(). It will not be processed
in the following loop until next turn while loop or your for(;;) loop
if you are right.

>
> > // 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.

I did. but they didn't talk about what I am concerned. And also, for
your link
http://www.ecst.csuchico.edu/~beej/guide/net/examples/selectserver.c
I found, it didn't use writefd in select() and didn't check writebility
before send().

Thanks,



Relevant Pages

  • Re: Network Sockets Question
    ... When the message sent to the listener application is ... > goes back and waits for another socket. ... > that are called within this loop. ... Again, using MSDN, I ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Socket
    ... The old mswinsck control you reference to did the same exactly thing, ... listened in a loop for when data is incoming. ... > uses an endless loop that verify the buffer of the socket to see ... is there a way to set an event listener that will activate ONLY ON ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Java Reflection with local variables
    ... variables, global variables, objects, methods, while loop ... Also, you seem at least somewhat experienced, but I want to point out that most of the time this is done not with a debugger but by having the class do this work for you. ... In Java, you can do this with a Listener, an Observer and an Observable, or a Property Change Listener. ...
    (comp.lang.java.programmer)
  • Re: Detecting object deletion while in a loop
    ... > The listener array consists of a derivation of a listener class. ... > the Destroy will invalidate the listener array. ... ThrowEvent can increment the reference count. ... Then during the loop, if the count has gone back down to one, the loop ...
    (comp.lang.cpp)
  • Re: socket select send
    ... > I am wondering, besides i and listener, whoelse is in the master set? ... > void receiving(){ ... As you just set connectionfd it will be processed in the following loop ...
    (comp.unix.programmer)