[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: I/O multiplexing among socket descriptors



Hi

Check for select return value.

accept and select functions can be interrupted. So you need to check
errno value and see whether its EINTR and continue.


Something like


for(;;) {
    FS_SETs


    status = select(..);
    if (status < 0) {
	if (errno == EINTR)
	    continue;
	else {
	    perror("select");
	    break;
	}
    }
}

Regards
-Bala

> 
> Hi,
> 
> Thanks for helping me out. I am yet to use poll. But I am still having
> problem with select. In the following code both the FD_ISSET macro is
> returning 1 for both the calls to select even before data has arrived on
> them ! So the recvfrom calls are proceeding sequentially and until data
> arrives in the recvfrom call it is blocking.
> 
> for(;;)
>     {
>       FD_SET(sockfd1,&set);
>       FD_SET(sockfd2,&set);
>       maxfd1 = max(sockfd1,sockfd2) + 1;
>       select(maxfd1,&set,&set,NULL,&time);
>       printf("\n sokcfd1 status = %d",FD_ISSET(sockfd1,&set);
>       printf("\n sokcfd 2 status = %d",FD_ISSET(sockfd2,&set);
> 
>       if(status = FD_ISSET(sockfd1,&set))
>        {
>          bzero(buff1,sizeof(buff1));
>         count = recvfrom(sockfd1,(char *)buff1,sizeof(buff1),0,(struct
> sockaddr *)&client,&addrlen);
>          printf("\n %s",buff1);
>        }
> 
>          if(status = FD_ISSET(sockfd2,&set))
>         {
>          bzero(buff2,sizeof(buff2));
>         count = recvfrom(sockfd2,(char *)buff2,sizeof(buff2),0, (struct
> sockaddr *)&client,&addrlen);
>          printf("\n %s ",buff2);
>          }
>      }
> }
> 
> So i decided to make the recvfrom call non-blocking by using the flag
> MSG_DONTWAIT flag option. and i changed the code for both the sockets as
> follows :
> 
> count = recvfrom(sockfd2,(char *)buff2,sizeof(buff2),MSG_DONTWAIT, (struct
> sockaddr *)&client,&addrlen);
>  if(errno != EWOULDBLCOK)
>          printf("\n %s ",buff2);
> 
> 
> but in this case both the sockets are not accepting data from their
> respective buffers. I am still not able to identiful the problem. Could you
> please help me out. I am refering Unix Network Programming by Richard
> Stevens, edition 2.
> 
> Regards,
> Lakshmi
> 
> 
> 
> 
> 
> 
> 
> 
> ----- Original Message -----
> From: "Ch Rama Krishna Prasad" <rkp@xxxxxxxxxxxxx>
> To: <linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>; "Lakshmi Gopinath"
> <lakbala@xxxxxxxx>; <linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>
> Cc: <ilugc@xxxxxxxxxxxxxxxxxx>
> Sent: Monday, February 12, 2001 10:03 AM
> Subject: Re: [LIP] I/O multiplexing among socket descriptors
> 
> 
> > Hi Lakshmi gopinath,
> >    I want to say one thing is that in select also we can set the events
> > intersted in so taht when the data comes on the socket it will set the
> revents
> > .. If the revents is read event then we can read the data and if the event
> is
> > write event you can send the data, For more see man on poll . if you have
> any
> > doubts contact me.
> >    On more thing poll is better than select so use poll.
> >
> > From
> > Ch.R.k.Prasad
> >
> > On Sun, 11 Feb 2001, Lakshmi Gopinath wrote:
> > > Hi Rama Krishna Prasad,
> > >
> > > Thanks for helping me out.But I am still facing some problems. fcntl can
> be
> > > used to set the socket in non-blocking mode. but the purpose of
> select/poll
> > > is that the process should block at select or poll instead of call to
> read
> > > or write or the like , till one of the sockets it is tesing becomes
> readable
> > > or writable or an exception condition occurs.for this why do i have to
> set
> > > the socket to non-blocking mode. bcoz anyway the process has to block in
> the
> > > select system call.
> > >
> > >  my purpose is to wait on a particular socket for a few seconds for data
> to
> > > arrive and if data does not arrive i want to move on to the next socket.
> i
> > > realised that linux does not support RCVTIMEO socket option. so i wanted
> to
> > > use the timer option in select. so i wrote this piece of code :
> > > for(;;)
> > >    {
> > >       FD_SET(sockfd1,&set);
> > >       FD_SET(sockfd2,&set);
> > >       maxfd1 = max(sockfd1,sockfd2) + 1;
> > >       select(maxfd1,&set,&set,NULL,&time);
> > >       if(FD_ISSET(sockfd1,&set))
> > >        {
> > >          count = recvfrom(sockfd1,(char *)buff1,sizeof(buff1),0,
> > >          (struct sockaddr *)&client,&addrlen);
> > >          printf("\n %s ",buff1);
> > >        }
> > >    if(FD_ISSET(sockfd2,&set))
> > >        {
> > >          count = recvfrom(sockfd2,(char *)buff2,sizeof(buff2),0,
> > >          (struct sockaddr *)&client,&addrlen);
> > >          printf("\n %s ",buff2);
> > >         }
> > > }
> > >
> > > but only after data arrives on sockfd1 does it move onto sockfd2. i
> cannot
> > > understand the reason for this. could you please help me out.
> > >
> > > Regards,
> > > lakshmi
> > >
> > >
> > >
> > > ----- Original Message -----
> > > From: "Ch Rama Krishna Prasad" <rkp@xxxxxxxxxxxxx>
> > > To: <linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>; "Lakshmi Gopinath"
> > > <lakbala@xxxxxxxx>; <linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>
> > > Cc: <ilugc@xxxxxxxxxxxxxxxxxx>
> > > Sent: Saturday, February 10, 2001 9:50 AM
> > > Subject: Re: [LIP] I/O multiplexing among socket descriptors
> > >
> > >
> > > > Hi GopiNath,
> > > >
> > > >    you are trying blocking sockets. Then recvfrom,recv,sen,sendto are
> > > blocking
> > > > system calls. First open the socket in non blocking mode so taht it
> will
> > > not
> > > > block the sockets.
> > > >
> > > > Example like this
> > > > lSockid=socket(AF_INET,SOCK_DGRAM,0);
> > > >       /*
> > > >      * Get the flags of the socket
> > > >      */
> > > >     if((flags = fcntl(lSockid, F_GETFL, 0)) < 0)
> > > >     {
> > > >        perror("CreateSocket():The getting flags is failed");
> > > >        return(-1);
> > > >     }
> > > >
> > > >     /*
> > > >      * Set the flag to Non-block mode
> > > >      */
> > > >     if( fcntl( lSockid, F_SETFL, flags | O_NONBLOCK ) < 0 )
> > > >     {
> > > >        perror("CreateSocket():theSetting Flags Failed");
> > > >        return(-1);
> > > >     }
> > > >
> > > > Now the socket will be non blocking mode and use the getsockopt option
> to
> > > get
> > > > the socket options and bu using the select or poll system call you
> will
> > > proceed.
> > > >
> > > > From
> > > > Ch.R.K.Prasad
> > > >
> > > >
> > > >
> > > >
> > > > On Fri, 09 Feb 2001, Lakshmi Gopinath wrote:
> > > > > Hello Everybody,
> > > > >
> > > > > I need some help regarding socket programming.I need to do multiplex
> > > between
> > > > > multiple sockets. For the same purpose I tried 2 approaches.
> > > > >
> > > > > 1. using select
> > > > > 2. using S_RCVTIMEO socket option.
> > > > >
> > > > > I wrote 2 simple programs - one that uses socket option and the
> other
> > > that
> > > > > uses select call. The program can accept UDP dgrams on 2 UDP ports.i
> > > created
> > > > > 3 simple programs.
> > > > > selrecv.c - uses select to multiplex between the 2 udp ports
> > > > >
> > > > > sockrecv.c - uses socket options to multiplex between the 2 udp
> ports
> > > > >
> > > > > sender.c - sends udp dgrams. change the port no in the program to
> 8000
> > > > > ,create an executable and again change the program to 6000 and
> create
> > > > > another executable.and use them 2 send dgrams to the 2 ports.
> > > > >
> > > > > But both the approaches failed...
> > > > > 1. while using select unless dgram is first got from sockfd1 ( the
> first
> > > > > recvfrom call) it does not go to the next recvfrom call..ie it
> blocks at
> > > > > recvfrom then at select
> > > > >
> > > > > 2. while using socket option. i change the socket options using
> > > setsockopt .
> > > > > but when i verify it with getsockopt it remains unchanged.
> > > > >
> > > > > I am not able to understand the reason. Could someone plsssss help
> me
> > > out
> > > > >
> > > > > Regards,
> > > > > Lakshmi
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > ---------------------------------------------
> > > > > The mailing list archives are available at
> > > > > http://lists.linux-india.org/cgi-bin/wilma/linux-india-programmers
> > > >
> > >
> > >
> > > ---------------------------------------------
> > > Find out more about this and other Linux India
> > > mailing lists at http://lists.linux-india.org/
> >
> 
> ---
> Visit our home page at: www.chennailug.org
> Send e-mail to 'ilugc-request@xxxxxxxxxxxxxxxxxx' with 'unsubscribe' 
> in either the subject or the body to unsubscribe from this list.
> 
>