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

Re: I/O multiplexing among socket descriptors



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
>