java stun client_我应该使用Java和哪些Stun库?

Java,我试图编写自己的STUN客户端,但似乎我犯了错误,因此,大部分时间它都被冻结了.所以我想知道什么STUN客户端库可用于Java,开始.

跟进:(在尝试以下的同时,仍然没有涉及NAT /防火墙后面的解决方案)

/* 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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Rust 编程语言来实现一个 STUN 客户端。STUN(Simple Traversal of UDP through NATs)是一种用于网络地址转换(NAT)穿透的协议。 下面是一个简单的示例代码,演示了如何使用 Rust 和 tokio 实现一个基本的 STUN 客户端: ```rust use std::net::{SocketAddr, UdpSocket}; use tokio::runtime::Runtime; fn main() { // 设置 STUN 服务器的地址 let stun_server: SocketAddr = "stun.example.com:3478".parse().unwrap(); // 创建 UDP 套接字 let socket = UdpSocket::bind("0.0.0.0:0").expect("Failed to bind socket"); // 发送 STUN 请求 let mut buf = [0u8; 1024]; let request: [u8; 20] = [ 0x00, 0x01, 0x00, 0x00, 0x21, 0x12, 0xA4, 0x42, // STUN 请求头部 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // STUN 请求数据 0x00, 0x08, 0x00, 0x14, // STUN 请求数据长度 ]; socket .send_to(&request, stun_server) .expect("Failed to send STUN request"); // 接收 STUN 响应 let (recv_len, _) = socket.recv_from(&mut buf).expect("Failed to receive STUN response"); let response = &buf[..recv_len]; // 处理 STUN 响应 // 这里可以根据 STUN 协议解析 response 数据 println!("STUN response: {:?}", response); } ``` 这只是一个简单的示例,实际上你可能需要更多的代码来处理 STUN 协议的各种情况和错误处理。你可以使用其他 Rust 来帮助解析和处理 STUN 响应数据。 请注意,这个示例使用了 tokio 实现异步网络编程。你需要在 `Cargo.toml` 文件中添加相应的依赖,例如: ```toml [dependencies] tokio = { version = "1", features = ["full"] } ``` 希望这个示例能对你有所帮助!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值