Java 和我应该使用哪些 Stun 库?
Posted
技术标签:
【中文标题】Java 和我应该使用哪些 Stun 库?【英文标题】:Java and Which Stun libraries i should use? 【发布时间】:2011-08-07 22:34:30 【问题描述】:Java,我试图编写自己的 STUN 客户端,但似乎我犯了错误,因此大多数时候它都被冻结了。所以我想知道哪些 STUN 客户端库可用于 Java,以便开始使用。
跟进:(同时尝试跟进,仍然没有解决 NAT/防火墙背后的问题)
第 1 步:Stun 类:http://babbly.googlecode.com/svn/trunk/src/org/babbly/core/net/InetAddresResolver.java
/* modified */
import java.io.*;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import net.java.stun4j.StunAddress;
import net.java.stun4j.client.SimpleAddressDetector;
import java.util.Random;
public class stun
public static final int MAX_PORT_NUMBER = 65535;
public static final int MIN_PORT_NUMBER = 1024;
private static DatagramSocket socket = null;
private static SimpleAddressDetector detector;
public static synchronized InetAddress resolveInternetInterface(InetAddress dest)
InetAddress networkInterface = null;
if(dest == null)
try
dest = InetAddress.getByName("78.12.2.61");
catch (UnknownHostException e)
if(dest != null)
socket.connect(dest, getRandomPortNumber());
networkInterface = socket.getLocalAddress();
socket.disconnect();
if(networkInterface == null || networkInterface.isAnyLocalAddress())
try
networkInterface = InetAddress.getLocalHost();
catch (Exception ex)
return networkInterface;
public static synchronized InetAddress resolveInternetInterface()
return resolveInternetInterface(null);
public static int getRandomPortNumber()
return new Random().nextInt(MAX_PORT_NUMBER - MIN_PORT_NUMBER);
public static InetSocketAddress getPublicAddress(int localPort)
InetSocketAddress resolvedAddr = null;
String stunAddressStr = "stun.xten.com";
String portStr = "3478";
int stunPort = Integer.parseInt(portStr);
StunAddress stunAddr = new StunAddress(stunAddressStr, stunPort);
detector = new SimpleAddressDetector(stunAddr);
System.out.println("Created a STUN Address detector for the following " + "STUN server: " + stunAddressStr + ":" + stunPort);
detector.start();
System.out.println("STUN server detector started;");
StunAddress mappedAddress = null;
try
mappedAddress = detector.getMappingFor(localPort);
catch (IOException e)
e.printStackTrace();
System.out.println("lala");
detector.shutDown();
if(mappedAddress != null)
System.out.println("stun: no nat detected");
resolvedAddr = mappedAddress.getSocketAddress();
else
System.out.println("sun: nat detected, hitting the ip");
String dstProperty = "78.22.22.61"; // put the ip of the target to hit
InetAddress destination = null;
try
destination = InetAddress.getByName(dstProperty);
catch (UnknownHostException e)
e.printStackTrace();
InetAddress publicHost = resolveInternetInterface(destination);
resolvedAddr = new InetSocketAddress(publicHost, localPort);
return resolvedAddr;
第 2 步:使用它
InetSocketAddress test;
test = stun.getPublicAddress(40446);
System.out.println("STUN: " + test.toString());
第 3 步:结果
Created a STUN Address detector for the following STUN server: stun.xten.com:3478
STUN server detector started;
Apr 18, 2011 7:04:16 PM net.java.stun4j.stack.NetAccessPoint start
INFO: Bound a socket on ap: net.java.stun4j.stack.AccessPoint@0.0.0.0/0.0.0.0:40446 status: running
Apr 18, 2011 7:04:17 PM net.java.stun4j.stack.NetAccessPoint stop
INFO: Closed socket on ap net.java.stun4j.stack.AccessPoint@0.0.0.0/0.0.0.0:40446 status: running
lala
stun: no nat detected
STUN: /78.12.2.61:40446
BUILD SUCCESSFUL (total time: 12 seconds)
【问题讨论】:
【参考方案1】:有
JSTUN:http://jstun.javawi.de/ 眩晕:http://java.net/projects/stun另见:STUN, TURN, ICE library for Java
【讨论】:
这个有一个内置的 JAR 和示例。您提到的链接很少,但是那些有效或没有 JAR 文件,它们是否有任何静态模块或其中大部分是在 SERVER 中构建的而不是简单的 Client 方法?例如:code.google.com/p/babbly/source/browse/trunk/src/org/babbly/… 第一个没有任何示例或文档。第二个几乎是一团糟。第三个链接没有准确答案。 JAR 文件中有什么内容吗?只是一个小的,上面我准备了一个,但代码太多了。 JSTUN 在一个 Jar 文件中,包含客户端和服务器。如果你下载源代码,里面有文档,以演示和单元测试的形式。 你写的是STUN客户端还是STUN服务器?使用 JSTUN,您可以同时测试服务器和客户端。有了它,您可以修复自己的实现。以上是关于Java 和我应该使用哪些 Stun 库?的主要内容,如果未能解决你的问题,请参考以下文章
Webrtc、websockets、Stun/turn 服务器,一起工作?
适用于 iOS iPhone 的 ICE、STUN、TURN 库
如何使用 java 创建我自己的 STUN 或 TURN 服务器
为 Node.js 设置 STUN 服务器时,主 IP 地址和辅助 IP 地址应该是啥?