Non-blocking socket reads block! (Bug?)

Non-blocking socket reads block! (Bug?)

Post by Armita » Thu, 22 Jun 1995 04:00:00



Boy, I hope this hasn't been addressed already...  

I have an application which builds and runs correctly under a wide variety
of Unicies (ULTRIX, AIX, SUN, NeXT, etc.)  It is a daemon which listens
on a port, and does things when a client connects to it (TCP socket streams).

The main body of the daemon is a tight loop which does read(2) on a socket
descriptor returned earlier by an accept(2).  As you might suspect, the
socket ought to be non-blocking, so that work can continue if there is no
data waiting to be processed, and so it is.  After the call to socket(2),
I do a fcntl(2) on the descriptor setting the O_NDELAY flag (that's
O_NONBLOCK to you POSIX types...)  This correctly makes the socket and
subsequent calls to read(2) non-blocking on all other platforms.  But
the read() blocks under Linux (1.2.9 kernel).

I was able to fix the problem by also doing a fcntl() on the socket descriptor
returned by accept() and setting the O_NDELAY flag there too (it has to be
set on _both_; just setting it on the one returned by accept makes the
read() block).  Weird.  I read the man pages again, and I saw nothing to
indicate that it didn't have to be done that way...  but, as I said, this
code worked fine on tons of other platforms.

So, am I on *?  Is this a known bug?  An unknown bug?  Any help would
be appreciated (though, as I said, the code now works -- I'm just curious
as to what the deal is...)

Thanks!

--
 ============================================================================
|    Peter (Z-Man) Zatloukal   |  That is not dead, which may forever lie,   |

 ============================================================================

 
 
 

Non-blocking socket reads block! (Bug?)

Post by Alan C » Thu, 22 Jun 1995 04:00:00



>read() block).  Weird.  I read the man pages again, and I saw nothing to
>indicate that it didn't have to be done that way...  but, as I said, this
>code worked fine on tons of other platforms.

Its another fine example of what happens when the standard API doesnt have
a real definition. Roll on POSIX 1003.something BSD socket API

Alan
--
  ..-----------,,----------------------------,,----------------------------,,

 ``----------'`----------------------------'`----------------------------''
Redistribution of this message via the Microsoft Network is prohibited

 
 
 

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

3. Blocking and Non-Blocking socket

4. minimum shell for ssh/procmail

5. Difference between blocking and non-blocking socket ?

6. bad library file

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

8. Where do I find my .Profile or .Login file?

9. Non-blocking sockets on SunOS 4 are blocking

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

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

12. Non-Block read & Block read

13. non-blocking reads on a socket