Search Unity

LAN Multiplayer - Server discovery with UDP broadcast

Discussion in 'Multiplayer' started by tsx5000, Feb 28, 2014.

  1. tsx5000

    tsx5000

    Joined:
    Jan 15, 2014
    Posts:
    3
    I need to create a server in Unity without using a Master Server as it will be available only on LAN.
    I created a prototype where one user creates the server while others can connect to the server by entering the server's ip and it works fine over the LAN (tested in Windows,Web player and Android). So I needed a way of finding the server's IP which I found can be done using UDP broadcasting.
    I am quite weak in networking so I used this link for reference : http://stackoverflow.com/questions/16110014/unity3d-server-auto-discover

    But unfortunately it doesn't work after adding the UDP modifications. Here is the code :

    Code (csharp):
    1.  
    2. {
    3.     private string connectionIP = "";
    4.     private int connectionPort = 25001;
    5.  
    6.     string errorMsg = "";
    7.     string testMsg = "";
    8.  
    9.     UdpClient udpClient;
    10.     int udpPort = 5100;
    11.     IPAddress multicastAddress = IPAddress.Parse("239.0.0.222");
    12.     IPEndPoint remote_end;
    13.  
    14.     void Start()
    15.     {
    16.         //connectionIP = Network.player.ipAddress;
    17.     }
    18.  
    19.     void startClient()
    20.     {
    21.         remote_end = new IPEndPoint (IPAddress.Broadcast, udpPort);
    22.         udpClient = new UdpClient (remote_end);
    23.         udpClient.JoinMulticastGroup (multicastAddress);
    24.  
    25.         udpClient.BeginReceive (new AsyncCallback (serverLookup), null);
    26.  
    27.         StartCoroutine (clientConnection ());
    28.     }
    29.  
    30.     void startServer()
    31.     {
    32.         Network.InitializeServer (32,connectionPort,true);
    33.         // 32 indiacates max players allowd on the server
    34.        
    35.         startBroadcast ();
    36.     }
    37.    
    38.     void OnGUI()
    39.     {
    40.         if (Network.peerType == NetworkPeerType.Disconnected)
    41.         {
    42.             GUI.Label (new Rect (10, 10, 200, 20), "Status : Disconnected");
    43.             GUI.Label (new Rect (250, 10, 200, 20), errorMsg);
    44.  
    45.             GUI.Label (new Rect (10, 100, 100, 20), "My IP : " + connectionIP);
    46.  
    47. //          GUI.Label (new Rect (10, 100, 100, 20), "Enter IP : ");
    48. //          connectionIP = GUI.TextField(new Rect(130,100,100,20),connectionIP);
    49. //          GUI.Label (new Rect (10, 130, 100, 20), "Enter Port : ");
    50. //          connectionPort = int.Parse(GUI.TextField(new Rect(130,130,100,20),connectionPort.ToString()));
    51.  
    52.             if (GUI.Button (new Rect (10, 50, 100, 20), "Connect"))
    53.             {
    54.                 startClient ();
    55.             }
    56.  
    57.             if (GUI.Button (new Rect (150, 50, 100, 20), "Initialize Server"))
    58.             {
    59.                 startServer ();
    60.             }
    61.  
    62.             if (GUI.Button (new Rect (10, 200,70, 20), "Test"))
    63.             {
    64.                 testMsg = Network.TestConnection ().ToString();
    65.             }
    66.             GUI.Label (new Rect (100, 200, 300, 20),testMsg);
    67.         }
    68.         else if(Network.peerType == NetworkPeerType.Client)
    69.         {
    70.             GUI.Label (new Rect (10, 10, 200, 20), "Status : Connected as Client");
    71.             GUI.Label (new Rect (10, 50, 200, 20), "IP : " + Network.player.ipAddress + " Port : " + Network.player.port.ToString());
    72.            
    73.             if (GUI.Button (new Rect (10, 90, 100, 20), "Disconnect"))
    74.             {
    75.                 Network.Disconnect (200);
    76.             }
    77.         }
    78.  
    79.         else if(Network.peerType == NetworkPeerType.Server)
    80.         {
    81.             GUI.Label (new Rect (10, 10, 200, 20), "Status : Connected as Server");
    82.             GUI.Label (new Rect (10, 50, 200, 20), "IP : " + Network.player.ipAddress + " Port : " + Network.player.port.ToString());
    83.            
    84.             if (GUI.Button (new Rect (10, 90, 100, 20), "Disconnect"))
    85.             {
    86.                 Network.Disconnect (200);
    87.             }
    88.         }
    89.     }
    90.  
    91.     void OnFailedToConnect(NetworkConnectionError e)
    92.     {
    93.         errorMsg = "Error : " + e.ToString ();
    94.     }
    95.  
    96.     void startBroadcast()
    97.     {
    98.         udpClient = new UdpClient ();
    99.         udpClient.JoinMulticastGroup (multicastAddress);
    100.         remote_end = new IPEndPoint (multicastAddress, udpPort);
    101.  
    102.         StartCoroutine (BeginMulticast ());
    103.     }
    104.  
    105.     void serverLookup(IAsyncResult ar)
    106.     {
    107.         byte[] buffer = udpClient.EndReceive (ar,ref remote_end);
    108.         connectionIP = remote_end.Address.ToString ();
    109.  
    110.         Debug.Log ("Server IP : " + connectionIP);
    111.     }
    112.  
    113.     IEnumerator BeginMulticast()
    114.     {
    115.         while(true)
    116.         {
    117.             byte[] buffer = Encoding.ASCII.GetBytes ("Server");
    118.             udpClient.Send (buffer, buffer.Length, remote_end);
    119.  
    120.             Debug.Log ("Sent");
    121.  
    122.             yield return new WaitForSeconds (1);
    123.         }
    124.     }
    125.  
    126.     IEnumerator clientConnection()
    127.     {
    128.         Debug.Log("Waiting for server...");
    129.         yield return new WaitForSeconds (5);
    130.         Debug.Log ("Trying to connect...");
    131.         Network.Connect (connectionIP, connectionPort);
    132.     }
    133.  
    134. }
    What am I doing wrong ?
     
  2. AgusB

    AgusB

    Joined:
    Jun 20, 2013
    Posts:
    65
    I'm trying to do the same. I read somewhere that unity free didn't allow to work with the UDP packets