• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Xanather

Question about TCP

7 posts in this topic

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:

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

but instead just this:

[b]byte[] data_received = NetworkObject.Receive();[/b] //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. Edited by Xanather
0

Share this post


Link to post
Share on other sites
[quote]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.

[quote]do packets always get received in FULL[/quote]

There are no packets. See forum FAQ for details.

[quote]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.

[quote][b]{read byte by byte here until all data is read?[/b][/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.

[quote][b]byte[] data_received = NetworkObject.Receive();[/b] //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.

[quote]ANSII[/quote]

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

Share this post


Link to post
Share on other sites
[quote]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][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][/left][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)][/quote][/background][/left][/size][/font][/color]
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 [i]segments[/i]). 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.

[quote]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][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][/left][/size][/font][/color]
[/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).

[quote]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][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][/left][/size][/font][/color]
[/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.[/size]
1

Share this post


Link to post
Share on other sites
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:

[b]byte[] data_received = NetworkObject.Receive();[/b]

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 [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

(im going to look through the FAQ now) Edited by Xanather
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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 [url="http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/5a8ce5a1-f43a-493a-87ba-9a66ba8a25de"]here[/url].

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.
0

Share this post


Link to post
Share on other sites
[quote]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 [url=https://www.amazon.com/dp/0201633469/ref=as_li_ss_til?tag=enchage-20&linkCode=as4&creativeASIN=0201633469&adid=1DSJS52MMNJQ37B8GT4D]TCP/IP Illustrated, by Stevens[/url] as the classic textbook on how this stack works.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0