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

发布于 2024-11-02 05:46:55 字数 4472 浏览 0 评论 0原文

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: [email protected]/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 [email protected]/0.0.0.0:40446 status:  running
lala
stun: no nat detected
STUN: /78.12.2.61:40446
BUILD SUCCESSFUL (total time: 12 seconds)

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)

Step 1: Stun class: 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;

  }

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: [email protected]/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 [email protected]/0.0.0.0:40446 status:  running
lala
stun: no nat detected
STUN: /78.12.2.61:40446
BUILD SUCCESSFUL (total time: 12 seconds)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文