【问题标题】:error in connect to xmpp(openfire) server using java使用 java 连接到 xmpp(openfire) 服务器时出错
【发布时间】:2015-02-25 23:03:16
【问题描述】:

我是 XMPP 的新手。一整天,我一直在从 Java 连接到我的 XMPP 服务器(Openfire 版本 3.9.3)。我正在使用 Smack(版本 4.0.7)库。这是简单的代码...

ConnectionConfiguration config =new ConnectionConfiguration("servername",5223);
XMPPTCPConnection connection = new XMPPTCPConnection(config);
    // Connect to the server
    try {
            connection.connect();
        connection.login("username", "password");

        }  catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        } catch (SmackException e) {
            e.printStackTrace();
        } 

但是当我运行此代码时,会显示此错误...

org.jivesoftware.smack.SmackException$NoResponseException
at org.jivesoftware.smack.XMPPConnection.throwConnectionExceptionOrNoResponse(XMPPConnection.java:548)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.throwConnectionExceptionOrNoResponse(XMPPTCPConnection.java:867)
at org.jivesoftware.smack.tcp.PacketReader.startup(PacketReader.java:113)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.initConnection(XMPPTCPConnection.java:482)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:440)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:811)
at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)
at test.third.<init>(third.java:19)
at test.third.main(third.java:34)

可能有一个愚蠢的错误和简单的解决方案。我用谷歌搜索,但不知何故我没有得到正确的解决方案。

【问题讨论】:

    标签: java xmpp openfire smack


    【解决方案1】:
    public  void connectAndLogin( {
    
        connect();
        login();
    
    }
    
    private void connect() {
        /**
         * Set server configuration
         * 
         * connect to server    
         */
        setConfiguration();
        try {
            getConnection().connect();
        } catch (XMPPException e) {
            e.printStackTrace();
            setConnection(null);
        }
        }
    
        private void setConfiguration() {
            ConnectionConfiguration config = new ConnectionConfiguration(Constants.IP);
            SmackConfiguration.setPacketReplyTimeout(Constants.PACKET_TIME_OUT);
            System.out.println(SmackConfiguration.getVersion());
            config.setRosterLoadedAtLogin(false);
            // config.setCompressionEnabled(true);
            config.setVerifyChainEnabled(false);
            config.setReconnectionAllowed(true);
            config.setSASLAuthenticationEnabled(false);
            config.setSecurityMode(SecurityMode.disabled);
            config.setDebuggerEnabled(false);
            connection = new XMPPConnection(config);
    }
    
    private void login() {
        if(getConnection()!=null){
    
    
        String USER_NAME="";
    
        String PASSWORD="";
    
        try {
            getConnection().login(USER_NAME,PASSWORD, Constants.RESOURCE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用端口 5222 而不是 5223。这是旧的 SSL 方式,通常不再使用。

      【讨论】:

        猜你喜欢
        • 2013-10-07
        • 1970-01-01
        • 2012-04-16
        • 1970-01-01
        • 2013-08-27
        • 1970-01-01
        • 2016-04-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多