TcpSocket: DataAvailable

Hello

Just a short question: Will the DataAvailable event only fire if a write on the other end of the connection is done?
For example, I sent a string from A to B. (A string of several MB’s) Will the dataAvailable event fire if the whole string is passed from A? Or when the first few bytes arrive at B?

If case of the latter, how do I check when to read the buffer with ReadAll? If I don’t know if the sending is finished?

Thanks for the help.

You will get a DataAvailable when some “chunk” of data has arrived. It may be a complete transmission. It may be part of a transmission. It may be multiple transmissions. Or the end of one and the beginning of another. You get the idea.

You need to implement your own protocol so you know what to expect. In your case, you might send the length of data to expect, followed by the data itself. I use a full-on bi-directional protocol that runs something like:

remote: “I’m sending a file”
local: “OK, send me the name”
remote: “The file name is xyz.baz”
local: “OK, send me the length”
remote: “I’m sending nnn bytes”
local: “OK, send the file”
remote: …

This might be overkill for your needs, but it has served me well. Both sides know where they stand at every step of the process.

Thank you for the very clear answer Mr Hare.
The method you suggested will suit my needs perfectly :slight_smile:

Little side question: Is there a size limit on the interal buffer of a TCPSocket?

As I understand things, the TCPSocket buffer is just whatever the host OS provides. The socket code for all 3 platforms Xojo targets just calls into OS libraries for nearly everything.

In short, you probably don’t need to worry about it at all, unless the size of data you are trying to move is freakishly large.

I’ve sent lots of data using TCPSockets, ranging in size from a few bytes to many megabytes. Never had an issue at all with internal buffer size limits.

Thanks Mr Larsen :slight_smile: