1

I have searched all most all the links in the internet about NAT traversing with C# and STUN. I got the public IP and the port that is using by the application. On most webpages i've read about STUN, a protocol, which should help me to connect to another client behind a NAT-Router or a firewall. Now my question... if I understand STUN, STUN is ONLY there to give me the public IP-Address and the characterization of my NAT.

But so HOW can I connect with this informations to another client?

here is the example I used to configure the STUN with my application http://www.codeproject.com/Articles/18492/STUN-Client

Sandaru
  • 1,229
  • 2
  • 21
  • 39
  • What specifically have you tried? Post some code examples and a specific question. – Ashigore Apr 28 '14 at 11:21
  • What is so hard to read the STUN specs and - ah - implement them? STUN is an algorithm. Obvously "I work by examples, not by reading the specs" won't help you much over the long term. – TomTom Apr 28 '14 at 11:23
  • This is my first experience with RDP and stun.. it it is hard me to understand the concept. I have an idea bout NAT and how it works. Problem is where i need to run the STUN server and how to use the clients public IP and port for initial hand shaking. and also how to keep the connection alive – Sandaru Apr 28 '14 at 11:31
  • Ashigore,TOmTOm I read all the specs and related documents and also i cannot past all of my code in here because it has 21 classes. You can say "But copy the selected code area"..... but nop i can't because im using command design pattern so definitely i need to post all the classes :( – Sandaru Apr 29 '14 at 11:21
  • STUN is not just a a way to get your public IP. It always ascertains the port mapping. Public port does not always equal internal listening port. Knowing both the remote IP and mapped public port are both essential. IP alone will not guaranteed NAT traversal success. – selbie Apr 30 '14 at 04:52

2 Answers2

2

You can try use http://en.wikipedia.org/wiki/UDP_hole_punching it's very simple and easy to implement

Suhan
  • 1,434
  • 2
  • 13
  • 28
  • 1
    I try to do something very similar, but it's really impossible to implement that by copy paste, so he will be forced to understand how it works) – Suhan Apr 28 '14 at 11:29
2

Here is a c# implementation:

http://www.codeproject.com/Articles/18492/STUN-Client

Example usage:

// Create new socket for STUN client.
Socket socket = new Socket
    (AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Any,0));

// Query STUN server
STUN_Result result = STUN_Client.Query("stunserver.org",3478,socket);
if(result.NetType != STUN_NetType.UdpBlocked){
    // UDP blocked or !!!! bad STUN server
}
else{
    IPEndPoint publicEP = result.PublicEndPoint;
    // Do your stuff
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372