I’m making a multiplayer game, and I’m currently trying to get automatic portmapping to work. When making a socket and setting its IP protection in visual studio, there’s no errors, but in unity, it says:
“SocketException: An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call.”
The level of protection I’m trying to use is unrestricted, but I’ve tested the other levels, and they don’t work either.
I am using Open.NAT for the portmapping, and I am using Unity 2021.3.9f1.
Here is the code:
public class AutomaticPortMapping : MonoBehaviour
{
public async void Start()
{
NatDiscoverer discoverer = new NatDiscoverer();
// using SSDP protocol, it discovers NAT device.
NatDevice device = await discoverer.DiscoverDeviceAsync();
// display the NAT's IP address
print("The external IP Address is: " + await device.GetExternalIPAsync());
// create a new mapping in the router [external_ip:1702 -> host_machine:1602]
await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 1602, 1702, "For testing"));
// configure a TCP socket listening on port 1602
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 1602);
Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.SetIPProtectionLevel(IPProtectionLevel.Unrestricted);
socket.Bind(endPoint);
socket.Listen(4);
}
}
The code is directly copied from Open.NAT - A NAT Traversal library for .NET and Mono - CodeProject, so it should work, but it might be outdated.