首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Stun和ice4j接收公共IP和端口

如何使用Stun和ice4j接收公共IP和端口
EN

Stack Overflow用户
提问于 2016-04-24 21:25:25
回答 1查看 2.4K关注 0票数 1

我会尽量简短的。

我希望在没有经过服务器的情况下,在两个java应用程序之间创建通信(这些应用程序稍后将被传输到android)。因此,我花了几周的时间环顾四周,在做了大量的工作之后,我发现了眩晕和ice4j。关于如何使用ice4j的最佳解释--我找到了这里,它几乎向我展示了如何通过以下代码向代理添加眩晕服务器(我并不真正知道代理是什么,只是它管理与眩晕和转向的通信):

代码语言:javascript
运行
复制
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 (我在github上找到的ice4j源代码中的一个类)从代理接收SDP信息。然而,我从ice4j.jar获得了中央maven存储库,并将它添加到netbeans的常规项目中(我这样做是因为我对maven不太熟悉,只是想在我的常规项目上建立一个常规库)。这个jar库没有SDPUtils类,而且由于我对这段代码不太了解,无法亲自修复它,我想知道你们中是否有人能帮我修复上面的代码,或者给我举一个例子来回答标题上的问题。

但是,除非您可以按照我在最后一句中说的那样做,或者向我指出一些示例代码,否则您的帮助很可能是没有用的,因为我无法完全理解这背后的理论,因为我不知道许多概念。

我得到本周末才能弄清楚,如果我不知道,我就完蛋了。所以,如果你能或者认识一个能帮忙的人,我会非常感激的。

(谢谢你阅读到目前为止,并试图帮助:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-27 19:07:33

这就对了

SdpUtils.java

事实上,我也在从事与我的大学项目相同的工作。从上周开始,我正在挖掘p2p连接在nat上建立的网络。 我知道你从哪里得到的代码片段,我想告诉你,这段代码中有错误,这是我纠正的错误。

代码语言:javascript
运行
复制
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
}

}

这段代码要求安装server,而ice4j测试中的一段代码是在说其他一些东西--只需查看一下Ice.java

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36829060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档