On 03-Mar-99 Rene' Witte wrote:
> ...another peculiar problem is that read() from a (tcp) socket returns -1,
> i.e. no data. So I fired up the debugger and traced the calls to read.
> Surprisingly, everything worked fine now (data gets read and send as it
> should be). Again, without debugger --- nope, with debugger --- OK.
>
> Does somebody have an idea why an app would work with gdb, but not without?
That's normal for a non-blocking read. I suspect that the debugger is just
slowing things down such that you never wound up doing an empty read in your
particular case. You can either set the socket to be blocking or use the
NetLibSelect() & netFDIsSet() combo to watch the socket for incoming data.
In this example, popsock is a global NetSocketRef that has a non-zero value
when connected to a peer. NetLibSelect() will drop out if; 1) a GUI event
happens, 2) sysTicksPerSecond has elapsed, 3) incoming data on the rfds
sockets, 4) error (usually peer disconnected) in the rfds sockets (this is
confirmed with a read of 0 bytes).
Err errno, h_errno;
SWord rv, bytes;
NetFDSetType rfds, wfds;
long l1;
netFDZero(&rfds);
if (popsock) netFDSet(popsock, &rfds);
rv = NetLibSelect(AppNetRefnum, netFDSetSize,
&rfds, NULL, NULL, sysTicksPerSecond, &errno);
if (rv > 0) {
if (popsock && netFDIsSet(popsock, &rfds)) {
if ((bytes = NetLibReceive(AppNetRefnum, popsock, ch,
sizeof(ch), 0, 0, 0, AppNetTimeout, &errno)) > 0) {
l1 = poptick(ch, bytes, popsock);
if (l1 < 0) ClosePOP3();
} else ClosePOP3();
}
}
If you just want to do your reads inline, then you can set the blocking
option on the socket. In Unix I would use fcntl():
if(fcntl(s, F_SETFL, O_NONBLOCK)) exit(0);
or
if(fcntl(s, F_SETFL, FNONBIO)) exit(0);
I think one of those should work in PalmOS with the BSD headers. Check
unix_fcntl.h and maybe also look at the FNDELAY option.
PalmOS native API lets you use NetLibSocketOptionSet():
int zero;
zero = 0;
if (NetLibSocketOptionSet(AppNetRefNum, s,
netSocketOptLevelSocket, netSocketOptSockNonBlocking,
(char *)&zero, sizeof(zero), AppNetTimeout, &errno) < 0) {
ErrDisplay(ErrToString(errno));
}
I haven't compiled either of these so I hope there are no typos :)
/* Chris Faherty <[EMAIL PROTECTED]>, finger for PGP */