select() says there's data, read() says there isn't

select() says there's data, read() says there isn't

Post by John Ca » Thu, 07 Mar 1996 04:00:00





>I have a non-blocking tcp-ip socket that I do a select() on to see if
>there's data. select() says there's data. I do a read() on the socket
>and zero bytes read is returned. I know there should be data to be
>read. And in any case if there wasn't data, read() should return a -1
>with errno=EWOULDBLOCK.

If the connection has been closed by the other end, read will return 0.
This is correct behavior because there will never be any more data
received.

--

 
 
 

select() says there's data, read() says there isn't

Post by Andrew Gier » Thu, 07 Mar 1996 04:00:00


[Vic, here's one for the FAQ (if not already there...)]


>I have a non-blocking tcp-ip socket that I do a select() on to see if
>there's data. select() says there's data. I do a read() on the socket
>and zero bytes read is returned. I know there should be data to be
>read. And in any case if there wasn't data, read() should return a -1
>with errno=EWOULDBLOCK.

>Most of the time this doesn't happen, but under some circumstances it
>does, with the result being a tight loop with select() and read()
>being called and the process taking up nearly all the CPU cycles.

>Does anyone have any idea what is happening?

>(This is on a Sun SparcStation 5 running Solaris 2.5)

>Many thanks.
>--
>-----------------------------------------------------------------------


read() returns 0 for end-of-file (FIN received from peer).

Once the FIN is received, then the socket is always read-ready, and
read() will return 0 every time it is called, hence the CPU usage.

Once read() has returned 0, then either close the socket, or if you
still have data to send, at least stop select()ing it for read.


[.sig under construction]

 
 
 

select() says there's data, read() says there isn't

Post by Vic Metcal » Fri, 08 Mar 1996 04:00:00


: [Vic, here's one for the FAQ (if not already there...)]

: >I have a non-blocking tcp-ip socket that I do a select() on to see if
: >there's data. select() says there's data. I do a read() on the socket
: >and zero bytes read is returned.

The zero from a read() is there under part II question 1.  Actually this
answer is quoted from one of your postings.  I will add a new question
though with the select() thrown into the mix.  Something like...

II.13:  How come select says there is data, but read returns zero?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The data that causes select to return is the EOF because the other side
has closed the connection.  This causes read to return zero.  For more
information see question II.1.

I've made the change to the local copy on brutus here, and will update the
copy on the web soon.

For anyone who is interested, the URL for the unix-socket-faq is:

  http://www.interlog.com/~vic/sock-faq/

I would be very interested in hearing about any errors in it.  I'm not much
of a socket wizard myself.

Take care,
  Vic.

 
 
 

select() says there's data, read() says there isn't

Post by Friedhelm Waitzma » Wed, 13 Mar 1996 04:00:00




>>I have a non-blocking tcp-ip socket that I do a select() on to see if
>>there's data. select() says there's data. I do a read() on the socket
>>and zero bytes read is returned. I know there should be data to be
>>read. And in any case if there wasn't data, read() should return a -1
>>with errno=EWOULDBLOCK.
>Not quite.  A "zero byte" read indicates EOF.  And select() would
>iniindicate at that point that "a blocking read won't block" so it
>would return true.

You may try the following:
Before select()ing, reset O_NDELAY or O_NONBLOCK flag
resp. using fcntl() and set the flag again before read()ing.

However, if the peer side of the connection is closed, the
select() always returns true.

Regards,
Friedhelm.

 
 
 

1. select() says there's data, read() says there isn't


Well, lots of people tell him that this means EOF and the other end
has gone away
BUT
I had a problem with Sun where a select says that there is data, a
read gets a zero return, a second select says there is data again
(perhaps later on!) and a read then gets real data.

So I only counted the other end dead after several consecutive zero
read returns.

2. INVALID COMPRESSED FORMAT (ERR=1)

3. 'mount' say NO iso9660 support and 'cat /proc/filesystems' say YES!!!

4. newbie question

5. 2.1.X kernels say PPP isn't supported, and can't see /dev/ttyS3

6. ?XServer fails to start. XF86OpenConsole Cannot open /dev/console

7. Select/Poll says data pending but subsequent read blocks.

8. xfs dies quietly, how do I find clues?

9. df says 'can't read table of mounted filesystems'

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

11. Says drive isn't formatted?

12. Modem says BUSY when it isn't!!!

13. Please say it isn't so...