Search Unity

Can't connect to hosted server (Not LAN)

Discussion in 'Multiplayer' started by slimabob, Jun 25, 2016.

  1. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    A few months ago, I created a project following a unet tutorial to set up a custom Network Manager. It worked great, I was able to host a server and my friend was able to connect to it. Now, I've made a project doing the exact same things, and we can't get it to connect. Here is what I have for my custom network manager:


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class NetworkManager_Custom_V2 : NetworkManager {
    7.  
    8.     public void StartupHost()
    9.     {
    10.         SetPort();
    11.         Debug.Log(Network.player.ipAddress);
    12.         Debug.Log(Network.player.externalIP);
    13.         Debug.Log(Network.player.port);
    14.         Debug.Log(Network.player.externalPort);
    15.         NetworkManager.singleton.StartHost();
    16.     }
    17.  
    18.     public void JoinGame()
    19.     {
    20.         SetIPAddress();
    21.         SetPort();
    22.         NetworkManager.singleton.StartClient();
    23.     }
    24.  
    25.     public void SetIPAddress()
    26.     {
    27.         string ipAddress = GameObject.Find("InputFieldIPAddress").transform.FindChild("Text").GetComponent<Text>().text;
    28.         NetworkManager.singleton.networkAddress = ipAddress;
    29.     }
    30.  
    31.     void SetPort()
    32.     {
    33.         NetworkManager.singleton.networkPort = 7777;
    34.     }
    35.  
    36.     void OnLevelWasLoaded(int level)
    37.     {
    38.         if(level == 0)
    39.         {
    40.             //SetupMenuSceneButtons();
    41.             StartCoroutine(SetupMenuSceneButtons());
    42.         }else
    43.         {
    44.             SetupOtherSceneButtons();
    45.         }
    46.     }
    47.  
    48.     IEnumerator SetupMenuSceneButtons()
    49.     {
    50.         yield return new WaitForSeconds(0.15f);
    51.         GameObject.Find("HostButton").GetComponent<Button>().onClick.RemoveAllListeners();
    52.         GameObject.Find("HostButton").GetComponent<Button>().onClick.AddListener(StartupHost);
    53.  
    54.         GameObject.Find("JoinButton").GetComponent<Button>().onClick.RemoveAllListeners();
    55.         GameObject.Find("JoinButton").GetComponent<Button>().onClick.AddListener(JoinGame);
    56.     }
    57.  
    58.     void SetupOtherSceneButtons()
    59.     {
    60.         GameObject.Find("DisconnectButton").GetComponent<Button>().onClick.RemoveAllListeners();
    61.         GameObject.Find("DisconnectButton").GetComponent<Button>().onClick.AddListener(NetworkManager.singleton.StopHost);
    62.     }
    63. }
    I have no clue what happened, because everything is identical to that old project.

    I did get a new router since then, but I made sure to forward port 7777, and I'm giving my friend my external IP to connect to, but it just isn't working.


    Here is a picture of my Network Manager settings:


    and this is what the console outputs when I start up a server.


    I think it possibly has something to do with the UNASSIGNED_SYSTEM_ADDRESS and the fact that even though the port is hardcoded to use 7777, it's deciding to use 65535 (or at least that's what it appears to be doing).

    I've been trying to fix this for a few days and at my wit's end. If someone more knowledgeable in unet than I could help, that would be incredible.

    EDIT: I have also set up the project's multiplayer settings in the "Services" tab.
     
  2. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    I wouldn't read too much into the output from Network.player, I think it's really more of a leftover from the old networking system.

    You should check NetworkServer.listenPort, networkClient.serverPort, networkClient.serverIP. I think Network.player.ipAddress is fine though.

    Also output ipAddress in the SetIPAddress method just to make sure you've got something that looks like an IP there.

    Forwarding the ports is definitely the right thing to do but did you make sure to forward both TCP and UDP? Maybe post a screen shot of your router settings just to be sure.

    If you would like not never have port forwarding issues again, take a look at my plugin on the asset store :) http://u3d.as/qhh
     
  3. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    Hey, thanks for the reply! For some reason, I can't reference networkClient.serverPort or networkClient.serverIP. Any idea why?

    Also, NetworkServer.listenPort is returning 7777 like it should, and Debug.Logging ipAddress returns whatever IP I put into the text box (Here's how it's set up) so that doesn't seem to be the issue



    I'm also somewhat confused, because I have a UI Text element display Network.player.ipAddress, and the IP that is showing is different from what I get if I were to go to https://www.whatismyip.com/.



    Is there any particular reason for this?

    EDIT: Oh, and here's my port forward settings:



    So it looks like Network.player.ipAddress returns my Device IP and not my public IP address.
     
    Last edited: Jun 25, 2016
  4. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    I guess in your case it would be client.serverPort and client.serverIP since you are extending from NetworkManager and the default NetworkClient is just called "client."

    The port forwarding settings look good.

    Just as a side note, can't you just call "StartClient()" instead of "NetworkManager.singleton.StartClient()." You're extending from NetworkManager which has the StartClient() method in it so you should be able to just call it. I don't think it's going to hurt anything doing it the way you're doing it, but it just stands out to me as kind of odd.

    My next question would be what kind of actual error message the client is getting (if any) when trying to connect. Make sure to turn the Log Level up all the way and read through the output carefully, there may be some clues as to what's going wrong. Also give the client a good 30 seconds to try and connect so that you can get the error message if it times out.
     
    panthera4 likes this.
  5. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    Alright, thanks- when I do that I get 0 for the serverPort, and a blank message for serverIp. Does this mean that it's still trying to start the server on localhost?

    Changed it. The reference I was using wrote it that way, so I figured I'd just try to do it their way before I change things around.

    So, I turned the log level all the way up and it seems to hang when it gets to here for about 10 seconds.


    before returning



    My GoogleFu might just be weak, but I can't find much on DisconnectEvent error 6.

    If you need the log in its entirety I can screencap that.
     
    Last edited: Jun 25, 2016
  6. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    ranjithran likes this.
  7. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    I disabled my PC and router firewall



    and now I'm not getting the error message, but it still won't connect.



    Is this something that could be solved via your NAT Traversal plugin?

    EDIT: Back to something that I brought up earlier. When I Debug.Log client.serverPort and client.serverIp after calling SetPort and StartHost I get:

    maybe that has something to do with it?
     
    Last edited: Jun 25, 2016
  8. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    That actually seems right for the client on the Host since it is a "local client" it doesn't really need to know the port and ip. The client on the Client side should have the proper values set though.

    I'm really not sure what the issue is, maybe it's something on your friend's end and not yours somehow?

    I would like to believe my plugin would help you but the fact that it's not working even with manual port forwarding may indicate that their is some bigger issue, or that there is something wrong with the code that neither of us are seeing.

    Was that your whole NetworkManager you posted above or do you maybe have an Awake method? I think there either is or used to be some issue with having in Awake method. I have this in my NetworkManager's Awake method:
    Code (CSharp):
    1. Type type = typeof(UnityEngine.Networking.NetworkManager);
    2. MethodInfo baseMethod = type.GetMethod("Awake", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
    3. baseMethod.Invoke(this, null);
    Which just calls the base classes Awake method.

    That's just a total shot in the dark though, it's hard to say what the issue could be. I would definitely try having different people connect to you if you can.

    Your next step is either going to be something like Fiddler or Wireshark to take a peak into the actual network traffic or just try and get the attention of someone smarter than me to help you out :)

    P.S. Love your profile icon, Cave Story is my jam.
     
  9. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    Hey, thanks! Cave Story is my all-time favorite game and it's always great to see another fan!

    Yep, that's the entirety of my NetworkManager- no Awake method. I did change all of the NetworkManager.Singleton's to what you suggested, so now I've got this:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class NetworkManager_Custom_V2 : NetworkManager {
    7.  
    8.     public void StartupHost()
    9.     {
    10.         SetPort();
    11.         StartHost();
    12.         Debug.Log(NetworkServer.listenPort);
    13.         Debug.Log(client.serverPort);
    14.         Debug.Log(client.serverIp);
    15.     }
    16.  
    17.     public void JoinGame()
    18.     {
    19.         SetIPAddress();
    20.         SetPort();
    21.         StartClient();
    22.     }
    23.  
    24.     public void SetIPAddress()
    25.     {
    26.         string ipAddress = GameObject.Find("InputFieldIPAddress").transform.FindChild("Text").GetComponent<Text>().text;
    27.         //NetworkManager.singleton.networkAddress = ipAddress;
    28.         networkAddress = ipAddress;
    29.         Debug.Log(ipAddress);
    30.     }
    31.  
    32.     void SetPort()
    33.     {
    34.         //NetworkManager.singleton.networkPort = 7777;
    35.         networkPort = 7777;
    36.     }
    37.  
    38.     void OnLevelWasLoaded(int level)
    39.     {
    40.         if(level == 0)
    41.         {
    42.             //SetupMenuSceneButtons();
    43.             StartCoroutine(SetupMenuSceneButtons());
    44.         }else
    45.         {
    46.             SetupOtherSceneButtons();
    47.         }
    48.     }
    49.  
    50.     IEnumerator SetupMenuSceneButtons()
    51.     {
    52.         yield return new WaitForSeconds(0.15f);
    53.         GameObject.Find("HostButton").GetComponent<Button>().onClick.RemoveAllListeners();
    54.         GameObject.Find("HostButton").GetComponent<Button>().onClick.AddListener(StartupHost);
    55.  
    56.         GameObject.Find("JoinButton").GetComponent<Button>().onClick.RemoveAllListeners();
    57.         GameObject.Find("JoinButton").GetComponent<Button>().onClick.AddListener(JoinGame);
    58.     }
    59.  
    60.     void SetupOtherSceneButtons()
    61.     {
    62.         GameObject.Find("DisconnectButton").GetComponent<Button>().onClick.RemoveAllListeners();
    63.         GameObject.Find("DisconnectButton").GetComponent<Button>().onClick.AddListener(NetworkManager.singleton.StopHost);
    64.     }
    65. }
    66.  
    I'll try having someone else connect and report back with my results. If I'm not able to connect with a second PC though, would anyone else be able to? Right now I'm running the server on my laptop that has the most recent build on it whilst trying to connect through an instance run in play-mode in Unity.

    I'll take a look into those programs, do you have any suggestions on places to go to get further help?

    Thanks again for all of the help you've given- I really appreciate it!
     
  10. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    Probably not.

    This is pretty much the place to be. You can either try yelling louder around here or maybe give Unity Answers a try.

    Are you trying to connect from one computer to another in the same network? If so you may want to try using the local IP instead of the external IP.
     
  11. Deleted User

    Deleted User

    Guest

    try to use 8080 port by default.
     
  12. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    Just wanted to let you know that I picked up your asset that you linked in the beginning of the thread- and everything works fine now! I have a feeling it has something to do with the way my router is set up. Just in case someone else finds this thread, that is what I did to fix it.
     
    thegreatzebadiah likes this.
  13. Hexa-GD

    Hexa-GD

    Joined:
    Feb 12, 2018
    Posts:
    1
    Hello, I had the same problem it turned out my windows firewall whas nagging about it so I disabled it and it works fine now!
     
  14. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    Seems like exactly the same issue i had, but with firewall turned off, no router between me and line, and still no connect. I chalked it up as UNet does not support NAT Punchthrough. The only people i see saying they can connect with ease to anyone external IP, and have them connect back with external ip seems to be using some other networking package.

    I dunno, i think unity has purposely broken direct connection, conveniently right around the time UNet and their Paid Multiplayer services are released.. I haven't been able to get a single connection at all with real IP's, only LAN, or through matchmaker. Seems fishy to me.

    It just really sucks, because as a hobbiest, there's no way i'm shelling out money to buy networking that works, or anything really, unless i make something that stands a chance of selling, and nothing much is successful without online play these days.

    unitys paid services are a bit pricey and jokish to me, but that's just my opinion.