TCP/Datastream

Everything else that doesn't fall into one of the other PB categories.
GenRabbit
Enthusiast
Enthusiast
Posts: 118
Joined: Wed Dec 31, 2014 5:41 pm

TCP/Datastream

Post 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
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: TCP/Datastream

Post 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 ]
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
GenRabbit
Enthusiast
Enthusiast
Posts: 118
Joined: Wed Dec 31, 2014 5:41 pm

Re: TCP/Datastream

Post 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.
dige
Addict
Addict
Posts: 1247
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: TCP/Datastream

Post 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.
"Daddy, I'll run faster, then it is not so far..."
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: TCP/Datastream

Post 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.
Post Reply