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)
上一篇 2022年7月17日 下午9:16
下一篇 2022年7月17日 下午9:16


相关推荐

  • 【已解决】罗技K380蓝牙键盘可以连接电脑,但无法输入怎么办?[通俗易懂]

    【已解决】罗技K380蓝牙键盘可以连接电脑,但无法输入怎么办?[通俗易懂]【问题】罗技k380可以连接并正常使用平板、手机。电脑显示着已连接,但就是无法输入。之前是可以用的,最近需要频繁配对突然不能用了。网上的各种方法都不好使,问京东客服也解决不了准备申请售后。【解决方案】后来仔细看了下k380使用方法,发现Fn+F1可以重置键盘的蓝牙设备,于是在电脑删除了k380蓝牙设备后,在蓝牙键盘上进行了Fn+F1重置,最后进行重启配对就ok了。可能是频繁配对让键盘有了缓存错误了。

    2022年10月16日
    4
  • docker 安装 shipyard(中文版) 集群管理平台

    docker 安装 shipyard(中文版) 集群管理平台1 安装 shipyard 下载依赖镜像 dockerpullre etcddockerpu docker proxydockerp shipyard 将原先脚本修改为中文脚本 wgethttps raw githubuse

    2026年3月18日
    2
  • MPI入门

    MPI入门1 什么是 MPIMPI 是一个跨语言的通讯协议 用于编写并行计算机 支持点对点和广播 MPI 是一个信息传递应用程序接口 包括协议和和语义说明 他们指明其如何在各种实现中发挥其特性 MPI 的目标是高性能 大规模性 和可移植性 MPI 在今天仍为高性能计算的主要模型 主要的 MPI 1 模型不包括共享内存概念 MPI 2 只有有限的分布共享内存概念 但是 MPI 程序经常在共享内存的机器上运行 在 MPI 模型周边

    2026年3月19日
    1
  • 结巴分词基础用法

    结巴分词基础用法结巴分词基础

    2025年6月6日
    4
  • JavaScript[容易忽视的错误]:当续行遇到换行,换行符丢失

    JavaScript[容易忽视的错误]:当续行遇到换行,换行符丢失1、引言在JavaScript中,当定义一个大的字符串,特别是有换行情况时,为了看起来整齐、易读,一般使用续行符,例如: varscript=”varchart=anychart.pieChart([\[‘Chocolatepaste’,5],\[‘Whitehoney’,2],\

    2022年5月18日
    50
  • 添加 wappalyzer浏览器插件

    添加 wappalyzer浏览器插件通过 wappalyzer 官网 https www wappalyzer com 方法 1 直接输入网址搜索方法 2 放在浏览器书签栏上 方便随时使用 步骤 1 点击首页的 download 步骤 2 拖动这个按钮到浏览器书签栏以后想看哪个网站的技术框架 点这个书签就好啦

    2026年3月19日
    2

发表回复

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

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