Search Unity

Resolved How to set up a custom server with Netcode and Unity Transport?

Discussion in 'Unity Transport' started by Nit_Ram, Dec 6, 2022.

  1. Nit_Ram

    Nit_Ram

    Joined:
    May 8, 2016
    Posts:
    27
    Hello there,

    I'm currently working on a project for university. Our task is to create a multiplayer game, so I thought I might have a look at the new Unity Netcode. My goal is to have a Unity server instance running on my PC, so that I'm not dependent on any hosting services like AWS, Photon, ...
    I forwarded the port on my router and created a firewall rule to open the port (UDP, right?). This works with the simple UDP example provided here: https://gist.github.com/darkguy2008/413a6fea3a5b4e67e5e0d96f750088a9
    I can send from a different network and it arrives at my PC on which the Unity server is supposed to be running.

    Problem is: I can't connect a Netcode client in Unity. WireShark shows that the connection attempt from the client is incoming on the server PC, but the connection attempt is not succesful and times out after a couple of tries.

    I'm quite confused with the IP and Ports settings. I tried all sorts of different settings.
    How I understand it:
    Address: Only relevant for the client? Put in the public IP of the server here.
    Port: Is the same for client and server.
    Server Listen Address: Only relevant for the server? Put in the local/private IP of the server machine.
    Is this correct? Or does it need to be different on server and client?

    upload_2022-12-6_18-45-5.png

    Do I need to open TCP or UDP ports? Or both? Or something completely different?
    Do you have an idea why the connection isn't working? Help and hints would be greatly appreciated!

    Thank you,
    Martin
     
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    You might be interested in this question on the Unity Transport FAQ.

    To answer some of your questions:
    • Yes, 'Address' is only relevant for the client and should be the public IP of your server. (I'd advise editing your screenshot to hide that IP address, by the way.)
    • 'Server Listen Address' is indeed only relevant for the server. Setting it to the local IP address of the server machine is correct, but I'd actually advise setting it to 0.0.0.0 to make the server listen on all available addresses.
    • Unless you're using WebSocket support in Transport 2.0 (which is still in preview), then yes only the UDP port needs to be opened.
    In the situation you're describing (connection requests showing up on the server but no answers), I've found the two most common causes are either a binding to the wrong address, or a misconfigured firewall. For the wrong binding, using 0.0.0.0 as the listen address should avoid this issue. For the misconfigured firewall, I know you mentioned that you already set up a rule for the port, but I'd recommend disabling it entirely just as a quick test.

    You can also verify if something else might not be already using the port your server is listening on. The
    netstat
    command can be used for this purpose. On Windows, something like
    netstat -a -p UDP
    will show all current bindings to UDP ports.
     
  3. Nit_Ram

    Nit_Ram

    Joined:
    May 8, 2016
    Posts:
    27
    Thank you for your quick response! Don't worry, the public IP on the screenshot is fictional. ;)
    It turns out that disabling the firewall did the trick.
    But the issue is still weird. Even if I let everything through in the firewall settings, the connection fails.

    upload_2022-12-6_20-20-14.png
    This still leads to an unsuccessful connection attempt.

    Do you have an idea what might cause this behaviour? Are there some more things happening in the background of Unity Transport that need special treatment? Are there more firewall settings that need to be taken care of other than opening ports?
     
  4. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Maybe ensure that the rule applies to the correct profile (e.g. Public/Private/Domain)? Also, is the rule you're showing only applying to a specific application? If so, might be worth a try to create a rule that covers all executables.

    Otherwise I'm not sure what could be causing issues. Normally all that's necessary is to open the port that the server listens on. There's no background traffic that uses something else or anything like that.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    You may want to give disabling the router‘s firewall a shot (only for a couple minutes!).
    Also check the router security settings in case it is aggressively filtering. Things like stealth mode or not responding to ping/icmp have frequently caused me headaches trying to run my own game server.

    I‘d first try and get a client connect via local network on two machines before moving on to testing connections over the internet. That way you‘ll have it easier to test and you can rule out any issues with network settings on these PCs as well as your test app.
     
  6. Nit_Ram

    Nit_Ram

    Joined:
    May 8, 2016
    Posts:
    27
    The firewall rule applies to all applications and all domains. When testing it with a console UDP server the traffic comes through. Just not for Unity Transport.

    It's strange... I'll continue trouble shooting (though I'm mostly out of ideas, as the connection can be established for non unity apps) and post an update in case I find a solution.
     
  7. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    It's a stretch, but maybe there's another rule in the firewall that's blocking your specific game server? Say maybe the first time you ran it it popped the "do you want to give access" dialog box and you clicked "no" or dismissed the window and the firewall added a rule blocking the application?

    You may also want to set up the firewall log. Maybe there would be pertinent information to be found in there.
     
    Nit_Ram likes this.
  8. Nit_Ram

    Nit_Ram

    Joined:
    May 8, 2016
    Posts:
    27
    Oh my god, Simon, the hint that I might have clicked the firewall popup away was key.
    I only tested the server in the Unity Editor. Apparently my home network is seen as a public network (no idea why) and when first starting the Unity Editor I clicked Okay on the firewall popup without thinking about it. And the default is to not allow access in public networks...
    In hindsight I really should have made a build, then there would have been a new popup where I could have accepted access on all domains.

    So in case anyone else runs in this issue:
    Just deactive the "forbidden" rule in your firewall settings and you're good to go (Öffentlich == public).
    upload_2022-12-8_10-48-25.png

    Damn, finding that simple issue took way too much time. :D

    But thank you all for your support and your ideas!
     
    Last edited: Dec 8, 2022
    VKyuzel and simon-lemay-unity like this.