Multiplayer Platforming with Mango flavored TCP and UDP
- Posted: Jul 22, 2011 at 6:00 AM
- 4,989 Views
- 5 Comments
Today's project shows off how you can take advantage of the new TCP/UDP socket supporting coming in the Windows Phone Mango release. You'd like taking an existing game and plugging in multiplayer support might be hard, or that socket support is a little much for you, but this post shows that it isn't and there's no need to fear...
Windows Phone Peer-to-Peer Multiplayer Game using Sockets in XNA
One of the new features for the app platform in Windows Phone Mango is TCP and UDP Sockets. In this blog post, I'll talk about using this to augment an existing game to add peer-to-peer multiplayer over WiFi using UdpAnySourceMulticastClient. Phones running the game on the same WiFi network automatically discover one another and the players just appear on the screen.
The game itself is from another sample, the Windows Phone 7 Platformer Starter Kit from David Rousset's blog.
The changes are to add code to run UDP Multicast sockets and to enable multiplayer.
The source code for the full project is attached in the zip file below.
PlatformerGame.cs: This contains the game code and is where the sockets are initialized, and where the sends and receives are handled.
UdpAnySourceMulticastChannel.cs: This contains the UDP multicast sockets code for joining the group, sending and receiving data.
OtherPlayer.cs: This is a modification of Player.cs to add other players to the game.
...
Hope this helps show a simple way to communicate from phone to phone and demonstrate the power of sockets. Happy developing!
Here's a couple code snips to wet your appetite.
public void Open()
{
if (!IsJoined)
{
this.Client.BeginJoinGroup(
result =>
{
try
{
this.Client.EndJoinGroup(result);
IsJoined = true;
this.OnAfterOpen();
this.Receive();
}
catch
{ }
}, null);
}
}
private void Receive()
{
if (IsJoined)
{
Array.Clear(this.ReceiveBuffer, 0, this.ReceiveBuffer.Length);
this.Client.BeginReceiveFromGroup(this.ReceiveBuffer, 0, this.ReceiveBuffer.Length,
result =>
{
if (!IsDisposed)
{
IPEndPoint source;
try
{
this.Client.EndReceiveFromGroup(result, out source);
this.OnReceive(source, this.ReceiveBuffer);
this.Receive();
}
catch
{
IsJoined = false;
this.Open();
}
}
}, null);
}
}
void Channel_PacketReceived(object sender, UdpPacketReceivedEventArgs e)
{
string data = e.Message;
Console.WriteLine(data);
string[] pos = data.Split(',');
//Discard packets that do not match
if (pos.Length != 5)
{
return;
}
try
{
if (pos[0] != identifier.ToString()) //if not originated from this phone
{
if (pos[1].Contains("ReachedExit"))
{
level.TimeRemaining = TimeSpan.Zero;
}
else
{
Vector2 position = new Vector2(float.Parse(pos[1]), float.Parse(pos[2]));
Vector2 velocity = new Vector2(float.Parse(pos[3]), float.Parse(pos[4]));
level.UpdateOtherPlayer(int.Parse(pos[0]), position, velocity);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Caught unexpected exception: " + ex.Message);
}
}
Adding UDP/TCP/Socket support to your app or game doesn't have to be hard or scary, it's just a Send and Receive away...
Here’s a few more links you might find interesting:
- Mixing the Game State Management and Platformer XNA Recipes
- Juicing up your Mango development with two Mango Development Learning Resources
- Download the Web Installer to get Mango Dev Tools Beta
- ISO Image of the Mango Dev Tools
- Code Samples for Windows Phone
- New APIs in Silverlight for Windows Phone OS 7.1
- App Hub
Comments Closed
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
The code above contains "catch { }" so everybody should disregard this article
@Matt Zinkevicius:Why? What's the problem with try catch statements?
@Tanjoodo: I think he means it's good practice to be a bit more specific about the exceptions you want to catch and let other exceptions throw, as the code is right now, a unexpected exception would just be swollowed in the catch.
is ha look
@Matt Zinkevicius: The world is not perfect. Everybody should leave this planet.
Remove this comment
Remove this thread
close