- Home /
Can I use TcpListener.AllowNatTraversal or Socket.SetIPProtectionLevel in Unity ?
Hello,
I'm implementing NAT Punchtrough with the library Open.NAT. Everything works well except that the server needs to use a socket that allows Nat Traversal.
Unfortunately, the method .AllowNatTraversal(true) from a TcpListener, and the lower-level version, the method SetIPProtectionLevel(IPProtectionLevel.Unrestricted) from a Socket, both throw an error :
System.Net.Sockets.SocketException (0x80004005): An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call.
Just in case, here is the socket version code :
var endPoint = new IPEndPoint(IPAddress.Any, 1602);
var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.SetIPProtectionLevel(IPProtectionLevel.Unrestricted);
socket.Bind(endPoint);
socket.Listen(4);
The exact same code works fine in a C# console program.
This error is only thrown in Unity.
Would anyone happen to have an idea why ?
Thank you for your time,
Galaad
Answer by dajohnso · Apr 20, 2020 at 02:01 PM
First I would check all the arguments to ensure they are not null and then I would check the value of endPoint and socket before I use them. Additionally anytime you are performing these operations it good practice to have them in an if/then statement to handle errors so the program doesn't crash if they are null or invalid but is instead handled by your own programming.
I suspect your socket.SetIPProtectionLevel is failing because the socket is null or -1 due to a bad socket call not a bad SetIPProtectionLevel call.
Nope, already checked for that. $$anonymous$$oreover, as I said, the exact same lines work fine in a standard C# program. The error only happens in Unity (currently using v2019.3). Does this code run on Unity for you ? Thanks for trying anyway :)
Been program$$anonymous$$g for 30 years but I'm new to unity. Are you using visual studio, if so what version? When you compile the code in Unity is it using the "community" version or do you have it setup to use your full version? I was suspecting that maybe the enum for IPProtectionLevel.Unrestricted may not have a valid value?
What version of .net framework is in your Unity build v.s. external build?
https://docs.unity3d.com/2019.1/Documentation/$$anonymous$$anual/dotnetProfileSupport.html
Also, what version of unity are you building under?
Hey Galaad, I'm having this exact same issue, did you actually found a solution?
Hi SuperSerch, no I didn't find a solution for this. There was an explanation on the why it didn't work in Unity, but I don't remember it, sorry. I switched to PhotonBolt which is a nice networking solution. Good luck.
Your answer
Follow this Question
Related Questions
tcp socket error message (C#) 1 Answer
Asynchronous socket hangs intermittently on iOS 1 Answer
UDP Sockets for networking, getting overloaded?? 0 Answers
.NET Sockets Application question 0 Answers