前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >stun client java实现_STUN Client

stun client java实现_STUN Client

作者头像
全栈程序员站长
发布2022-09-09 10:59:42
8020
发布2022-09-09 10:59:42
举报
文章被收录于专栏:全栈程序员必看

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

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
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.png
74fd2798202fde65c452efa4582b9818.png

Collapse|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.png
74fd2798202fde65c452efa4582b9818.png

Collapse|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.cn/160521.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
NAT 网关
NAT 网关(NAT Gateway)提供 IP 地址转换服务,为腾讯云内资源提供高性能的 Internet 访问服务。通过 NAT 网关,在腾讯云上的资源可以更安全的访问 Internet,保护私有网络信息不直接暴露公网;您也可以通过 NAT 网关实现海量的公网访问,最大支持1000万以上的并发连接数;NAT 网关还支持 IP 级流量管控,可实时查看流量数据,帮助您快速定位异常流量,排查网络故障。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档