Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Peon

Newbie Q: What is contained in a packet?

This topic is 5234 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was thinking of learning how to use the Windows socket API in order to make a simple multiplayer RTS. I don''t fully understand sockets yet, but one question I did have is when I send a packet, what is actually contained in it? I was thinking of all the stuff I would need to send to each player... I would need to send unit information obviously (ie: units moving), probably LOS data, etc... but I don''t understand how I would put this into a packet. A packet, as I understood it, was basically sent like a string... so does each packet need to start with a header of sorts, declaring what''s inside of it? I also use a heavily OOP style, is it possible to simply pipe an object through the socket? I''m a little confused as to how all this works together, obviously.

Share this post


Link to post
Share on other sites
Advertisement
Do you mean by that, what is sent in reality (physically over the network) or what your protocol (the interface between network API and your game) should be capable of?



Indeterminatus

--si tacuisses, philosophus mansisses--

Share this post


Link to post
Share on other sites
I (think) what I'm asking is what data should be contained within a packet? So for example, if I have a player and they move a unit, I will probably need to send that data to all the players in the game. How would I put this information (ie: the a unit has moved) into a packet that can be interpreted by the receiver?

To answer your question, I think I am interested in what is physically sent over the network.

[edited by - Peon on April 15, 2004 2:28:20 PM]

Share this post


Link to post
Share on other sites
Well that strongly depends on how your game works. In any case, the less you have to send over network, the better.

What you could do (just as example) is the following:

Reserve the first byte of your packet data for the type of the message (1 for commands to the game for disconnecting, connection requests etc., 2 for unit movements, ...)

This way you could tell more easily what your game has to handle.
Have a handler for each type, interpreting the rest of the packet data.

If your game receives a packet starting with 2, the rest of the packet data is sent to the "unit movement handler", which will know that the next byte is the number of the unit, the following bytes the new position etc.
If your game receives a packet starting with 1, the packet is passed over to the "base handler", which will know that the next byte is the specific action asked (for example 1 for disconnecting etc.)

As for the physical part: well ... that depends on what protocol your socket connection relies (TCP/IP, UDP/IP in most cases). There will be some headers since the network API is organized in layers. If you want to learn more about that, you could take a look at the OSI layer model and learn something about TCP/IP and UDP/IP. There are more protocols in reality, but you won''t need to learn about them, since you most likely won''t use them




Indeterminatus

--si tacuisses, philosophus mansisses--

Share this post


Link to post
Share on other sites
Put all your info into a struct and send it like so:

send( (char*)&MyStruct, sizeof( MyStruct ) );

At least I think that's how I did it.

PS: Oh yeah, don't forget to recv() it by casting it back to your struct type.


Stay Clausal,

Red Sodium

[edited by - red_sodium on April 15, 2004 2:51:44 PM]

[edited by - red_sodium on April 15, 2004 2:53:26 PM]

Share this post


Link to post
Share on other sites
peon,
looking at your name and your focus on multiplayer rts, i guess you have warcraft 3 in mind as an example.

i can give you some ideas on how blizzard implemented the networking in w3:

packet structure:
their packets contain a primer (to identify the packets), like 0x7f (i think thats the one if i remember properly). after the primer you''ll find the packet type and the size (thats important for tcp streams otherwise you don''t know how much data you need to retrieve).
basically they read this header from the socket to see how much data will follow, then buffer that data and process it when completely received.

how the netcode works:
in the case of war3, only the user input is sent to the other clients (this happens with a fixed timestep and a syncronized seed for the RNG). each client basically just gets told when the other users did what action. but you should look into simpler netcodes in the beginning, maybe some irc client/server programming, or some homebrew IM... just get creative and try to fully understand sockets (when using linux you can get an even deeper insight into sockets when reading through the manpages and several interesting open source projects).


in the case you want to "view" some packets, check out ethereal, you can capture some game traffic and try to decipher it (the login will be relatively simple but the ingame is usually encoded or encrypted).


--
Volker Schoenefeld
Student @ RWTH Aachen (Germany)

Share this post


Link to post
Share on other sites
Thanks for the advice. I was thinking about it some more, and figured I could probably get away with sending just messages (similar to what Volkers suggested... I think) If a player built a unit, I could send a UNIT_CREATE message (with some sort of unique packet ID) followed by the ID of what kind of unit (specifying its type), and its position.

From that point on, I could send packets when a player clicks a spot on the map with units selected; this packet would be something like UNIT_MOVE, followed by an ID of that unit (unique to that specific game, a particular instance of it) and its destination coordinate. I wouldn't have to send an entire unit class as a packet, as I originally thought. If I casted a spell or used an ability, I would send a SPELL_CAST packet containing the ID of the spell, its center, and the specific instance ID of the unit that used it (so that I know how much "mana" to subtract. Upon dying, units could broadcast a UNIT_DIED message, with their instance ID, so that all clients could remove it from their list of "alive" objects. Does this seem logical?

Volkers: Yep, I was actually thinking of doing a homebrew IM program first, and maybe adding additional features that might be more relevant to the RTS as I understand network programming better; no need to jump right in to the hard stuff.

As a sidenote, I figure if I save all the packets, I could probably even make replays... true?

[edited by - Peon on April 15, 2004 12:35:13 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Hedos
volkers: So do you mean Warcraft III use TCP? and only TCP?


w3 uses tcp for all traffic except the lan "server scan" broadcasting (obviously).

quote:
Original post by Peon
Thanks for the advice. I was thinking about it some more, and figured I could probably get away with sending just messages (similar to what Volkers suggested... I think) If a player built a unit, I could send a UNIT_CREATE message (with some sort of unique packet ID) followed by the ID of what kind of unit (specifying its type), and its position.



yes, thats it.


quote:
Original post by Peon
As a sidenote, I figure if I save all the packets, I could probably even make replays... true?



yes, thats how it is done. thats basically the way waaaghtv (my warcraft hltv) works. and thats the way the official w3 replays are saved.

you might want to check out http://warcraft.kliegman.com/cgi-bin/cvsweb.cgi/src/replay/w3g_actions.txt
and
http://warcraft.kliegman.com/cgi-bin/cvsweb.cgi/src/replay/w3g_format.txt
its a description of the warcraft3 replay format and the warcraft 3 ingame actions. especially the action will help you understand how it works i think.

--
Volker Schoenefeld
Student @ RWTH Aachen (Germany)

Share this post


Link to post
Share on other sites
volkers: Thanks for the reply. But doesn''t TCP give some lags sometimes ( like a 1-2 seconds lag ) ?
Warcraft III doesn''t seem to have any problem with lag, how do they do that with TCP?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!