Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Multiplayer Platforming with Mango flavored TCP and UDP

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);
    }
}

windowsphoneplatformer

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:

Follow the Discussion

Remove this comment

Remove this thread

close

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.