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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 一个interface可以继承多个interface_java语言支持单继承和多继承

    一个interface可以继承多个interface_java语言支持单继承和多继承   搞Java也有两个年头多了 ,今天在修改程序时无意中发现,Java接口中继承了多个接口,哎,真是惭愧直到现在才搞明白。于是就赶紧写了一个例子:packagecom.iman.wrms.t;publicinterfaceIOne{ publicvoidone();} packagecom.iman.wrms.t;publicinterfaceIT

    2022年10月20日
    6
  • 怎样发外链,使网站能快速收录,秒收录

    怎样发外链,使网站能快速收录,秒收录本文来自:优优蜘蛛池(http://www.zhizhuchi.vip)1.首先就是大家都熟知的百度网站提交2.利数桥带族用的周边产品添加网站的外链吸引蜘蛛进入你的网站。3.写一篇原创文章加上自己的网站链接后投稿到大型的站长网站。4.到高权重的论坛注册账号,这个人签名里添加上自己网站的超链接,发发贴,顶顶贴,就能无形中增加了。2:利用公司名字和地址在分类信息做外链。3:利用论坛昵称做高质量外链。4:在帖子内容中巧带二级域名链接。SEO优化之网站怎么实现百度秒收录何谓”秒收录”?大家可以经常

    2022年5月17日
    56
  • SqlSessionFactory介绍[通俗易懂]

    SqlSessionFactory介绍[通俗易懂]SqlSessionFactory是MyBatis的关键对象,它是单个数据库映射关系经过编译后的内存镜像。SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象来获得,而SqlSessionFactoryBuildr则可以从XML配置文件或一个预先定制的Configuration的实例构建出SqlSessionFactory的实例。每一个MyB…

    2022年5月2日
    39
  • python画qq图_python绘制散点图

    python画qq图_python绘制散点图qq图有两个作用:1、检验一组数据是否服从某一分布。2、检验两个分布是否服从同一分布。qq图全称是quantile-quantileplot,从名称中可以了解到是和分位数相关的图。由于最近在做数据分析时用到了,然而看了一些博客,要么是qq图讲解的比较详尽但是没有使用Python;要么是使用Python语言但是没有讲清楚原理。基于此,想写一篇博客尽量讲清楚原理并且用Python实现出来。qq图原理…

    2022年8月10日
    30
  • c盘扩展卷灰色无法操作的解决办法_win10c盘扩展卷是灰色的

    c盘扩展卷灰色无法操作的解决办法_win10c盘扩展卷是灰色的今天有百事网网友“丅亿页”遇到了这样一个问题:电脑C盘剩余容量太小,在看到百事网的一篇“如何合并磁盘分区windows7调整分区大小方法”文章后,也想将自己C盘系统盘空间扩大。按照上面文章中介绍的步

    2022年8月1日
    12
  • 解决SecureCRT 中文乱码

    解决SecureCRT 中文乱码在linux服务器上搭建solr,用的是SecureCRT 连接linux服务器,发现不能输入中文,配置文件中的中文也是乱码;先以为是SecureCRT工具编码的问题,把编码改成utf-8之后发现还是有乱码;最后才发现其实还有一个地方没有改配置,那就是字体必须是中文字体,而且字符集得是支持中文的字符集如gb2312:还有就是这里不能选择带@的中文字体,不然字体就是躺着的

    2022年7月17日
    19

发表回复

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

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