Question about TCP

Started by
6 comments, last by Xanather 11 years, 10 months ago
Before I start im going to say I am talking about the System.Net classes in the .net framework.

So ive been reading a game development book lately on C#/XNA. As I learn more always think "how do people do this, and how do people do that". This is one of them moments. I am aware of very basic networking concepts/stratergies.

Im going to get straight to it: What happens when you send a tcp message (packet) larger than it should be. Does the Send() code throw an exception saying the packet is too big, if so what is the actual maximum packet size? Even though the defined maximum tcp/udp packet size is 64kb, ive heard from almost everywhere this is not a safe size as other technologies limit the size.

Say if the packet IS to big and a exception is thrown but there is still more data needs to be sent? Do you split up the transmission into certain x bytes per packet and then would you create a start/end ANSII (or unicode whatever) character at the begin/finish of a data transmission? The start character will be a ANSII in the start of the first packet and the end ANSII character will be in the last packet. Once the end ANSII character has been recieved the receiver then knows all data has been read and then processes the data? Is this a good stratergy?

While im asking this, do packets always get received in FULL (well definately for TCP) right? Not incremental (e.g. 10 bytes each milisecond for 100 miliseconds for the whole packet, im just using this as an example), if this is true im guessing there is no need to create this:

while(NetworkObject.IsDataAvailable)
{
read byte by byte here until all data is read?
}


but instead just this:

byte[] data_received = NetworkObject.Receive(); //Blocking call; this is assuming packets are received full and there is no need to read each byte.

Thanks,
Xanather

All answers/helpful replies are greatly appriciated.
Advertisement
What happens when you send a tcp message (packet) larger than it should be.[/quote]

TCP is a stream, there is no "too big". It's a pipe, you keep pushing things on one end and they come out in same order and intact in other.

do packets always get received in FULL[/quote]

There are no packets. See forum FAQ for details.

Even though the defined maximum tcp/udp packet size is 64kb,[/quote]

UDP has maximum datagram size. TCP does not.

There are however send/receive buffers, which are something else.

{read byte by byte here until all data is read?[/quote]

That is how TCP stream is read, you just don't read bytes individually, but you ask to receive all that are available.

It is up to you to make sense of these bytes, such as where one message begins and ends, how long to read, etc... It's just a stream, like reading from file.

byte[] data_received = NetworkObject.Receive(); //Blocking call; this is assuming packets are received full and there is no need to read each byte.[/quote]

This will return data that has arrived so far. Again, it is up to you to determine if all that was sent has arrived, or if you need to call receive again.

ANSII[/quote]

Minor nitpick - it's either ASCII (American Standard Code for Information Interchange) or ANSI (American National Standards Institute).

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Im going to get straight to it: What happens when you send a tcp message (packet) larger than it should be. Does the Send() code throw an exception saying the packet is too big, if so what is the actual maximum packet size? Even though the defined maximum tcp/udp packet size is 64kb, ive heard from almost everywhere this is not a safe size as other technologies limit the size.[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)][/quote][/background]

[/font]
TCP does not send packets (conceptually). TCP is a stream oriented protocol. At the lower layer, it does create distinct units of data (known as segments). These segments are generally delivered by wrapping them in IP packets.

There are two important buffers to be aware of with TCP - the send buffer and the receive buffer. If the send buffer is almost full, then most networking APIs* will enqueqe as much as possible and return the number of bytes it added. It is up to you to remember to send the remaining bytes. If the receive buffer fills, then TCP will allow you to buffer some data in the send buffer. You are allowed to modify the default sizes of these buffers to suit your application's requirements.


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Say if the packet IS to big and ... there is still more data needs to be sent? Do you split up the transmission into certain x bytes per packet and then would you create a start/end ANSII (or unicode whatever) character at the begin/finish of a data transmission? The start character will be a ANSII in the start of the first packet and the end ANSII character will be in the last packet. Once the end ANSII character has been recieved the receiver then knows all data has been read and then processes the data? Is this a good stratergy?[/background]

[/font]
[/quote]
There are a number of ways of layering a packet oriented protocol on top of TCP. A simple way is to use some arbitrary byte values as delimiters, like you described. The problem is that if your protocol is non-text based, you could end up with arbitrary bytes in the "content", and your protocol must have a way to deal with this. Even if it is text based, your code may need to be robust and should deal with such unexpected characters.

A common way that doesn't suffer from this is to prefix a "packet" with the length of the packet. For example, you might say that your protocol alternates between 3 byte, big endian "packet length" headers followed by a packet of that number of bytes long. Note that my choice of 3 bytes was arbitrary, you could have any number of bytes, or even a variable number of bytes (e.g. a stream of 7 bit values with the high bit indicating whether to treat the next byte as data or a continuation of the length field).


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]While im asking this, do packets always get received in FULL (well definately for TCP) right? Not incremental (e.g. 10 bytes each milisecond for 100 miliseconds for the whole packet, im just using this as an example)[/background]

[/font]
[/quote]
All of the data will eventually get there (or you'll get notification that the connection is broken). However, it is not guaranteed to arrive in the same way as it was sent - that is, if you send 100 bytes, then 50 bytes, then 100 bytes, TCP might deliver you 250 bytes in one go, it might deliver 2 chunks of 125 bytes, or 250 one-byte chunks, or any combination between. TCP tries to make efficient use of the network by default, so that last one is unlikely to happen (see "Nagel's Algorithm - for time sensitive applications you may need to disable it).

* [size="2"]Caveat: I haven't done any raw socket programming in .Net so I cannot comment directly on how its socket API works.

Okay, I understand now. Thanks for the replies.

But what if you do use a message delimiter and the delimiter is a unicode character? Would you use:

byte[] data_received = NetworkObject.Receive();

in order to get all current available bytes and then convert all the received bytes to unicode characters. After that check the new unicode string received to see if it has the delimiter character in it? If so split the received unicode string (between the unicode delimiter character), any data within the string that comes after the unicode character should be processed with any previous strings received (processed together) and the data that comes before the delimiter should be passed into some sort of string buffer to later be pulled out and processed upon once another delimiter has been reached? (i.e data just keeps getting added to the string buffer until a delimiter is reached).

I should really just buy a book on this stuff... lol.

All answers are appriciated.

Thanks,
Xanather biggrin.png

(im going to look through the FAQ now)
The NetworkObject in the XNA framework is a wrapper on top of Live! networking, which generally uses UDP rather than TCP. It has very different guarantees of packet delivery, completeness, ordering, etc, than you would see from a TCP stream. If you want to know how the XNA Framework does networking, you should read its documentation.
enum Bool { True, False, FileNotFound };
Oh no, sorry, I was just using NetworkObject as an example, I should have stated. NetworkObject can be for example, a Socket? or .net's NetworkStream?

Ive just read most of documentation about .net's 4.0 TcpClient/TcpListener/NetworkStream/Sockets classes including how the methods and properties work. Sadly though they don't really go in depth about how the protocol itself works. I'm starting to get an really good idea though, ive asked a few question about the protocol logic on the msdn forums here.

I appriciate the time you people have spent replying to my questions, hplus0603, rip-off and Antheus but may I ask 1 more favour from anyone; that someone read through the question I have posted/asked on the msdn forums and reply with any errors I have made in that post here if thats possible?

Thanks,
Xanather.
they don't really go in depth about how the protocol itself works[/quote]

If you want to know how TCP works in detail, I recommend TCP/IP Illustrated, by Stevens as the classic textbook on how this stack works.
enum Bool { True, False, FileNotFound };
Ok thanks.

This topic is closed to new replies.

Advertisement