Finding and Joining a Network Session

Describes how to find and join a network session.

The Complete Sample

The code in the topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download NetworkingSample.

The NetworkSessionProperties collection used to create a session with specific properties can also be used to search for sessions with particular properties. Because these values are integers, we will create a set of enumerations in this example to describe them and their placement in NetworkSessionProperties collection.

Note

Setting session properties to use as search parameters is an optional step. You may also pass in an empty NetworkSessionProperties to NetworkSession.Find or NetworkSession.BeginFind to view all available sessions, regardless of property settings.

enum SessionProperty{ GameMode, SkillLevel, ScoreToWin }

enum GameMode{ Practice, Timed, CaptureTheFlag }

enum SkillLevel{ Beginner, Intermediate, Advanced }

Joining a Network Session

To set search parameters

  1. Create a NetworkSessionProperties collection.

  2. Set the values to the desired search parameters.

    NetworkSessionProperties searchProperties = 
        new NetworkSessionProperties();
    searchProperties[(int)SessionProperty.GameMode] = 
        (int)GameMode.Practice;
    searchProperties[(int)SessionProperty.SkillLevel] = 
        (int)SkillLevel.Beginner;
    

To find an available network session

  1. Create an AvailableNetworkSessionCollection to hold the search results.

    AvailableNetworkSessionCollection availableSessions;
    
  2. Use NetworkSession.Find to retrieve a list of available sessions.

  3. Specify the type of session in which you are interested, the number of local players that wish to join, and any session properties that should be matched when searching.

    int maximumLocalPlayers = 1;
    availableSessions = NetworkSession.Find(
        NetworkSessionType.SystemLink, maximumLocalPlayers, 
        searchProperties);
    

    The searchProperties argument is optional. You may pass in null to match any available session, regardless of the session's property settings.

    Note

    This search also may be performed asynchronously using NetworkSession.BeginFind and NetworkSession.EndFind.

    Once you have a list of available sessions, you can examine each session to determine which one to join. In this example, we print some information about an available session for the player to examine.

    int sessionIndex = 0; 
    AvailableNetworkSession availableSession = 
        availableSessions[sessionIndex];
    
    string HostGamerTag = availableSession.HostGamertag;
    int GamersInSession = availableSession.CurrentGamerCount;
    int OpenPrivateGamerSlots = 
        availableSession.OpenPrivateGamerSlots;
    int OpenPublicGamerSlots = 
        availableSession.OpenPublicGamerSlots;
    string sessionInformation = 
        "Session available from gamertag " + HostGamerTag +
        "\n" + GamersInSession + 
        " players already in this session. \n" +
        +OpenPrivateGamerSlots + 
        " open private player slots available. \n" +
        +OpenPublicGamerSlots + " public player slots available.";
    
        spriteBatch.DrawString(spriteFont, sessionInformation, 
            new Vector2(100, y), color);
    

To join an available multiplayer session

  1. Create a NetworkSession.

    NetworkSession session;
    
  2. Join the AvailableNetworkSession using NetworkSession.Join.

    session = NetworkSession.Join(
        availableSessions[selectedSessionIndex]);
    
  3. After joining a NetworkSession, subscribe to any session events that may interest the peer.

    session.GamerJoined += 
        new EventHandler<GamerJoinedEventArgs>(session_GamerJoined);
    session.GamerLeft += 
        new EventHandler<GamerLeftEventArgs>(session_GamerLeft);
    session.GameStarted += 
        new EventHandler<GameStartedEventArgs>(session_GameStarted);
    session.GameEnded += 
        new EventHandler<GameEndedEventArgs>(session_GameEnded);
    session.SessionEnded += 
        new EventHandler<NetworkSessionEndedEventArgs>(
            session_SessionEnded);
    

See Also

Concepts

Network Session Management

Tasks

Creating a Network Session