2016-04-24 39 views
0

我会尽量简短。如何使用Stun和ice4j接收公共IP和端口

我希望在不通过服务器的情况下创建2个java应用程序之间的通信(稍后将被传输到android)。因此,我花了几周的时间四处寻找,经过很多工作后,我发现了stun和ice4j。关于如何使用ice4j的最佳解释,我发现了here,它向我展示了我需要做什么来将代理服务器添加到代理中(我真的不知道代理是什么,只是它管理我与STUN的通信和TURN),通过这样的代码:

import java.io.IOException; 
import java.net.InetAddress; 
import java.net.UnknownHostException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import org.ice4j.Transport; 
import org.ice4j.TransportAddress; 
import org.ice4j.ice.Agent; 
import org.ice4j.ice.IceMediaStream; 
import org.ice4j.ice.harvest.StunCandidateHarvester; 

public class ice4jTesting { 

    public static void main(String[] args) { 

     Agent agent = new Agent(); 
     String[] hostnames = new String[] {"jitsi.org", "numb.viagenie.ca", "stun.ekiga.net"}; 

     for(String hostname: hostnames) { 
      try { 
       TransportAddress address; 

       address = new TransportAddress(InetAddress.getByName(hostname), 3478, Transport.UDP); 
       agent.addCandidateHarvester(new StunCandidateHarvester(address)); 
      } catch (UnknownHostException ex) { 
       Logger.getLogger(SimpleStun.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 

     IceMediaStream stream = agent.createMediaStream("audio"); 
     int port = 5000; 
     try { 
      agent.createComponent(stream, Transport.UDP, port, port, port+100); 
      // The three last arguments are: preferredPort, minPort, maxPort 
     } catch (IllegalArgumentException | IOException ex) { 
      Logger.getLogger(SimpleStun.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

然而,这教程利用SDPUtils后,一类是在ice4j的源代码我上github发现,才能收到来自代理的SDP信息。然而,我从the central maven repository得到了ice4j.jar,并将它添加到了我的netbeans常规项目中(我这样做是因为我对maven不是很熟悉,并且只想在我的常规项目上定期创建一个库)。这个jar库没有SDPUtils类,并且由于我不太了解这些代码来自己修复它,所以我想知道是否有人能够帮我修正上面的代码,或者向我展示如何回答关于标题的问题。但是,除非你可以按照我在最后一句中所说的去做,或者指向我一些示例代码,否则你的帮助很可能不会有用,因为我完全没有理由理解这背后的理论,因为我不知道的许多概念。

我一直到本周末才弄明白这一点,如果我不这样做,我会很烦。所以,请,如果你能或知道有人可以帮助,我会非常感激。

感谢您阅读它,到目前为止,并试图帮助:)

+0

感谢您的风滚草成就球员... – RaKXeR

回答

4

你去那里
SdpUtils.java

其实我也正在同我的大学项目。从上周我挖掘网络为p2p连接建立over nat。
我知道,表单,您一小段代码片段上面的话,我想告诉你,有在该代码这里的错误是,我纠正了一个

import java.io.IOException; 
import java.net.BindException; 
import java.net.InetAddress; 
import org.ice4j.Transport; 
import org.ice4j.TransportAddress; 
import org.ice4j.ice.Agent; 
import org.ice4j.ice.IceMediaStream; 
import org.ice4j.ice.harvest.StunCandidateHarvester; 

public class IceTest { 

public static void main(String[] args) throws Exception { 
    Agent agent = new Agent(); // A simple ICE Agent 

    /*** Setup the STUN servers: ***/ 
    String[] hostnames = new String[] { "jitsi.org", "numb.viagenie.ca", "stun.ekiga.net" }; 
    // Look online for actively working public STUN Servers. You can find 
    // free servers. 
    // Now add these URLS as Stun Servers with standard 3478 port for STUN 
    // servrs. 
    for (String hostname : hostnames) { 
     try { 
      // InetAddress qualifies a url to an IP Address, if you have an 
      // error here, make sure the url is reachable and correct 
      TransportAddress ta = new TransportAddress(InetAddress.getByName(hostname), 3478, Transport.UDP); 
      // Currently Ice4J only supports UDP and will throw an Error 
      // otherwise 
      agent.addCandidateHarvester(new StunCandidateHarvester(ta)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /* 
    * Now you have your Agent setup. The agent will now be able to know its 
    * IP Address and Port once you attempt to connect. You do need to setup 
    * Streams on the Agent to open a flow of information on a specific 
    * port. 
    */ 

    IceMediaStream stream = agent.createMediaStream("audio"); 
    int port = 5000; // Choose any port 
    try { 
     agent.createComponent(stream, Transport.UDP, port, port, port + 100); 
     // The three last arguments are: preferredPort, minPort, maxPort 
    } catch (BindException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalArgumentException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    /* 
    * Now we have our port and we have our stream to allow for information 
    * to flow. The issue is that once we have all the information we need 
    * each computer to get the remote computer's information. Of course how 
    * do you get that information if you can't connect? There might be a 
    * few ways, but the easiest with just ICE4J is to POST the information 
    * to your public sever and retrieve the information. I even use a 
    * simple PHP server I wrote to store and spit out information. 
    */ 
    String toSend = null; 
    try { 
     toSend = SdpUtils.createSDPDescription(agent); 
     // Each computersends this information 
     // This information describes all the possible IP addresses and 
     // ports 
    } catch (Throwable e) { 
     e.printStackTrace(); 
    } 

    /*The String "toSend" should be sent to a server. You need to write a PHP, Java or any server. 
    * It should be able to have this String posted to a database. 
    * Each program checks to see if another program is requesting a call. 
    * If it is, they can both post this "toSend" information and then read eachother's "toSend" SDP string. 
    * After you get this information about the remote computer do the following for ice4j to build the connection:*/ 

    String remoteReceived = ""; // This information was grabbed from the server, and shouldn't be empty. 
    SdpUtils.parseSDP(agent, remoteReceived); // This will add the remote information to the agent. 
    //Hopefully now your Agent is totally setup. Now we need to start the connections: 

    agent.addStateChangeListener(new StateListener()); // We will define this class soon 
    // You need to listen for state change so that once connected you can then use the socket. 
    agent.startConnectivityEstablishment(); // This will do all the work for you to connect 
} 

}

此代码需要设置SIP服务器,并在ice4j测试上说的其他内容只是看看Ice.java

+0

我什至不记得这个线程还在这里。感谢你的回答!我很快就会看到,我一定会给你发一封电子邮件。 – RaKXeR

+0

看一看[UDP Hole Punching](http://stackoverflow.com/questions/27129268/udp-hole-punching-java-example) –