1

I am currently working with the UNet client-server system in order to do networking and in order to be able to search for all devices on a port beyond LAN as suggested by this previous question. After searching around I found a suitable C# port-forwarding library from this website and have been following the instructions in order to be able to discover devices on ports. So far I have turned those instructions into the code below.

using UnityEngine;
using System.Collections;
using Mono.Nat;

public class PortForwarding : MonoBehaviour {

    void Start()
    {
        NatUtility.DeviceFound += DeviceFound;
        NatUtility.DeviceLost += DeviceLost;


        Debug.Log("Discovery Started");
        NatUtility.StartDiscovery();
    }





    private void DeviceFound(object sender, DeviceEventArgs args)
    {
        Debug.Log("DeviceFound");
        INatDevice device = args.Device;
        device.CreatePortMap(new Mapping(Protocol.Udp, 10000, 10000));
        // Can be .Udp or Tcp but both create no results
        foreach (Mapping portMap in device.GetAllMappings())
        {
            Debug.Log(portMap.ToString());
        }
        // on device found code
    }

    private void DeviceLost(object sender, DeviceEventArgs args)
    {
        INatDevice device = args.Device;

        // on device disconnect code
    }


}

In my scene I have three empty GameObjects each with a script, one is to create a server, the second creates a client and the other is to test port-forwarding (the first two scripts are shown here). Unfortunately I am getting no result, hence today I am asking you where have I gone wrong and how could I use this library to discover clients or servers beyond LAN.

If I have missed any details out or you require more in order to solve my problem just ask.

enter image description here

Community
  • 1
  • 1
g_l
  • 201
  • 1
  • 6
  • 15
  • I am looking into this and will let you if I find anything useful – Programmer Jul 31 '16 at 14:24
  • Thanks, it seems an easy method of discovery – g_l Jul 31 '16 at 14:27
  • I tried your code and it works. It's good to subscribe to an event once instead of every frame. Putting `NatUtility.DeviceFound += DeviceFound;` in the update function is not a good idea. I updated your code to fix that. Attach the updated code in your question to a gameobject and tell me what log you saw. – Programmer Jul 31 '16 at 15:41
  • @Programmer I am getting no change in logs, I'm uploading a picture of them (when using your code). If necessary i can get a screen video of my setup as you said it was working for you. – g_l Jul 31 '16 at 16:52
  • You are not getting any log at-all? You are not even getting `Discovery Started` log? Attach the script to a gameobject that is enabled.... – Programmer Jul 31 '16 at 16:56
  • No change in logs from the previous code that I was using, the picture is now attached above – g_l Jul 31 '16 at 16:58
  • Please disable any other script running. Just let this only this script run until the problem is solved. Also where did you put the `Mono.Nat.dll` file? – Programmer Jul 31 '16 at 17:01
  • In the assets, still I am getting no device found log. I'm trying it in a new project with a single empty object – g_l Jul 31 '16 at 17:03
  • What unity version and What Os version? – Programmer Jul 31 '16 at 17:10
  • Unity 5.3.4f1 Personal – g_l Jul 31 '16 at 17:12
  • Windows 10 (Version 1511?) – g_l Jul 31 '16 at 17:14
  • Open up firewall and make sure that Unity is not blocked.You can google hoe to do this on Win10. Also update to Unity 5.4 since that's the version I am using. – Programmer Jul 31 '16 at 17:19
  • Should I need to open up my firewall in the first place as would the user do that anyway? It should work with firewall shouldn't it... – g_l Jul 31 '16 at 17:22
  • No, but sometimes, firewall automatically blocks an application. It happens sometimes but it is easier for users to unblock firewall than for them to manually perform port-forwarding in the router setting. – Programmer Jul 31 '16 at 17:30
  • Have done both and still the logs do not change @Programmer – g_l Aug 01 '16 at 09:32

0 Answers0