*BSD News Article 38089


Return to BSD News archive

Xref: sserve comp.unix.questions:57943 comp.unix.bsd:15343
Newsgroups: comp.unix.questions,comp.unix.bsd
Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.cs.su.oz.au!metro!dmssyd.syd.dms.CSIRO.AU!seurat.syd.dit.csiro.au!not-for-mail
From: ken@syd.dit.CSIRO.AU (Ken Yap)
Subject: Q: how do I detect EOF on a select(2)ed fd?
Message-ID: <3akj9mINN5nb@seurat.syd.dit.csiro.au>
X-Face:  bak'McMAD{%JrA$mQ(j_Ex_o?a/F8/Nt<W\sbPkh?,)PF7TK1{Lh"HJLuQfhE}(Dj!g:c(U  =wh/r[<MJUF}hXzR*URO0e/Lh'mn_YUpU+;ycf6:0>ng*t2KX(NcfGalVs^Ke^C61:F
Sender: ken@syd.dit.csiro.au (Ken Yap)
Nntp-Posting-Host: seurat.syd.dit.csiro.au
Reply-To: ken@syd.dit.csiro.au (Ken Yap)
Organization: CSIRO Division of Information Technology
Date: Sat, 19 Nov 1994 10:16:22 GMT
Lines: 23

In my server, I've got a fd which is a TCP socket. For testing I
connect to it with telnet. I send some data, then close the connection
by quitting telnet. The problem is that I don't see the EOF. My code is
currently like this:

int sock_rbused(int *sock)
{
        long            nbytes;
        fd_set          readfds;
        int             i;

        FD_ZERO(&readfds);
        FD_SET(*sock, &readfds);
        i = select(32, &readfds, 0, 0, 0);
        i = ioctl(*sock, FIONREAD, &nbytes);
        if (i < 0)
                return (i);
        return ((int)nbytes);
}

I thought FIONREAD would return -1 on EOF but it doesn't. I also tried
calling select with exceptionfds but no bit is set on EOF. So how do I
detect EOF without blocking?