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.