select() behavior for Blocking and non-blocking sockets.

select() behavior for Blocking and non-blocking sockets.

Post by Vishal Murga » Wed, 05 Mar 1997 04:00:00



Hi,

This is a question on select() usage:

I've a scenerio wherein I expect connect() requests
from multiple clients. My server application starts
up different instances to service the client requests.
I'm using select() call with readfd set to the bind'ed
socket to accept the connections.
I notice a peculiar thing:
If the client sends a connect() to the server, the
connect() call succeeds, but, the server doesn't come
to know of the client connect request.

However, if I change the listening socket on the server
to NON-Blocking, (using ioctl with FIONBIO), the server
does accept connection.

I would like to know the concept behind this. Why doesn't
the server get the connect request incase of blocking socket ?

I sincerely appreciate your comments/suggestions.

Regards,
Vishal Murgai.

------------------------------------------------------------------------

 
 
 

select() behavior for Blocking and non-blocking sockets.

Post by Chad M. Fraleig » Thu, 13 Mar 1997 04:00:00


Quote:> I've a scenerio wherein I expect connect() requests
> from multiple clients. My server application starts
> up different instances to service the client requests.
> I'm using select() call with readfd set to the bind'ed
> socket to accept the connections.
> I notice a peculiar thing:
> If the client sends a connect() to the server, the
> connect() call succeeds, but, the server doesn't come
> to know of the client connect request.

> However, if I change the listening socket on the server
> to NON-Blocking, (using ioctl with FIONBIO), the server
> does accept connection.

        It *should* work the same reguardless of if it's non-blocking.
There's the possibility it's some sort of OS bug.. what OS and version
are you using? Also would it be possible for you to post the relavent
code (including creation/setup).. perhaps you're doing something that
effects it without realizing it.

--
                                   _ _ _ _ _
 \--------------------v-----------> > > > > >-----------v--------------------/

 /--------------------^-----------<_<_<_<_<_<-----------^--------------------\
  <   Tick or Tock.. Tick or Tock..   /|\  http://www.bookcase.com/~chadf/  >
  /----------------------------------'-^-`----------------------------------\

 
 
 

select() behavior for Blocking and non-blocking sockets.

Post by Aryeh M. Friedm » Tue, 18 Mar 1997 04:00:00


What OS are you on?  IOt works perfectly as the following shows on freebsd
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <unistd.h>

#define MAXCONN OPEN_MAX-5
#define SRC_IP "206.83.178.152"
#define SRC_PORT 8000
#define LFD_PORT 9000
#define DATA "\
HTTP/1.0 200 OK\n\
Server: Digital Video Technology PUSH1\n\
Expires: 0\n\
Pragma: no-cache\n\
Content-Type: multipart/x-mixed-replace;boundary=ThisRandomString\n\n"

void bpipe();
int cur_fd;

main()
{
        int i;
        int maxfd;
        int lfd;
        int src;        /* vid src */
        int nfd=-1;
        int slot[MAXCONN];
        char s[500];
        fd_set rset, wset;
        struct timeval tv={0,0};
        struct sockaddr_in sin;

        signal(SIGPIPE,bpipe);

        src=socket(AF_INET,SOCK_STREAM,0);

        sin.sin_family=AF_INET;
        sin.sin_addr.s_addr=inet_addr(SRC_IP);
        sin.sin_port=htons(SRC_PORT);

        connect(src,(struct sockaddr *) &sin,sizeof(sin));

        lfd=socket(AF_INET,SOCK_STREAM,0);

        sin.sin_family=AF_INET;
        sin.sin_addr.s_addr=htonl(INADDR_ANY);
        sin.sin_port=htons(LFD_PORT);

        bind(lfd,(struct sockaddr *) &sin,sizeof(sin));

        listen(lfd,1);

        for(i=0;i<MAXCONN;i++)
                slot[i]=-1;

        while(1) {
                maxfd=0;
                FD_ZERO(&rset);
                FD_ZERO(&wset);
                FD_SET(lfd,&rset);
                FD_SET(src,&rset);

                maxfd=src;
                if(lfd>src)
                        maxfd=lfd;

                for(i=0;i<MAXCONN;i++)
                        if(slot[i]!=-1) {
                                FD_SET(slot[i],&wset);
                                if(slot[i]>maxfd)
                                        maxfd=slot[i];
                        }

                printf("maxfd=%d\n",maxfd);
                select(maxfd+1,&rset,&wset,NULL,&tv);

                if(FD_ISSET(src,&rset)) {
                        readn(src,s,500);
                }

                if(FD_ISSET(lfd,&rset))
                        for(i=0;i<MAXCONN;i++)
                                if(slot[i]==-1) {      
                                        slot[i]=accept(lfd,NULL,NULL);
                                        sprintf(s,"%s",DATA);
                                        write(slot[i],s,strlen(DATA));
                                        printf("accepted %d\n",slot[i]);
                                        break;
                                }

                for(i=0;i<MAXCONN;i++)
                        if(slot[i]!=-1)
                                if(FD_ISSET(slot[i],&wset)) {
                                        printf("reading %d\n",slot[i]);
                                        cur_fd=slot[i];
                                        write(slot[i],s,500);
                                        slot[i]=cur_fd;
                                }
        }

Quote:}

