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

Unity Network Manager overwrites network address at runtime

Discussion in 'Multiplayer' started by Davedub, Aug 31, 2018.

  1. Davedub

    Davedub

    Joined:
    Mar 30, 2016
    Posts:
    22
    Hi All,

    So I recently started work on a project I completed a year or two ago. The networking stuff was working fine before, but now I simply cannot connect to my host over a network.

    So I ran through the tutorials again and found that Unity is overwriting the network address I specify with 'localhost' at runtime. Which means I can never connect an Android device over my local network as I did before.

    I made a short YouTube movie to demonstrate the problem:


    Any ideas anyone?

    - davedub
     
  2. storybelldev

    storybelldev

    Joined:
    Dec 12, 2017
    Posts:
    6
    @Davedub , You are right. Unity is overwrite the network address as you start host.
    see source of the network manager.

    but, you still connect your android device using local network discovery.
    1) Start Host and start local network discovery as host.
    2) On Client, start local network discovery as client, this will give you a ip address of host via callback called OnServerDetected(string address, string data).
    3) Now, you have the ip address of the host. so set the network address of the client as the ip address you received.
    your both device are now connected, no matter which device or platform you used.
     
  3. Davedub

    Davedub

    Joined:
    Mar 30, 2016
    Posts:
    22
    @storybelldev

    The method you describe is very similar to how my original project worked - I used network discovery to find the Host's IP and then connected using it. The only difference is that I used OnReceivedBroadcast to obtain the Host's IP address, but it used to work seamlessly. In fact, if I run the original project now, the Network discovery part still works fine and obtains the Host's IP. The problem occurs when I try to connect to the Host.

    So for the purposes of finding out what's going wrong, I have made a hugely cut down version with hard coded network addresses.

    You can see from the movie I posted that the Android device is trying to connect to the correct IP address. In this simple, cut down example, all the Android client tries to do is the following:

    Code (csharp):
    1.  
    2.                 NetworkManager.singleton.networkAddress = "192.168.1.18";
    3.                 NetworkManager.singleton.networkPort = 7777;
    4.                 NetworkManager.singleton.StartClient();
    5.  
    This should work right? Yet the code fails to connect, as is seen towards the end of the movie. I am of course certain that the IP address points to my laptop running my Host. I have tried over two wifi networks and using two different Android devices. Note that one of the wifi networks is using the exact same Android hotspot that my project used to work on. Moreover, one of the Android devices I have tested with is the exact same one that the project used to work on. Something has changed and as far as I can see, it's not my code or network setup!

    I looked at the code you linked to and can see that (for whatever reason), the Network Manager changes the Network Address to "localhost" at runtime. So perhaps this is not the cause of the difficulty I'm having.

    But my problem remains - why on earth can't I connect with such a simple example using verified, hard coded IP addresses?
     
  4. storybelldev

    storybelldev

    Joined:
    Dec 12, 2017
    Posts:
    6
    @Davedub

    try two thing if it helps you.
    1) Local Network callback, OnReceivedBroadcast(string fromAddress, string data) has the ip address of host. but not same as your hard coded ip.
    so, you have to split the fromAddress received from the host as

    string hostip = fromAddress.Split(':')[3];

    This will give you a host ip. Use this hostip inplace of your hardcoded ip and see if this works.

    2) Make sure your port 7777 is not already occupied by other application. if so, try to change the port. But,make sure host and client have same PORT.
     
    Last edited: Aug 31, 2018
  5. Davedub

    Davedub

    Joined:
    Mar 30, 2016
    Posts:
    22
    Thank you for the reply. I'm in Asia, so I'm at the end of my day now, but will try the steps you suggest at the next opportunity.
     
  6. Davedub

    Davedub

    Joined:
    Mar 30, 2016
    Posts:
    22
    [SOLVED]

    Good call on the port number, I had a nodejs project running int he background on port 7777.

    Thank you for your help!
     
    storybelldev likes this.