Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

LAN with UNet

Discussion in 'UNet' started by iagoccampos, Aug 6, 2015.

  1. iagoccampos

    iagoccampos

    Joined:
    Aug 2, 2014
    Posts:
    10
    Hey guys, I need to know if is possible to make a multiplayer mobile game without internet connection, using only the same wireless (LAN) with UNet.

    I'm trying to do it since the UNet was lauched. The connection works perfectly at the same machine (computer-computer), but not in different machines (computer-mobile, computer1-computer2, mobile-mobile). I don't have too much experience in network area, maybe you will see some illogical code of my part.

    This is my NetManager:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetManager : NetworkManager {
    6.  
    7.     public UnityEngine.UI.Text hostAddress, joinAddress;
    8.  
    9.     public void CreateLocalGame()
    10.     {
    11.         NetManager.singleton.StartHost();
    12.         GameController.Instance.isServer = true;
    13.         networkAddress = hostAddress.text;
    14.     }
    15.  
    16.     public override void OnStartHost ()
    17.     {
    18.         GameController.Instance.mainPanel.MovePanel(1);
    19.     }
    20.  
    21.     public override void OnStopHost ()
    22.     {
    23.         if(!GameController.Instance.isServer)
    24.         {
    25.             GameController.Instance.mainPanel.disconectedText.text = "The host was disconnected";
    26.             GameController.Instance.mainPanel.DisconnectDialogPanel();
    27.         }
    28.     }
    29.  
    30.     public override void OnClientDisconnect (NetworkConnection conn)
    31.     {
    32.         if(GameController.Instance.isServer)
    33.         {
    34.             GameController.Instance.mainPanel.disconectedText.text = "The player was disconnected";
    35.             GameController.Instance.mainPanel.DisconnectDialogPanel();
    36.         }
    37.     }
    38.  
    39.     public void JoinLocalGame()
    40.     {
    41.         GameController.Instance.isServer = false;
    42.         NetworkManager.singleton.StartClient();
    43.         networkAddress = joinAddress.text;
    44.     }
    45.  
    46.     /*public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId)
    47.     {
    48.         Vector3 spawPos = Vector3.right * conn.connectionId;
    49.  
    50.         GameObject player = Instantiate(base.playerPrefab, spawPos, Quaternion.identity) as GameObject;
    51.  
    52.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    53.     }*/
    54. }
    55.  


    There something that I miss? It's hard to a mobile hold a nethost? I will need to work with LLAPI?
    Sorry for my bad english
     
  2. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    86
    I've been testing over my LAN between different pc's and tablets fine, using the local network IP address of the server to connect(eg 192.xxx.xxx.xxx). I did need to forward ports for the server when attempting to connect over the net though. I've not yet tried hosting from an android device.

    My host/connection code is very simple at the moment, to start a server I set the port and then StartHost() ie:
    netManager.networkPort = serverPort;
    netManager.StartHost();

    And then to connect from a client:
    netManager.networkPort = serverPort;
    netManager.networkAddress = serverIP;
    netManager.StartClient();

    Perhaps you're missing setting ports?
     
    Tunity likes this.
  3. iagoccampos

    iagoccampos

    Joined:
    Aug 2, 2014
    Posts:
    10
    Are you using NetworkClient to get the serverPort and Ip? How do you get the server ip and port? If you are using NetworkClient, accord to the documentation "The port/ip of the server that this client is connected to." It's not make sense to me... maybe because I'm newbie with network.
     
  4. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    86
    The server IP is just the IPaddress the machine is using on the local network (you can find that either with ipconfig from a cmd prompt on a windows machine, or perhaps from your router/switch), and the port is explicitly set when starting the server.

    The way I'm set up currently is just a direct connect type of thing, so I have the client input that IP & port via some UI elements, which the connection script can grab and use.
     
  5. iagoccampos

    iagoccampos

    Joined:
    Aug 2, 2014
    Posts:
    10
    I've made some changes in my code

    Code (CSharp):
    1. public class NetManager : NetworkManager {
    2.  
    3.     public UnityEngine.UI.Text joinPort, joinAddress;
    4.     int port = 1978;
    5.  
    6.     public void CreateLocalGame()
    7.     {
    8.         NetworkManager.singleton.networkPort = port;
    9.  
    10.         NetworkManager.singleton.StartHost();
    11.  
    12.         GameController.Instance.isServer = true;
    13.  
    14.         AddressData()
    15.     }
    16.  
    17.     public override void OnStartHost ()
    18.     {
    19.         GameController.Instance.mainPanel.MovePanel(1);
    20.     }
    21.  
    22.     public override void OnStopHost ()
    23.     {
    24.         if(!GameController.Instance.isServer)
    25.         {
    26.             GameController.Instance.mainPanel.disconectedText.text = "The host was disconnected";
    27.             GameController.Instance.mainPanel.DisconnectDialogPanel();
    28.         }
    29.     }
    30.  
    31.     public override void OnClientDisconnect (NetworkConnection conn)
    32.     {
    33.         if(GameController.Instance.isServer)
    34.         {
    35.             GameController.Instance.mainPanel.disconectedText.text = "The player was disconnected";
    36.             GameController.Instance.mainPanel.DisconnectDialogPanel();
    37.         }
    38.     }
    39.  
    40.     public override void OnClientConnect (NetworkConnection conn)
    41.     {
    42.         if(conn.connectionId != -1)
    43.         {
    44.             CancelInvoke("SendData");
    45.             Debug.Log("Stoped send data");
    46.         }
    47.     }
    48.  
    49.     public void JoinLocalGame()
    50.     {
    51.         GameController.Instance.isServer = false;
    52.  
    53.         NetworkManager.singleton.networkAddress = joinAddress.text;
    54.         NetworkManager.singleton.networkPort = int.Parse(joinPort.text);
    55.         NetworkManager.singleton.StartClient();
    56.  
    57.  
    58.         //StartReciver();
    59.     }
    60.  
    61. void AddressData()
    62.     {
    63.         Debug.Log("Network ip: " + Network.natFacilitatorIP);
    64.         Debug.Log("Network port: " + Network.natFacilitatorPort);
    65.  
    66.         Debug.Log("Networkplayer ip: " + Network.player.ipAddress);
    67.         Debug.Log("Networkplayer port: " + Network.player.port);
    68.  
    69.         Debug.Log("Networkplayer external ip: " + Network.player.externalIP);
    70.         Debug.Log("Networkplayer external port: " + Network.player.externalPort);
    71.  
    72.  
    73.         Debug.Log("NetworkManager ip: " + NetworkManager.singleton.networkAddress);
    74.         Debug.Log("NetworkManager port: " + NetworkManager.singleton.networkPort);
    75.         Debug.Log("//////////////");
    76.     }
    77. }
    That is the return of the logs:
    I've made all combinations with ip and port to connect my android with the computer... but no return... I'm getting crazy! What is the localhost ip?? T.T
     
  6. iagoccampos

    iagoccampos

    Joined:
    Aug 2, 2014
    Posts:
    10
    Problem solved :D
     
  7. chetan-rane

    chetan-rane

    Joined:
    Nov 26, 2012
    Posts:
    18
    May i know how you solved your problem.

    i am also trying to connect 1 machine build (pc or mac) running as server and one device build (ios/android) running as client over the network.

    i have tried to connect 1 to 1 using localhost and everything seams ok, by entering server ip i.e Host machine (pc or mac).

    But the issue, if i start two host (one is on mac and one is on window), then how could i show those two machine on clients build (on android or ios device). What is the way to show list of available servers?

    could you please help me?
     
  8. iagoccampos

    iagoccampos

    Joined:
    Aug 2, 2014
    Posts:
    10
    Aracon likes this.
  9. chetan-rane

    chetan-rane

    Joined:
    Nov 26, 2012
    Posts:
    18
    Thanks iagoccampos for your reply and the link.
    let me try this socket broadcasting solution.
     
  10. iagoccampos

    iagoccampos

    Joined:
    Aug 2, 2014
    Posts:
    10
    Np, any doubt ask me here.
     
    waqas_haxhmi likes this.
  11. dimosthenis

    dimosthenis

    Joined:
    Nov 13, 2015
    Posts:
    1
    Hey there @iagoccampos I'm really struggling and I could really use some help. I'm trying to build an co-operative puzzle app such that the users will create a lan and play it whithin. I can't see though how I can make the server see the client and vice versa.

    Should I use the LobbyManager component ? Should I use some script with NetworkManager ?

    Thanks in advance !
     
  12. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Use NetworkBehaviour instead of MonoBehaviour. And start looking at this.
     
  13. waqas_haxhmi

    waqas_haxhmi

    Joined:
    Jun 15, 2016
    Posts:
    15
    Hi !
    have you done with local network Multiplayer Game ???
     
  14. achata

    achata

    Joined:
    Nov 10, 2019
    Posts:
    2
    saludos des futuro :v
    también estoy en esas