Skip to content

But it said DataAvailable…

But it said DataAvailable…where is all my data?

I’ve seen this a few times and have made the mistake once or twice myself. You write some code with TCP sockets and rely on the DataAvailable event as if it means “all your data is here”.

But it’s not. So the code you wrote that parses the data into its components keels over because you only have part of what you were expecting to have. And so you ask: “Why isn’t all my data here”?

Well, DataAvailable literally means “some data arrived” – it is not necessarily all of your data. For example, it could be the last 50 bytes of the last thing the server you’re taliking to sent you plus a bunch from the next thing it sent after that. TCP may split your messages into many chunks, as those chunks arrive the hardware buffers them and the OS lets Xojo’s runtime know they arrived and then you get a DataAvailable event.

So what you should do is accumulate the data in an internal buffer as quickly as possible to avoid dropping any data. Then, periodically, maybe in a thread or a timer or even in the data available (if you can do this work really quickly), see if a complete message has arrived and queue that up to be processed by your code.

This way you only deal with full messages instead of partial data. Most well defined protocols have a well defined way to know what a complete message is.

So, if you’re having issues with not getting all you data, look over your code and see if you’ve written it in a way that assumes DataAvailable means “all you’re data has arrived”.