Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Trying to understand the new UNet - no luck

Discussion in 'Connected Games' started by Homicide, Feb 20, 2018.

  1. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209
    i say new, i mean, i havent used unity since 3.5, and back then it was all about the network view.

    anyways, i'm finding it very hard to get whats going on with the new system, so i hit the net in hopes of finding some proper IP based networking, and sadly, unity has next to no documentation other than localhost, which just doesn't cut it in todays world.

    So i searched and searched and found this tutorial ... but alas, i cant get it working. i'm assuming its because i'm missing some teeny step 12 tutorial videos back or something silly, but i'm not really interested in his whole project from zero to hero, just the networking aspect.



    I believe i am on the right track, but somethings not right. I cant connect at all. Just get Client timed out.

    i have tried localhost 127.0.0.1 also , just to check, because in his tutorial he uses localhost as well , :( sigh


    i have a simple playerPrefab build with a network identity component and a network transform component
    i have this custom network manager code (modeled after the tutorial) attached to an empty gameObject,

    i have the button wired to correct JoinGame(); I just don't understand why i cant connect.

    here's the code i got

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7. public class A3DNetworkManager : NetworkManager {
    8.  
    9.     public InputField ipField;
    10.  
    11.     public void StartupHost()
    12.     {
    13.         NetworkManager.singleton.StartHost();
    14.     }
    15.  
    16.  
    17.     public void SetPort()
    18.     {
    19.         NetworkManager.singleton.networkPort = 25000;
    20.     }
    21.  
    22.     public void SetIpAddress()
    23.     {
    24.         NetworkManager.singleton.networkAddress = ipField.text;
    25.         Debug.Log(ipField.text);
    26.     }
    27.  
    28.     public void JoinGame()
    29.     {
    30.         SetIpAddress();
    31.         SetPort();
    32.         NetworkManager.singleton.StartClient();
    33.     }
    34.  
    35.     public void DisconnectHost()
    36.     {
    37.         NetworkManager.singleton.StopHost();
    38.     }
    39.  
    40. }
    I'm hoping someone can point in the right path to IP Based connection, as local host is almost useless these days, yet so well documented and was easy quick to do ... but only through built in manager and only for lan. The new UNet has me baffled. Thanks.
     
    Last edited: Feb 20, 2018
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,714
    I've seen a question like this before, not sure if it was you, but if you can get it working for localhost it should be the same for another IP address.
     
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209
    so i thought, and nah wouldnt have been me. ive just a few posts since i returned.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,714
    When you go through an external IP, you may have to allow it on your router and port forward? That's all I can think of that differs. *For the server.
     
  5. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209
    im running in DMZ... i assumed that would alleviate that burden.

    But still , i cant quite help feel as if theres just something simple im missing.

    I mean, from what i remember before it was fairly easy in mere code, anda network view or two. But man , im just stumbled on this now. Well... keep on truckin eh ? :p thanks
     
  6. Ellernate

    Ellernate

    Joined:
    Aug 25, 2017
    Posts:
    81
    I have unet running on remote servers, the only difference between local testing is changing the IP that client connects to
     
  7. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209
    i get that, but still im at a loss. i cant find much on the subject as far as snippets. the unity docs are bare bones. ah well ty.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,714
    Okay, I would imagine DMZ should work.

    I tried a script like this, as a quick test in the editor, and it worked. I used my external IP address with port forwarding. Without port forwarding, it didn't (of course), and only local worked.
    Code (csharp):
    1. public class Test12 : NetworkManager
    2. {
    3.  
    4.     public override void OnClientConnect(NetworkConnection conn)
    5.     {
    6.         base.OnClientConnect(conn);
    7.         print("Hello, from client.");
    8.        
    9.     }
    10.     public override void OnServerConnect(NetworkConnection conn)
    11.     {
    12.         base.OnServerConnect(conn);
    13.         print("Server got client.");
    14.     }
    15.     void Update()
    16.     {
    17.         if (Input.GetKeyDown(KeyCode.S))
    18.         {
    19.             singleton.StartServer();
    20.         }
    21.         if (Input.GetKeyDown(KeyCode.C))
    22.         {
    23.             singleton.networkAddress = "my actual ip was here";
    24.             singleton.networkPort = 7777;
    25.             singleton.StartClient();
    26.         }
    27.     }
    28. }
     
  9. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209

    thanks methos, i will try again today and see what i can sort out. Appreciate that alot man :p its just been real hard to wrap my head around this after being so used to the old setup.

    Alas, im off to begin another day of deciphering UNet. I will keep ya posted on my trials and if i can get it working. Ty again.
     
  10. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209

    So i have taken exactly what you have there, Methos, and made only a change to Print() cause... i have no way to view that. I tried Debug.Log() instead, didn't work, so i tried adding this Text ui, still no luck.

    But after running it once, it spits out error failure to open socket on port (likely already in use)..... so i added Server.Reset()... seems to have fixed it, but i don't know. as theres no message being delivered anywhere it seems. At least not that i have been able to recieve and view / display.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7. public class Test12 : NetworkManager
    8. {
    9.     public Text txt;
    10.  
    11.     public override void OnClientConnect(NetworkConnection conn)
    12.     {
    13.         base.OnClientConnect(conn);
    14.         txt.text = "Hello, from client.";
    15.  
    16.     }
    17.     public override void OnServerConnect(NetworkConnection conn)
    18.     {
    19.         base.OnServerConnect(conn);
    20.         txt.text = "Hello, from client.";
    21.     }
    22.     void Update()
    23.     {
    24.         if (Input.GetKeyDown(KeyCode.S))
    25.         {
    26.             //NetworkServer.Reset();
    27.             singleton.StartServer();
    28.         }
    29.         if (Input.GetKeyDown(KeyCode.C))
    30.         {
    31.             singleton.networkAddress = "xxx.xxx.xxx.xx";
    32.             singleton.networkPort = 7777;
    33.             singleton.StartClient();
    34.         }
    35.     }
    36. }
    the xxx's are actually my real ip added in.

    I'm gonna try creating a simple player prefab now and working it into network, but im at a loss cause i cant really determine if anythings happening with out Print() Debug.Log() or txt UI updainting. :(

    Does Network.Instantiate() stil work the same way? gawd, i'm fuzzled. lol Will update in a bit.
     
    Last edited: Feb 20, 2018
  11. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209
    Ok. I have returned to square one and started over with the network tutorial, but because its based around localhost, i figured the bind ip server option would be what i might need?

    demo3.png

    Cannot open socket on ip {"my ip address is shown here" } and port {25000}; check please your network, most probably port has been already occupied
    UnityEngine.Networking.NetworkManagerHUD:OnGUI()

    StartServer listen on "my ip address is shown here" failed.
    UnityEngine.Networking.NetworkManagerHUD:OnGUI()

    man its gonna be another one of these days. for the record, i tried port 777 etc. argh.
     
    Last edited: Feb 20, 2018
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,435
    ServerBindtoIP is for when your server has multiple IP addresses, but you want it to service requests only on a single address. For example, you have a server with 2 network adapters, one that connects to an internal network, and one that connects to the Internet, you'd want to bind to the Internet adapter's IP if you are only expecting connections to come from the Internet and not from the internal network. By default it should respond on any IP address the server has. If you are using this, it would be the IP address assigned to your server, even if an internal NAT IP address, not the public IP address of your router.

    Though there was a reported issue when not using ServerBindtoIP when the server has multiple IP addresses a couple years ago. I don't know if it has been fixed.

    https://forum.unity.com/threads/une...-results-in-faild-connection-attempts.387112/

    I'd really recommend having 2 computers on the same network, and get it working between them, before taking the further step of involving the router and port forwarding or DMZ settings. As already mentioned, all the tutorials use localhost because there is nothing different about using localhost and another IP address other than you then involve properly setting up firewall and port forwarding settings that differ between OS and router model.

    Also be aware some company and University networks will actively block hosting servers, and possibly some home Internet providers. Which is yet another reason to try two computers on the same network first, so you know for sure your game code is solid, before you involve other hardware and services in the mix.
     
    Last edited: Feb 20, 2018
  13. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209
    I have a computer and my android phone with which im building to.

    I can build the tutorial project and use the NetworkManager and NetworkManagerHUD to connect fine between to the two devices on android and pc build either direction using 192.168.0. etc. I even managed to find the source code for the HUD and tweak the values so the gui fits on the android properly and can be seen :p

    however, the moment i try using my real external isp IP address , nothing works. this is what im trying to figure out.

    i have changed the value in the network manager "Network Address" , i have tried just manually inputting it from either device as a client connecting to the other device : server... its just not working.

    i wanted to test connection with a friend on other side of country, but nothing works except internally. i keep seeing everyone saying that the only difference from localhost to real IP based games is the address... so now im just totally friggin lost.

    Still think theres something wee minor im overlooking.
     
    Last edited: Feb 21, 2018
  14. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209
    Perhaps i should ask this in a different way, as easy as i can think?

    If the idea behind local play is the same except the ip address change, then someone do please tell me...

    "Where", what option, variable, setting, etc... what, or where does one enter the IP address?

    should the IP address work just typed in the HUD's Local"Client" connect where it says localhost?

    This is assuming, i am using an empty project, with a player prefab that has a network identity, and network transform component added.

    The network game object has the NetworkManager and NetworkManagerHUD component.

    the player prefab is assigned to the playerPrefab setting in the networkManager.
     
  15. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209
    When i check netstat -a it shows server using port 7777, but its UDP.... shouldnt this be tcp? Also weird is that when i use the port detection web site, it claims any port i try is closed. In DMZ mode, and... with no router even attached. O>o
     
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,714
    If you are getting a message that the address is in use, then either you didn't properly close it before or maybe it's waiting to close .. if you find port 7777 isn't normally open but becomes open (in netstat) when you start the server, then things should work.
    For the record, I also couldn't see udp port 7777 open from can youseeme.org but it still worked :)
    It is supposed to be udp, not tcp.

    I may not have had the server open when i checked its visibility remotely, come to think of it, and/or perhaps it doesn't report udp ports open :)

    As for the hosting side, that can always say localhost, and so long as it's forwarded (/dmz?), it should work, afaik. If you want to try forwarding instead of dmz just to be sure, I would do that.. then at least you can confirm.
     
    Homicide likes this.
  17. cephalo2

    cephalo2

    Joined:
    Feb 25, 2016
    Posts:
    212
    I had a weird issue where my project would not connect in a debug build, but worked fine in a release build. Frustrating. You might try that and see what happens.
     
  18. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    209

    To explain fully. My scene is now real basic, only a camera. Attached to camera is a network view, a simple server script, a simple RPC script, all as seen below.


    And that's it. Basic, should work, no? But alas, not one connection made over 3 days. With DMZ activated, with Router COMPLETELY REMOVED... doesn't seem to matter. Doesn't connect.

    I have also installed older version 4.5.1 3f, as noted by the networkView RPC, and even its not working. Im at a loss.


    I don't understand. I do HAVE it built as a real application, not through the editor, my friend is trying to use my real IP to connect, it DOES show in netstat -s... but no listening, no connection, its not connecting.

    for the record, everything else from android phones, all apps, nvidia shield tv, computer, internet, all working , no issues, just no connect through unity server / network.

    demo4.png

    you can see port 7777 is open there it seems, but no contact, no nothing.

    im using a simple press spacebar to send RPC via netwrk view after server starts.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SimpleServer : MonoBehaviour {
    5.  
    6.     public int playerCount = 8;
    7.     public int serverPort = 7777;
    8.     public bool useNat;
    9.     public bool serverStarted = false;
    10.     public string ipAddress = "000.000.000.00"; // my real ip is entered here in build.
    11.  
    12.     public void StartServer(){
    13.         useNat = !Network.HavePublicAddress ();
    14.         Network.InitializeServer (playerCount, serverPort, useNat);
    15.         serverStarted = true;
    16.     }
    17.  
    18.     public void JoinServer(){
    19.         Network.Connect (ipAddress, serverPort);
    20.     }
    21.  
    22.     void OnPlayerConnected(NetworkPlayer player){
    23.         Debug.Log (player.externalIP);
    24.     }
    25.  
    26.     void OnGUI(){
    27.  
    28.         if (!serverStarted) {
    29.             if (GUILayout.Button ("Start Server")) {
    30.                 StartServer ();
    31.             }
    32.         }
    33.         if(GUILayout.Button ("Connect")){
    34.             JoinServer ();
    35.         }
    36.     }
    37. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. [RequireComponent (typeof (NetworkView))]
    6. public class RemoteCaller : MonoBehaviour {
    7.  
    8.     public bool displayLog = true;
    9.     string log = "";
    10.     public string myName = "byte";
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         if(Input.GetKeyDown(KeyCode.Space)) {
    15.             SendGreeting(myName);
    16.         }
    17.     }
    18.  
    19.     void SendGreeting(string myName) {
    20.         networkView.RPC("PrintGreeting", RPCMode.Others, myName);
    21.         Log("Greeting Sent");
    22.     }
    23.  
    24.     [RPC]
    25.     public void PrintGreeting(string name) {
    26.         Log(name + " says: Hello!");
    27.     }
    28.  
    29.  
    30.     private void Log(string message) {
    31.         //Some simple logging on screen so we don't have to worry about the debug console.
    32.         log += "\n" + message;
    33.     }
    34.  
    35.     void OnGUI() {
    36.         //Some simple logging on screen so we don't have to worry about the debug console.
    37.         if(displayLog)
    38.             GUI.TextArea(new Rect(220, 245, 230, 400), log);
    39.     }
    40. }
    41.  

    Thanks Methos for checkin in again :p
     
    Last edited: Feb 21, 2018
  19. billybob1978

    billybob1978

    Joined:
    Dec 12, 2016
    Posts:
    5
    did you figure this out? seems the network manager just switches whatever IP you have to localhost and doesn't care.