# How does networking in XNA work?

I am quite confused over how networking works in XNA. I'm currently developing a game for Xbox. I want to create a multiplayer environment and read some tutorials about it.

But I'm confused over how packets are transmitted over the network - E.g.; what is the difference between a LocalNetworkGamer, a NetworkGamer and a LocalGamer? Why do I need to iterate over all LocalGamers when reading packets?

I know that's not a typical SE question, but can anyone point to a tutorial that explains how the networking operates as opposed to how to use it?

You can read this tutorial from msdn website (it's for xna 3.1 but it will work for xna 4):

http://msdn.microsoft.com/en-us/library/bb975645(v=xnagamestudio.31).aspx

Alternatively you can use lidgren for networking, you can find it here :

And a tutorial here :

http://xnacoding.blogspot.com/2010/07/how-to-lidgren-network.html

EDIT:

On xbox you can't use lidgren.

One thing you can do is to look into existing project. Like the one on App Hub :

http://create.msdn.com/en-GB/education/catalog/?devarea=19

• Note that if developing for the xbox (as OP is), you have to use their built in classes, or wrappers for those classes. (i.e. not lidgren). – George Duckett Dec 8 '11 at 11:53
• Yes you're right, i've forgotten it's for xbox, he can't uses lidgren. – Julien Pires Dec 8 '11 at 12:18
• +1 for mentioning Lidgren and remininding me I'll need to start looking into it in a couple months – David Gouveia Dec 8 '11 at 14:57

Using the built-in networking in XNA, all networking centers on the NetworkSession. Once you have an active session (you can check the MSDN link abve or AppHub examples for creating and joining sessions) you have two types of players in the session:

• LocalNetworkGamer - players on your Xbox participating in the session
• NetworkGamer - players attached to other Xboxes participating in the session

The NetworkSession has three properties that let you find out who is who. LocalGamers gives you only players connected to the local Xbox, RemoteGamers gives you all players connected to other Xboxes in the session, and AllGamers gives you both.

Data is sent across the network through the session by calling SendData on a LocalNetworkGamer. There are several overloads of this method - some send data to all players in the session (including the person sending it), others send data only to target gamers. When you call NetworkSession.Update the framework collects all the outgoing SendData requests since the last update and sends the appropriate packets to the correct machines. NetworkSession.Update also receives all earlier sent data and stores it in a buffer per gamer. To actually do something with the data sent over the network you need to pull it out of these buffers; this is done by calling ReceiveData on all the LocalNetworkGamers in your network session. If you don't call ReceiveData you will eventually have issues where the incoming data buffer is full and overflows.