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 are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Testing Netcode through the internet

Discussion in 'Netcode for GameObjects' started by Rodlaiz, Dec 8, 2021.

  1. Rodlaiz

    Rodlaiz

    Joined:
    Jul 30, 2013
    Posts:
    38
    So, I've followed many tutorials on YouTube and also did the the tutos in the Netcode documentation section
    I've managed to create a game where I can see both in the Host and in the Client both players moving.

    Problem's when I try to run the test through a public IP by changing the Connect Address in the U Net Transport of the Network Manager. It doesn't work.



    I've also forwarded the port 8000 in my router, but to be honest, I don't know if that's working properly.
    Is there any simple guide on how to host a game through the internet? Does the Network Manager + U Net Transport components should be enough for this test or I'm missing some extra code?
     
  2. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    290
    Pretty sure you want to set the connection via the `NetworkManager`, and not directly on the `UNetTransport`. The network manager _used_ the transport, and is the higher level component. So use the connect or start method of the NetworkManger to connect to your server from the client...
     
  3. Rodlaiz

    Rodlaiz

    Joined:
    Jul 30, 2013
    Posts:
    38
    The documentation + tutos that I've followed are using the UNetTransport, and in there they configure the IP address + ports
     
  4. mes007

    mes007

    Joined:
    Apr 1, 2019
    Posts:
    4
    Have you figured it out?

    I am also using the Networking-netcode with unity 2020.3.21 and locally everthing works fine, But when opening Host from coputer A, I cant connect as a client from computer B who is connected to same wifi as computer a.
     
  5. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    116
    You need to configure firewall rules on the pc to allow traffic inbound and outbound.
     
  6. mes007

    mes007

    Joined:
    Apr 1, 2019
    Posts:
    4
    The firewall rules are set to public for my unity app on both computers. As well i can see them ping correctly. (they are using the same wifi)
     
    Last edited: Feb 20, 2022
  7. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    116
    Ping is not equals the port you are trying to connect to. It also doesn't matter which profile you have set (public, private, domain etc). You have to make a new port rule and open the necessary port for inbound and outbound connection.
     
  8. ANLevant

    ANLevant

    Joined:
    Jun 10, 2014
    Posts:
    6
    I"m having this very same issue while using a Dedicated Server hosted in an online service (Vultr) firewall is setted up, but clients just can't connect to the server
     
  9. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    As long as your firewall + router have the port open (make sure that's true for UDP traffic) it should work.
     
  10. ANLevant

    ANLevant

    Joined:
    Jun 10, 2014
    Posts:
    6
    Well, it doesnt :)

    I am trying to create a Multiplayer game using NetCode (Old MLAPI).
    I've tried it locally and it works, even with a dedicated server.

    I've created a VM using Vultr online services. I've configured the firewall of my machine to allow port 7777 connections (See evidence1).

    After setting up the code and deploying the server on Vultr (See evidence2 for code), I try to connect with a client locally.



    I've setted up the UNet Transport component to point to my remote server, and then I use the code to start my client and connect to the remote server, but it just won't work (See evidence3)

    I need support with this issue, I need to connect to a remote dedicated server. Thi sis very urgent
     

    Attached Files:

  11. Dezeris85

    Dezeris85

    Joined:
    Dec 16, 2016
    Posts:
    24
    Has anyone figured this out?? I'm now stuck on the very same problem.
     
  12. naishtech

    naishtech

    Joined:
    Sep 21, 2018
    Posts:
    16
    Same problem here.

    I've tried both the Boss Room sample and the Hello World sample using all the same versions/libraries as in the instructions.

    I've added firewall setting rules and port forwarded on my router.

    Both are fine locally but I cannot get any UDP transport (LiteNet / UNetTransport / etc) to connect as a client through the internet. The server starts correctly, but the client gets disconnected after about 10 seconds.

    netbug.png
     
  13. vincent582

    vincent582

    Joined:
    Apr 8, 2020
    Posts:
    1
    Hi everyone, any update about it ??

    I have the same problem, everything work fine localy but when I try to connect over internet it doesnt work.
    I've tried to deploy on dedicated server using microsoft Playfab, and also using UnityRelay but in all case the client object never spawned.

    On Debug I can see I've got the proper IP adress and port from server (or host) and the PlayerObject Spawned, but never happen on client.

    I really dont know if I made mistake and where...
    I also try with or without approval connection and following the boss room exemple.

    It's been weeks I tried to fix it, and today install Netcode for GameObject 1.0.0-pre.7 hoping it will fix it but still doesn't work.

    I am thinking to use Mirror instead of Unity Netcode until all the beta will end...
     
  14. naishtech

    naishtech

    Joined:
    Sep 21, 2018
    Posts:
    16
    I made the same decision and started using Mirror, then I stumbled on this: FirstGearGames/FishNet: FishNet: Networking Evolved. (github.com). Worth taking a look - everything just seems to work, and the discord channel is very helpful. I feel Netcode is very unintuitive in it's current state and I don't like the direction they are taking with their whole relay servers either.
     
  15. Jice128

    Jice128

    Joined:
    May 13, 2019
    Posts:
    37
    I figured. in Unity (or build) my ip address is 192.168.12.121 (my local address). not the "localhost", or "127.0.0.1", and NOT MY EXTERNAL IP.

    so my external ip is = 95.55.**.**.
    in my router rule = incoming UDP on 9998 port goes to PC 192.168.12.121
    on builded .exe or Unity editor I typing 192.168.12.121 (!!!!)
    on external PC I launching builded .exe I typing 95.55.**.**. port 9998 and then I may connect to my "server/host" from inside.
    I wonder why this not in documentation...
     
  16. MaxYari

    MaxYari

    Joined:
    Feb 20, 2018
    Posts:
    13
    TL;DR: set 0.0.0.0 as Server Listen Address in Unity Transport to avoid unnecessary problems.

    I'm not sure if this is stated directly anywhere, but apparently one needs to either specify same "Listening address" on the server side as the one that be connected by the client. As @Jice128 mentioned this listening address should be an IP of this machine inside local network, but from the outside client should be trying to connect via external IP (your local router routes everything from your external to internal IP, that's what port forwarding is for). This approach is extremely uncomfortable if you don't know IP of the machine on which server will run in advance, gladly it's possible to set 0.0.0.0 in server address to bypass this server side check and accept any connections directed at this port no matter the IP, this way connections thorough any local IP will work, as far as it leads to this machine (127.0.0.1, 192.168..., hamachi IP), ofcourse if those connections are made from within local network (or using hamachi). For outside connections port forwarding and firewall opening still should be made to direct traffic from outside to server machine.

    P.S.: Lazy to properly format text or fix typos, sry for that, hope it's readable.
     
    Caphalem likes this.
  17. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    981
    Because I struggled with this myself, here's the step-by-step process when using the Bootstrap sample included in the Netcode package. Netcode for GameObjects works fine over the Internet without Relay, ICE, UDP hole punching through NAT - but it does require certain steps to set it up AND it requires each host to configure port forwarding.

    Using Netcode for GameObjects to host a game on the Internet without Relay:

    Make sure you have the Bootstrap sample installed (in PackageManager select Netcode for GameObjects package):
    upload_2022-9-21_16-30-17.png

    Open the Bootstrap scene and select the BootstrapManager object, then edit the Connection Data, entering your computer's local IP address and 0.0.0.0 for the Server Listen Address, for example:
    upload_2022-9-21_16-31-39.png

    This local IP address is also the one you need to use in port forwarding on the router (I'll get to that further down).

    Make a build.
    Name it "host" or similar. This build can be a "server" or "host" but only on your local machine.

    Get your public IP address.
    Let's assume it is "66.67.98.99" - be sure to use yours not this example IP. Enter it in the same Address field in Connection Data:
    upload_2022-9-21_16-34-17.png

    Note that the public IP typically changes for most users from day to day. So be sure to re-check in case it suddenly stops working days later. This is what dynamic DNS services are used for (more below).

    Make a build.
    Name it "client" or similar. Just DO NOT OVERWRITE the "host" build!
    ZIP up the "client" build and distribute it to someone who can remotely test connections.

    NOTE: This "client" build will be able to connect to an instance of the "host" build that runs on your local machine and nothing else! This "client" build will NOT be able to connect to your "host" instance when you run it on your local network, not even on your local machine! This "client" build will also not be able to host/serve games on any network other than yours. All of this is due to the host's public IP being hardwired in the build.

    Lastly, setup port forwarding on both the router that connects to the Internet AND on any software firewall running on your system (ie Windows Firewall or some "Internet Security" or "Antivirus" software that runs a firewall). Personally, but only while testing, I would recommend to simply TURN OFF any software firewall that runs on your operating system. Those software firewall GUIs are often #§%&! ... terribly confusing and loaded with additional proprietary features and for non-english users, the translations are often flabbergastingly odd.

    On my router, it simply looks like this - see the second entry:
    upload_2022-9-21_16-48-34.png

    Yes, I also host and play DOOM. :)

    192.168.0.78 is my machine's local IP address on the router's local network (LAN).

    Now run the "host" instance locally and choose to either "Host" or "Serve" and instruct your testers to run the "client" instance and to click on the "Client" button. Everyone should click the "Random Teleport" button at least once, otherwise each peer's sphere will occupy the same position.

    If there's exactly one sphere per connected peer (including the host) after everyone randomized their positions, then everything worked fine!

    It's now up to you to implement this in a way that does not require making multiple builds, and allows users to enter IP addresses and hostnames (ie mynetcodegame.com).

    A couple things you'll definitely need:
    Note: Dynamic DNS can often be conveniently configured on the router itself allowing for, with proper port forwarding, multiple machines hosting various games on the local network all using the same dynamic DNS hostname.
     
    Last edited: Sep 21, 2022
  18. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    981
    Click to see improved versions of BootstrapManager.cs and BootstrapPlayer.cs

    Replace these with the files of the same name in the folder: Assets/Samples/Netcode for GameObjects/<version>/Bootstrap/Scripts
    Based on version 1.0.2, tested in Unity 2022.1.

    These scripts add all the functionality you HAVE to have even in the simplest projects. Specifically if you have trouble connecting Netcode apps over the Internet, try my versions. They allow you to enter IP/Hostname and Port at runtime and all the potentially confusing "which address to use where and when?" issues cannot occur. All you need to do is properly configure port forwarding (or disable firewalls). The host/server accepts client connections both locally and over the Internet simultaneously.

    Here's the changelog:
    • added input field for hostname or host IP
    • added input field for port (defaults to 7777)
    • use user-provided IP (or hostname) when starting as client
      • resolve hostname to IPv4 if needed
    • use local IP automatically when starting as server / host
      • server listen address is hardcoded to 0.0.0.0
    • display server IP in "mode" label
      • for host/server will show the local IP
      • for clients will show either the local IP (if client is in same network) or the public IP (if client connects over Internet)
    • "player" objects start in random position
    • local object is displayed in green, remote objects displayed in red
    What's missing?
    • Doesn't get/show the public IP of the host. Use Dynamic DNS or WhatIsMyIP.
    All the helper methods were scavenged from the links I posted above, ie stackoverflow.
     
    mustbemitchell_unity likes this.
unityunity