C# How to send string via UDP without Port Forward

2019-09-14 19:02发布

I need something simillar to this:


        var client = new UdpClient();
        IPEndPoint ep = new IPEndPoint(IPAddress.Parse("23.114.44.195"), 11000);
        client.Connect(ep);

        byte[] data = Encoding.ASCII.GetBytes("test");
        client.Send(data, data.Length);

The listener runs on that IP at PORT: 11000, but i don't want to port forward in my router (because I want to publish this app).

So, here is the question: How to send string via UDP without Port Forwarding?

标签: c# udp
1条回答
疯言疯语
2楼-- · 2019-09-14 19:32

You can use NAT hole punching, which doesn't require manual port forwarding configuration. It's quite simple technique. Please note that remote endpoint (receiver) should implement it too, not just sender.

In the simplest case, it works as:

[sender] <-> |internet| <-> [remote router with NAT] <-> [receiver]

Please note sender is not behind NAT.

To punch a hole from senderIP:portX to receiverIP:portY: sender binds to portX and waits for communication initialisation from receiver. receiver binds to portY and sends a dummy packet to senderIP:portX. sender gets receiverIP (which is actually its router IP) and portY from the package and now sends meaningful data to this address. NAT remembered that communication with senderIP:portX was going from receivers local IP and portY, so all data coming from senderIP:portX to the router's portY will be redirected to receivers local IP.

For the case when both sender and receiver are behind NATs, you need a rendezvous server, for details check the link.

查看更多
登录 后发表回答