0

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?

Damian
  • 1,084
  • 3
  • 14
  • 26
  • As far as I know that can't be done without a server in the middle that both clients would connect to and at the very least, rely the messages from one to the other. – Vlad Ciobanu May 31 '17 at 10:20
  • So if I want to send something withouth a server to an IP with a router, I have to port forward? – Damian May 31 '17 at 10:26
  • only port forwarding or udp hole punching or stun server – Leonid Malyshev May 31 '17 at 10:29
  • Have a look the resources in the answer to this question, it will explain far better than I could. https://stackoverflow.com/questions/575448/how-does-peer2peer-work-through-a-router – Vlad Ciobanu May 31 '17 at 10:29
  • Thanks in advance, i'll read it. How about UDP Hole Punching? What is this and how it works? – Damian May 31 '17 at 10:37
  • I'm not sure what the issue is. You are not using broadcast, nor Multicast. Your port number is 11000 which normally isn't blocked. So your UDP should be the same as a TCP connection (without ACK/NACK). So why do you need port forwarding? Router should not be blocking standard UDP messages. – jdweng May 31 '17 at 10:41
  • I don't know why, but I don't receive the string, also I port forwarded and I received it. – Damian May 31 '17 at 10:47

1 Answers1

3

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.

Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112
  • Ok, telling this to me it's easy, but either you do or not, I still don't know how to implement it *sad*. Any piece of code that could help me? – Damian May 31 '17 at 11:08
  • what exactly don't you understand? You can easily google a couple of C# implementations, e.g. https://stackoverflow.com/questions/9140450/udp-hole-punching-implementation, https://stackoverflow.com/a/12229756/453271 – Andriy Tylychko Jun 01 '17 at 09:54
  • Thanks for your help, altough I found another way. C# programmatically port forwarding. – Damian Jun 01 '17 at 21:50