[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Nonblocking- IO on sockets



Hello,

I have tried this code to have non blocking io on sockets (this is an echo server):

procMsg()
{
   char buf[64];
   char *cbuf;
   unsigned r;
   struct timeval tv;

   fd_set active_fd_set, read_fd_set;

   if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { /* set nonblockin mode */
      perror("fcntl F_SETFL, O_NONBLOCK");
      exit(1);
   }
   /* Initialize the set of active sockets. */
   FD_ZERO (&active_fd_set); /* */
   FD_SET (fd, &active_fd_set); 

   cbuf=buf; r = 0;
   do {  /* Reading loop */
      read_fd_set = active_fd_set;
      tv.tv_sec = 0;
      tv.tv_usec = 100000;
      r = select(fd+1, &read_fd_set, NULL, NULL, &tv); /* Wait 100ms for activity */
      if (r && FD_ISSET(fd,&read_fd_set)) { /* if socket is readable */
         cbuf++; 
         if ( (r = read(fd,cbuf,1)) <= 0) { /* read 1 byte */
            break;
         }
      } else { 
         break; /* Break if no more bytes */
      };
   } while (1);
   write(fd,buf,r); /* return message to client */
}

If I understand how select works, select should return 0 if client does not send any byte after 100ms (it woks that way in linux)., but rtems select doen't.

In the other hand the line:
fcntl(fd, F_SETFL, O_NONBLOCK) 
should make socket nonblocking, but I am still having a blocking IO!

Some ideas?

Thanks,

Camilo Alejandro Arboleda.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rtems.rtems.org/pipermail/rtems-users/attachments/20010809/1df202cf/attachment.html