UDP Hole Punching help

有些话、适合烂在心里 提交于 2019-12-03 09:20:14

UDP hole punching is a complex topic, and many attempts have been made to find a solution that works. The problem is that there are so many different NAT routers out there, and there is no standard way of implementing NAT, so all routers behave a bit different.

Some attempts have been standardized, e.g. STUN, ICE. They still work only in most cases, not all, but they accumulate a lot of knowledge and address many concerns -- more than your attempt will ever be able to, simply because you can't test your solution with every NAT router under the sun. Skype, for example, spent years of perfecting its NAT traversal mechanism.

I recommend you have a look at STUN or preferably ICE. Try to implement one of them or look for existing implementations.

Another option might be to simply create a port forward at the router, e.g. using UPnP or NAT-PMP.

That said, I wouldn't be surprised if the .NET peer to peer classes came with a NAT traversal mechanism, but I'm not familiar with them.

STUN is a best solution for the problem. it works for most of the scenarios.here is a simple example(C#) that gives you the NAT type,Local IP:Port and Public IP:Port.

       try
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.Bind(new IPEndPoint(IPAddress.Any, 0));

            STUN_Result result = STUN_Client.Query("stunserver.org", 3478, socket);
            Console.WriteLine("Net Type : " + result.NetType.ToString());
            Console.WriteLine("Local IP : " + socket.LocalEndPoint.ToString());
            if (result.NetType != STUN_NetType.UdpBlocked)
            {
                Console.WriteLine("Public IP : " + result.PublicEndPoint.ToString());
            }
            else
            {
                Console.WriteLine("");
            }
        }
        catch (Exception x)
        {
            Console.WriteLine(x.StackTrace.ToString());
        }

You can just add some breakpoints and check the behaviour of the code.

also you can traverse NAT using vs2010 onwords (.net4.0 < ) . there is a method AllowNATTraversal (bool value). set the value true for NAT Traverse. It uses Ipv6 address for connectivity. You can get Ipv6 global address from "Teredo" server by writing some commands in command prompt. Actually IPV6 is the technology that is used to avoid this problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!