Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Network Discovery, How to use without a Network Manager

Discussion in 'Multiplayer' started by LukasO, Sep 23, 2015.

  1. LukasO

    LukasO

    Joined:
    May 23, 2013
    Posts:
    115
    Hi,

    Trying to use network discovery without a network manager and I'm running into a few issues. What do I need to intitialize on the server and client end before I start discovery? I seem to run into errors either with CRC mismatching or "NetworkDiscovery StartAsClient - addHost failed". Code is below:

    Code (CSharp):
    1. public class MyNetworkManager : Singleton<MyNetworkManager>
    2. {
    3.  
    4.     public bool isAtStartup = true;
    5.  
    6.     NetworkClient myClient;
    7.  
    8.     [SerializeField]
    9.     NetworkDiscovery networkDiscovery;
    10.  
    11.     void Update()
    12.     {
    13.         if (isAtStartup)
    14.         {
    15.             if (Input.GetKeyDown(KeyCode.S))
    16.             {
    17.                 SetupServer();
    18.             }
    19.  
    20.             if (Input.GetKeyDown(KeyCode.C))
    21.             {
    22.                 SetupClient();
    23.             }
    24.  
    25.             if (Input.GetKeyDown(KeyCode.B))
    26.             {
    27.                 SetupServer();
    28.                 SetupLocalClient();
    29.             }
    30.         }
    31.     }
    32.  
    33.     void OnGUI()
    34.     {
    35.         if (isAtStartup)
    36.         {
    37.             GUI.Label(new Rect(2, 10, 150, 100), "Press S for server");
    38.             GUI.Label(new Rect(2, 30, 150, 100), "Press B for both");
    39.             GUI.Label(new Rect(2, 50, 150, 100), "Press C for client");
    40.         }
    41.     }
    42.  
    43.     // Create a server and listen on a port
    44.     public void SetupServer()
    45.     {
    46.         networkDiscovery.Initialize();
    47.         networkDiscovery.StartAsServer();
    48.         NetworkServer.Listen(47777);
    49.         isAtStartup = false;
    50.     }
    51.  
    52.     // Create a client and connect to the server port
    53.     public void SetupClient()
    54.     {
    55.         myClient = new NetworkClient();
    56.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    57.         networkDiscovery.Initialize();
    58.         networkDiscovery.StartAsClient();
    59.         //myClient.Connect("127.0.0.1", 4444);
    60.         isAtStartup = false;
    61.     }
    62.  
    63.     // Create a local client and connect to the local server
    64.     public void SetupLocalClient()
    65.     {
    66.         myClient = ClientScene.ConnectLocalServer();
    67.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    68.         isAtStartup = false;
    69.     }
    70.  
    71.     public void Connect(string ip, string port)
    72.     {
    73.         myClient.Connect(ip, int.Parse(port));
    74.     }
    75.  
    76.     // client function
    77.     public void OnConnected(NetworkMessage netMsg)
    78.     {
    79.         Debug.Log("Connected to server");
    80.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    81.         networkDiscovery.StopBroadcast();
    82.     }
    83. }
    Thanks,
     
    Who-am-I likes this.
  2. Who-am-I

    Who-am-I

    Joined:
    Mar 29, 2014
    Posts:
    73
    I am facing same problem.
    I start server(as a client) on PC and I am trying to connect my android device to Server(PC) using by Network Discovery.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class CustomNetworkManager : NetworkManager
    5. {
    6.     public NetworkDiscovery discovery;
    7.  
    8.    
    9.     void OnGUI()
    10.     {
    11.         if (GUI.Button(buttonServerRect, "Start Server"))
    12.         {
    13.                 StartHost();
    14.         }
    15.     }
    16.    
    17.     public void StartHost()
    18.     {
    19.         singleton.networkPort = 7777;
    20.         singleton.StartHost();
    21.     }
    22.    
    23.     public override void OnStartHost()
    24.     {
    25.         discovery.Initialize();
    26.         discovery.StartAsServer();
    27.     }
    28.    
    29.     public override void OnStopClient()
    30.     {
    31.         discovery.StopBroadcast();
    32.     }
    33. }