Hi,

I am polling a NetLib socket like this:


    chhar buf[1];
    while (readit(fd, buf, 1) >= 0) {
        /* do something else or sleep */
    }

The socket is connected to a remote host, which writes some data to the
socket and closes it immidiately. I am expecting my code to read all
the data from the socket and then quit the while loop. I am reading
one byte at a time.

However, after all the data has been read, NetLibSelect doesn't return
any error. It just returns 0 to indicate that no more data is available.
So my loop just hangs.

Then, after about 10 seconds, NetLibSelect finally returns 1 (data is
available). When I read it using NetLibReceive, it returns zero bytes.
The strange part is at this point, err is still zero, not
netErrSocketClosedByRemote as I expected.

The doc says exceptFDs does nothing, so I don't use it. Should I use
it to check for netErrSocketClosedByRemote??

What am I doing wrong here? I am using OS 3.5.

Thanks for your help!

- Ioi

/*=========================================================================
 * FUNCTION:      readit()
 * RETURN:        number of bytes read, or <0 if error, or 0 if the caller
 *                needs to call this function again.
 *=======================================================================*/

int
readit(int fd, char *p, int len)
{
    int res;
    Err err;
    NetFDSetType readFDs, writeFDs, exceptFDs;
    Int16 numFDs;
    UInt16 width;
    Int32 timeout;

    netFDZero(&readFDs);
    netFDZero(&writeFDs);
    netFDZero(&exceptFDs);
    netFDSet(fd, &readFDs);
    width = fd;
    if (width < 1) {
        width = 1;
    }
    numFDs = NetLibSelect(AppNetRefnum, width+1, &readFDs, &writeFDs,
                          &exceptFDs, 0, &err);
    if (numFDs <= 0) {
        /*
         * Socket not ready for reading. The caller should call
         * this function later.
         */
        return 0;
    }
    if (err) {
        return -1;
    }

    if (len == 1) {
        timeout = -1;
    } else {
        timeout = 0;
    }
    res = NetLibReceive(AppNetRefnum, fd, p, len, 0, NULL, NULL, timeout,&err);
    if (res == 0) {
        /*
         * NetLibSelect() told us data is available. If we read 0
         * bytes now, it means remote host has closed connection.
         */
        res = -1;
    }
    return res;
}

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to