2016-04-24 3 views
0

Я постараюсь быть кратким.Как получить публичный IP и порт с помощью Stun и ice4j

Я хочу создать связь между двумя java-приложениями (которые позже будут переданы на андроид) без прохождения через сервер. Таким образом, я провел недели, оглядываясь по сторонам, и после многих работ я нашел оглушение и 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 от агента. Однако я получил ice4j.jar от the central maven repository и добавил его в свой обычный проект netbeans (я сделал это, потому что я не очень хорошо знаком с maven и просто хотел иметь регулярную библиотеку в своем обычном проекте). В этой библиотеке jar нет класса SDPUtils, и поскольку я действительно недостаточно понимаю этот код, чтобы исправить его сам, мне было интересно, может ли кто-нибудь из вас помочь мне исправить код выше или показать мне пример того, как чтобы ответить на вопрос о названии.

Однако, если вы не можете сделать то, что я сказал в последнем предложении, или указать мне пример кода, ваша помощь, скорее всего, не будет полезна, поскольку я мысленно не способен понять теорию, лежащую в основе этого полностью из-за многие понятия я не знаю.

У меня есть до конца этой недели, чтобы понять это, и если я этого не сделаю, я довольно ввернута. Поэтому, пожалуйста, если вы можете или знаете кого-то, кто может помочь, я бы очень этому это оценил.

Спасибо за чтение его до сих пор и пытается помочь :)

+0

Спасибо за ребяческие достижения ребята ... – RaKXeR

ответ

4

Там вы идете
SdpUtils.java

На самом деле я также работаю над такой же, как мой проект университета. С прошлой недели я копаю веб-сайт для установления соединения p2p над nat.
Я знаю, что форма, где вы получили выше код snipet, я хотел бы сообщить вам, что есть ошибки в этом коде Вот это один, что я исправил

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) –

 Смежные вопросы

  • Нет связанных вопросов^_^