readn(rfd,buf,len)
int rfd;
char *buf;
int len;
{

 int n;
 char *bp;

      bp = buf;
      while(len > 0) {
        n = len;
        if((n=read(rfd,bp,n)) < 0){
           perror("readn: read error");
           break;
        }
        else if(n == 0){
           perror("readn: unexpected EOF");
           break;
        }
        len -= n;
        bp += n;

     }

Quote:}

void bpipe()
{
        printf("closing %d\n",cur_fd);
        close(cur_fd);
        cur_fd=-1;

Quote:}



Quote:>Hi,

>This is a question on select() usage:

>I've a scenerio wherein I expect connect() requests
>from multiple clients. My server application starts
>up different instances to service the client requests.
>I'm using select() call with readfd set to the bind'ed
>socket to accept the connections.
>I notice a peculiar thing:
>If the client sends a connect() to the server, the
>connect() call succeeds, but, the server doesn't come
>to know of the client connect request.

>However, if I change the listening socket on the server
>to NON-Blocking, (using ioctl with FIONBIO), the server
>does accept connection.

>I would like to know the concept behind this. Why doesn't
>the server get the connect request incase of blocking socket ?

>I sincerely appreciate your comments/suggestions.

>Regards,
>Vishal Murgai.

>------------------------------------------------------------------------

 
 
 

1. Non blocking socket blocks; says 'read would block' ?

Hi all,

I have three sockets in server which I am reading in a continuous loop,
so don't want to wait on any one. I have designated them as 'non blocking'
but when run the server exits on the very first read() and says 'would
block'. (Is it the same as 'EWOULDBLOCK' mentioned in R. Steven's book ?)
I don't want that I should get any error msg and exit, in fact the
whole purpose of nonblocking is that if something is available, read it
else proceed in the loop and come back to read next time.
The client (sender) is sending msgs on all three sockets. The sockets
are conn_oriented. I am using SunOS 4.1. The small code part is below:

int emer_s, env_s, sens_s;            /* socket fds */
//----------- making sockets nonblocking ******
if ((res=fcntl(emer_s,F_SETFL,FNDELAY)) < 0)
        {
          perror("fcntl res = -1");
          exit(1);
        }
if ((res=fcntl(env_s,F_SETFL,FNDELAY)) < 0)
        {
          perror("fcntl res = -1");
          exit(1);
        }
if ((res=fcntl(sens_s,F_SETFL,FNDELAY)) < 0)
        {
          perror("fcntl res = -1");
          exit(1);
        }

for(; ;)
   {

      if ((cc=read(emer_s,(char*)&gen_struct,size)) < size)
        {
        perror("read error");
          exit(1);
        }
......... do something
      if ((cc=read(env_s,(char*)&gen_struct,size)) < size)
        {
         perror("read error");
          exit(1);
        }
        ......... do something
      if ((cc=read(sens_s,(char*)&gen_struct,size)) < size)
        {
          perror("read error");
          exit(1);
        }
        ......... do something

   }

Any help is appreciated.

hashmi

--
-----
Atiqullah Hashmi                    
UTA (Univ. of Texas at Arlington)  

2. Hard drive messages

3. Blocking and Non-Blocking socket

4. How do I fix the Boot Manger in FreeBSD?

5. Non-blocking socket reads block! (Bug?)

6. UNIX on Mac?

7. Difference between blocking and non-blocking socket ?

8. Help with SMP on 3.1

9. What is difference of SYNC, ASYNC, BLOCKING, NON-BLOCKING sockets?

10. Non-blocking sockets on SunOS 4 are blocking

11. select() and write() behavior with non-blocking named-pipe

12. How do I set a non-blocking socket back to blocking?

13. Select in non-blocking sockets