Page 1 of 1

TCP/Datastream

Posted: Wed Jun 07, 2017 10:14 pm
by GenRabbit
I'm curious on a thing about TCP that I started to think on.
When starting a TCP stream, and your client sends say 20bytes. Is there any chance that you server on a NetworkServerEvent only get, say 4 of them and then gets the next 16 upon the next NetworkServerEvent?

If so is it safe to do like this;

Code: Select all

length = ReceiveNetworkData(Connection, *DataBuffer, 0)
if length >= 20
    ReceiveNetworkData(Connection, *DataBuffer, length) 
endif
if the data is not extracted on the first pass, will it be lost on the second, or will the second just extend the length until it is extracted? (seconds pass all 20 are present?)

Thanks for any answers

Re: TCP/Datastream

Posted: Wed Jun 07, 2017 10:58 pm
by Lunasole
GenRabbit wrote: if the data is not extracted on the first pass, will it be lost on the second, or will the second just extend the length until it is extracted? (seconds pass all 20 are present?)
As I remember from own "client-server" stuff new data is added to a buffer with every new event, nothing is lost until network buffer overflow occurs.

Anyway it doesn't looks nice to call ReceiveNetworkData() only once per event, code like following should be better:

Code: Select all

;[ DATA EVENT RECEIVED ]

Repeat
    bytes_read = ReceiveNetworkData (from_id, @buf(0), #TCP_SIZE)
    If bytes_read <= 0
    	; no more data (or connection error if -1)
    	Break
    Else
    	; process extracted data
    EndIf
ForEver

;[ CONTINUE SERVER LOOP ]

Re: TCP/Datastream

Posted: Wed Jun 07, 2017 11:15 pm
by GenRabbit
Thanks for the information. I was worried getting to little data. :). Yeah your code is nicer. Mine was just an simple to get the idea about what I'm up to.

I presume the TCP buffer max size is 65536 bytes.

Re: TCP/Datastream

Posted: Tue Jun 13, 2017 10:09 am
by dige
I guess its better to wait for the event and using a timeout condition, because of network delays.
If you do not know the exact amount of data that you will get, you can not immediately abort at ReceiveNetworkData () <= 0.

Re: TCP/Datastream

Posted: Wed Jun 14, 2017 1:50 am
by RichAlgeni
Yes! In regards to packets sent, you can a partial packet, the entire packet, or multiple packets!

If your intention is to build an interface between systems, and you can build both ends, it is easiest to send a fixed packet length (IMO) at the beginning of the packet.

For instance, the first 5 bytes may be the length of the data packet. If the packet data is empty, the length will be 0, and could be represented as '00000'.

The benefit of this is that you could write a network receive process to read in the first 5 bytes, then if the length is greater than 0, another network receive process could be called to read in the amount sent. You will either get the entire amount of data sent, or your process will encounter an error.