Best practices when writing out large buffer using TCPClient?

I’m accumulating sensor data over a period of 5mn, then I upload it in a batch using TCPClient. I noticed I sometimes get a return value of -1 when calling write(). What are the best practices there ? I have a buffer of about 28kbytes, and I write it out in chunks of 1024 bytes. Should I call flush() in between the writes? What is causing the write to fail ? I am thinking of simply restarting the whole write if I get a failure.

1 Like

Should I put some delay() 's in between TCPClient writes? It’s not really in the spirit of TCP though.

Hi @dustpuppy,

I’ve had good luck with the code here for sending bulk TCP https://www.hackster.io/middleca/sending-sound-over-the-internet-f097b4 . I think by virtue of that app I’m only sending 1024 bytes every 100 ms or so.

My guess is that the actual TCP TX/RX buffer on the photon is probably only 1-2k, so it might need a few milliseconds to talk to your router and get the packet handed off. So a short delay between packets or if you get a failed send would probably help with bulk transfers. For the app I linked, I keep a running buffer, and then chunk it off into a transmit buffer. I’m working on another that uses a circular buffer (easier to read / use).

While I was playing with finding max TX/RX speed, I was able to get my photon to send 100KB/s. So if you’re sending 1k packets, that would be sending 100 packets a second, so maybe a delay(10); ?

I hope that helps!

Thanks,
David

@Dave , just tried introducing a delay(30); in between writes of 1024 bytes and it seems to work better. If I get a failed send, should I retry that send after a delay, or is the stream messed up at this point ? I’m transferring 28k bytes every 5mn, so i’m not too worried about speed, I want to priviledge reliability.

Hi @dustpuppy,

Hmm, write returns -1 if the status check fails, which calls:

return (isOpen(_sock) && Network.from(nif).ready() && (SOCKET_STATUS_ACTIVE == socket_active_status(_sock)));

So either the socket isn’t open, or the network interface isn’t ready, or the socket isn’t active?

I hope that helps!

Thanks,
David

Hi,

I am using the TCPClient on Photon to connect to a web server (which is not in my power to change) and which sends me back quite large XML files (over 10KB) split in ~1.5KB chunks.

What should I expect to happen if I am unable to free the TCP buffer before it gets full? I am not familiar with low level TCP-IP implementations and I would like to know if the other party (the web server in this case) will wait until the Photon will be ready to receive more data or will the other party just disconnect.

Thank you!

Heya @pmijoiu,

The interstitial routers should buffer and retry the TCP packets if you can’t receive and acknowledge them, but that won’t last forever. You may get the first few quickly, and then the next few will be “slow”, since that’s the router replaying them to your device. Best to read and process quickly though!

Thanks,
David

Thank you!