🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Custom packets in TCP/IP and UDP?....

Started by
3 comments, last by SomeoneElse 23 years, 10 months ago
Greetings and thanks for reading this post!! I am aware that TCP/IP packets can be be customized to fit a game''s requirements, but what about UDP? Are there any resources out there which can show me how this is done? Also, this may more revolve around game engine design, but how does one go about setting up a game engine to handle putting data into packets for transmission over a network? Any insight on this would be appreciated...
Advertisement
SomeoneElse,

The IP (connection-oriented) protocol doesn''t really allow for packets, per se. Logically, it transfers a stream of bytes from one point to another. Technically, yes, at the very bottom of the communications it is all packet-driven, but neither application side see''s this happening; it''s transparent. What you can do is create "objects" that you send back and forth-- packets of information. Basically build your own protocol on top of the IP connection. It''s much like reading & writing to a file. Whatever you dump to the file has to be readable and parseable by a client.

I wrote one such protocol (over IP) for a game. I called it "COP" which stands for "Common Object Protocol". At the very top level of abstraction, communications between two machines is a series of "objects" being transmitted.

Both the client and server send "object packets" back and forth to talk to each-other. I would then define each object type, size and information (the size can be dynamic.)

So all communication would look like this:
[DWORD-OBJECT-SIZE] [DWORD-OBJECT-ID] [... object ...]

Where:
[DWORD-OBJECT-SIZE] is a DWORD (4-byte) size. It specifies the number of bytes that follow, which is a single object.
[DWORD-OBJECT-ID] is a COP object ID. It''s what gives meaning to the data. If the client recognizes the object ID then it can parse/decode the information; if not, it can safely "skip" or ignore the object entirely.
[... object ...] is a series of bytes with length [DWORD-OBJECT-SIZE].

In this sense, you can think of each object as a ''packet'' of information. You could build something similar using UDP, but UDP packets are limited in size. Typically only about 1.5k in size before they are broken up or simply dropped by routers. There is a Winsock 2 function call, somewhere in the ioctlsocket() function that will tell you what the max packet size is.

UDP is certainly the faster method of the two, when as-close-to-real-time transmission is required/needed. In this case you would simply have a small buffer that you sendto() and recvfrom(). Then, like the IP method above, you would have some sort of ID number as the first 1, 2 or 4 bytes that tells the reciever what information is inside the packet.


Hope this helps,
// CHRIS
// CHRIS [win32mfc]
struct GamePacket
{
unsigned long COMMAND;
char RECEIVED_FROM_PLAYER[32];
int XPOS;
int YPOS;
...so on
}

then cast it to (char*) when you send it over

-BacksideSnap-
Actually you got it backwords win32mfc. IP is a fundamentally a connectionless packet based protocol. UDP is a very thin layer on top of IP that mostly provides ports so that you can do multiplexing of data to/from an address (i.e. have more than one app using sockets per machine). TCP is a streaming connection oriented protocol which is also built on top of IP and provides robustness, etc.
IP is in fact conectionless and is soley responsible for addressing and routing of datagrams.

TCP is connection-oriented, meaning is uses sequence numbers to ensure packets are assembled in the correct order and requires acknowledgements to ensure packet delivery.

UDP on the otherhand is connectionless and its delivery system is best effort, meaning that the transmitting host doesn't care if the receiver gets a datagram or not.

You can use either with different tradeoffs in performance. For TCP you need to keep in mind the overhead incurred in TCP session establishment and session maintanence. Your host system only has a finite amount of system rescources to allocate to TCP requests. (On a side note, this is the premise behind TCP SYN Flood Denial of Service attacks from Crackerz)

UDP is probably your best bet as far as game performance. Again it's best effort delivery, so you'll need to implement some sort of check client side to re-request data in the event the host doesn't get back to you in your specified time parameters.

The struct idea is probably your best approach, but Chris's object passing idea would probably work as well. I'm thinking of trying that with something I'm working on.

On another side note, Chris is considering writing a book on Winsock programming which will probably address issues such as these. Chris write that book!

// Matt...

Just thought of this as after I posted ...

If you're building a game for the Internet, you may want to consider encrypting the data in your struct and adding a field containing a hash for integrity checking. This adds a little more overhead, but can help keep your game logic a little more secure and thwart attempts to cheat. If it's a popular game, it won't stay secret forever, because there are many clever individuals out there.


Edited by - t_sql_rex on August 26, 2000 4:08:03 PM

This topic is closed to new replies.

Advertisement