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

Java, i was trying to write my own STUN client, but it seems like i was making mistake and therefore, most of the time it gets freezed. So i would like to know what STUN Client libraries are available for Java, to get started.

Follow up: (in the mean time trying following, still involved no solution for behind NAT/firewall)

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

}

Step 2: Using it

InetSocketAddress test;

test = stun.getPublicAddress(40446);

System.out.println("STUN: " + test.toString());

Step 3: Result

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)

解决方案

There is

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值