Tcpsocket DataAvailable Question

What happens if a DataAvailable event of tcpsocket fires and the data is not read by readall. Will it fire again assuming the socket is connected and no more data will come again.

DataAvailable fires whenever there is data.

Normally each write operation on one side of the socket causes DataAvailable to fire on other side.

My question is the DataAvailable event fired once and i didnt read the data using readall. Will it fire again if there is no more extra data to come. I need to read the data which i didnt previously.

You should always read it all as the buffers used by sockets are not very large and NOT reading it means you could lose data.

Ideally you read all the data into some internal buffer in your program that is maybe shared.
Then you trigger some other process to parse out the data into whatever records or chunks you need instead o trying to do it in the data available routine.
That way the socket’s can read data as fast as it arrives & your methods to figure out “whole messages” won’t slow down the socket receiving data.

While this is true for the kernel’s buffers, the Xojo framework has its own buffers that it reads into. This avoids losing data or ending up with blocking I/O.

So given that the Xojo internal buffer won’t lose the data, will the data available event fire again if the event is ignored and the data isn’t read? My guess is no, but I have no first hand knowledge of how it works to back that up. I’ve always used readall to get the data and then do something with it in another method.

The dataavailable event will continue to fire even if you don’t use readall. I leave the data in the buffer until I find an “end of packet” delimiter e.g. endofline, endofline + endofline or wait for len(me.lookahead) >= contentlength.

It will fire again when new data is detected.

If no further data is available, then i will miss the previous data which i didnt read. Is that what you are saying?

The data previously received but not read will still be in the framework’s buffer. You get it via Read or ReadAll at any point.

sorry to highjack this thread.

But is this the same for Serial ?

what if the data is not enough for a “business package”

a package is 100bytes.but first time “dataavailble” you readall is only 80bytes .so you just give up and wait the other event come?

Yes, you wait for it all to arrive or you get an error, like the other end doing a premature ejac… er… disconnect, or the firewall guy taking a pair of sidecutters to your ethernet cable. :wink: Then process accordingly.

Think of it in terms of if you were downloading a 4GB file - it arrives bit by bit, byte by byte, not all in one dataavailable.