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


相关推荐

  • Spring概述「建议收藏」

    Spring概述「建议收藏」Spring概述

    2022年4月22日
  • st7789 旋转_有没有人调过 ST7789V驱动的显示屏啊「建议收藏」

    本帖最后由quanhengwen于2019-9-511:15编辑可以在显示命令之前,手工进行全屏清屏操作,可以避免初始化花屏现象。自己部分代码SPI控制的分辨率为320*240横屏显示/******************************************************************************函数说明:LCD初始化函数入口数据:无返回…

    2022年4月6日
  • drone无人机app下载苹果版_drone无人机教程视频

    drone无人机app下载苹果版_drone无人机教程视频目录文章目录目录摘要1.实例化无人机及地面站控制对象2.绑定服务3.增加连接点击事件及进行连接摘要本节主要记录HelloDrone的连接过程及Mavlink协议解析过程。1.实例化无人机及地面站控制对象核心内容:controlTower=newControlTower(context);drone=newDrone(context);@Override/***重写onCreate方法,在onStart方法之前启动*/pro

    2022年8月15日
  • DirectX修复工具全部文件修复失败或下载失败的解决方法

    DirectX修复工具全部文件修复失败或下载失败的解决方法最后更新于2019-2-24问题描述  近日接连接到一些用户的反馈,说他们在使用DirectX修复工具的时候,所有文件的状态均为修复失败或者下载失败(如下图)。虽然他们使用的是标准版或增强版而非在线修复版,程序还是会尝试下载需要的DirectX组件。注:由于我无法复现此现象,因此此图片来自网络截图。所有权归原上传者所有。  之后他们也都按照程序的提示以安全模式启动系统…

    2022年6月9日
  • 番茄时间计时器_番茄计时器怎么调时间

    番茄时间计时器_番茄计时器怎么调时间今天读书学到个番茄时间,不想下app,写了个html,在手机上打开,横屏,是一样的计时效果。方法很笨,问题很多,勉强能用。因为很简陋,没有调,如果不按顺序按键,可能崩溃。1.先选时间,30分钟

    2022年8月2日
  • Pycuda 学习笔记(二)gpuarray学习

    Pycuda 学习笔记(二)gpuarray学习本博客参照学习文档https://documen.tician.de/pycuda/array.html通过GPUArray矩阵类可以将numpy中的数组和矩阵直接转换成cuda可处理类型,该步骤即将CPU中的数据复制到GPU中。导入gpuArray和numpyimportpycuda.gpuarrayasgpuarrayimportnumpyasnp将numpyndar

    2022年11月3日

发表回复

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

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