Search Unity

TcpListener, Sockets -> AllowNatTraversal & SetIPProtectionLevel

Discussion in 'Multiplayer' started by GalaadMoutoz, Apr 16, 2020.

  1. GalaadMoutoz

    GalaadMoutoz

    Joined:
    Apr 6, 2018
    Posts:
    6
    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 :
    Code (CSharp):
    1. var endPoint = new IPEndPoint(IPAddress.Any, 1602);
    2. var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    3. socket.SetIPProtectionLevel(IPProtectionLevel.Unrestricted);
    4. socket.Bind(endPoint);
    5. 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
     
  2. Zebadiah

    Zebadiah

    Joined:
    Apr 6, 2014
    Posts:
    67
    I'm not totally sure why this doesn't work, but if you're looking for an existing solution for NAT Traversal you may want to look in to my NAT Traversal asset, or the newer better Noble Connect asset that included both NAT Traversal and relays.