stun client java实现_STUN Client

stun client java实现_STUN ClientIsyouremailaddressOK?Youaresignedupforournewslettersbutyouremailaddressiseitherunconfirmed,orhasnotbeenreconfirmedinalongtime.Pleaseclickheretohaveaconfirmationemail…

大家好,又见面了,我是你们的朋友全栈君。

Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions.

da60113721f2e742c85ed67225f2c075.png

Introduction

STUN – Simple Traversal of User Datagram Protocol (UDP) through Network Address Translators (NATs). In few words, it just helps you to map your local computer IP:port to public IP:port.

STUN working idea is pretty simple. The client just sends a UDP packet out to the STUN server and the server answers back with IP:port you connected. STUN does three tests to detect the NAT type.

74fd2798202fde65c452efa4582b9818.pngCollapse|Copy Code

In test I, the client sends a STUN Binding Request to a server,

without any flags set in the CHANGE-REQUEST attribute,

and without the RESPONSE-ADDRESS attribute. This causes the server

to send the response back to the address and port that the request came from.

In test II, the client sends a Binding Request with both the

“change IP” and “change port” flags from the CHANGE-REQUEST attribute set.

In test III, the client sends a Binding Request with only the “change port” flag set.

+——–+

| Test |

| I |

+——–+

|

|

V

/\ /\

N / \ Y / \ Y +——–+

UDP / IP \————->| Test |

Blocked \ ? / \Same/ | II |

\ / \? / +——–+

\/ \/ |

| N |

| V

V /\

+——–+ Sym. N / \

| Test | UDP

| II | Firewall \ ? /

+——–+ \ /

| \/

V |Y

/\ /\ |

Symmetric N / \ +——–+ N / \ V

NAT

\Same/ | I | \ ? / Internet

\? / +——–+ \ /

\/ \/

| |Y

| |

| V

| Full

| Cone

V /\

+——–+ / \ Y

| Test |——>/Resp\—->Restricted

| III | \ ? /

+——–+ \ /

\/

|N

| Port

+——>Restricted

///

/// UDP is always blocked.

///

UdpBlocked,

///

/// No NAT, public IP, no firewall.

///

OpenInternet,

///

/// No NAT, public IP, but symmetric UDP firewall.

///

SymmetricUdpFirewall,

///

/// A full cone NAT is one where all requests from the same internal

/// IP address and port are mapped to the same external IP address and port.

/// Furthermore, any external host can send a packet to the internal host,

/// by sending a packet to the mapped external address.

///

FullCone,

///

/// A restricted cone NAT is one where all requests from the same

/// internal IP address and port are mapped to the same external IP address and port.

/// Unlike a full cone NAT, an external host (with IP address X)

/// can send a packet to the internal host only if the internal host

/// had previously sent a packet to IP address X.

///

RestrictedCone,

///

/// A port restricted cone NAT is like a restricted cone NAT, but the restriction

/// includes port numbers. Specifically, an external host can send a packet,

/// with source IP address X and source port P, to the internal host only if

/// the internal host had previously sent a packet to IP address X and port P.

///

PortRestrictedCone,

///

/// A symmetric NAT is one where all requests

/// from the same internal IP address and port,

/// to a specific destination IP address and port, are mapped to the same external

/// IP address and port. If the same host sends a packet with the same source address

/// and port, but to a different destination, a different mapping is used.

/// Furthermore, only the external host that

/// receives a packet can send a UDP packet back to the internal host.

///

Symmetric

Using the Code

74fd2798202fde65c452efa4582b9818.pngCollapse|Copy Code

//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 serverSTUN_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}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160521.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • AndroidMainifest标签说明2——<activity>

    AndroidMainifest标签说明2——<activity>

    2022年1月10日
    43
  • activiti集成drools实验

    activiti集成drools实验

    2022年3月1日
    61
  • 云服务器上装软件吗,云服务器能安装软件吗?[通俗易懂]

    云服务器上装软件吗,云服务器能安装软件吗?[通俗易懂]云服务器能安装软件吗?有些时候电脑需要杀毒软件是很正常的事情,但是相比于这些庞大的云系统来说,一般的杀毒软件是根本运作不起来的,因为一旦安装了杀毒软件,肯定是要全部杀毒的,不可能这个系统可以,那个系统就不可以,这就谈到了一个空间包含的问题。如果包含量太小的话,也是没有什么作用的,那么云服务器能安装什么杀毒软件呢?什么样的杀毒软件才能够更好的运转起这个庞大的空间系统呢?对于庞大的空间系统,很多厂家为…

    2022年5月23日
    53
  • 第10月第28天 touchesBegan hittest「建议收藏」

    第10月第28天 touchesBegan hittest「建议收藏」1.-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event{[[selfnextResponder]touchesBegan:toucheswithEvent:event];[supertouchesBegan:toucheswithEvent:event];}-(voi…

    2022年7月25日
    8
  • 3dmax建模前景_3d建模师未来发展前景

    3dmax建模前景_3d建模师未来发展前景简单来说,没前途。本来不想说这个话题的,但是想了好久,觉得还是来说两句。我本人是从事建筑表现效果图行业的,主要用的软件就是3DMAX+VRay+PS。正式从业到现在已经三年了,半路出家。3DMAX这个软件深入很多个行业,广告、影视、工业设计、建筑设计、三维动画、多媒体制作、游戏、以及工程可视化等。但其实没有哪个行业是只用这一款软件的,在不少行业3DMAX也逐步被更新的软件替代。MAX强大在它是一个平台,所以他的上限很高,比一般的软件都要高很多,但是下限也很高,不是那么容易上手。.

    2025年10月28日
    3
  • Running LuaJIT「建议收藏」

    Running LuaJIT「建议收藏」原文:http://luajit.org/running.htmlLuaJIThasonlyasinglestand-aloneexecutable,called luajit onPOSIXsystemsor luajit.exe onWindows.ItcanbeusedtorunsimpleLuastatementsorwholeLuaap…

    2022年10月6日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号