使用Java在XMPP中的基本连接

时间:2017-03-11 13:40:43

标签: java sockets xmpp

我正在尝试了解基本的xmpp客户端服务器通信。我已经设置了ejabberd服务器并使用jitsi进行了测试。现在从Java我可以发送第一个流xml并从服务器获得响应。

<?xml version='1.0'?>
    <stream:stream to='192.168.1.18' 
        version='1.0' xmlns='jabber:server' 
    xmlns:stream='http://etherx.jabber.org/streams'>

服务器以

响应
<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='13712777409692765897' from='192.168.1.18' version='1.0' xml:lang='en'><stream:features><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-one.net/en/ejabberd/' ver='GtsjYPujnBb89z2vC8P/arjzmdE='/><register xmlns='http://jabber.org/features/iq-register'/><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><compression xmlns='http://jabber.org/features/compress'><method>zlib</method></compression><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>SCRAM-SHA-1</mechanism><mechanism>PLAIN</mechanism><mechanism>X-OAUTH2</mechanism></mechanisms></stream:features>

根据XMPP编程客户指南https://wiki.xmpp.org/web/Programming_XMPP_Clients,在服务器响应后,客户端必须发送iq进行注册,即

<iq type='get' to='shakespeare.lit' id='auth1'>
          <query xmlns='jabber:iq:auth'>
             <username>bill</username>
          </query>
      </iq>

服务器将以

响应
<iq type='result' id='auth1'>
          <query xmlns='jabber:iq:auth'>
              <username/>
              <password/>
              <digest/>
              <resource/>
          </query>
      </iq>

之后客户端将再次使用用户名和密码发送iq。 现在我尝试做同样的事情但是在第一次响应之后,进一步的服务器响应为空。我的代码如下

public static void Sock() throws IOException, InterruptedException{

    String connect="<?xml version='1.0'?> "
            + "<stream:stream "
            + "to='192.168.1.18' version='1.0' "
            + "xmlns='jabber:server' "
            + "xmlns:stream='http://etherx.jabber.org/streams'> ";

    String msg="<iq type='get' to='192.168.1.18' id='auth1'> "
            + "<query xmlns='jabber:iq:auth'> "
            + "<username>junaid</username> "
            + "</query> "
            + "</iq>";

        Socket socket = null; 
        try
            {
                String host = "192.168.1.18";
                int port = 5222;
                InetAddress address = InetAddress.getByName(host);
                 socket = new Socket(address, port);
                 socket.setKeepAlive(true);
                //Send the message to the server
                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);
                bw.write(connect);
                bw.flush();
                System.out.println("Message sent to the server : "+connect);

                //Get the return message from the server
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String message = br.readLine();
                System.out.println("Message received from the server : " +message);
                System.out.println(socket.isConnected());
                ///////Message
                bw.write(msg);
                bw.flush();
                System.out.println("Message sent to the server : "+msg);
                System.out.println(socket.isConnected());
                //Get the return message from the server
                is = socket.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                message = br.readLine();
                System.out.println("Message received from the server : " +message);

            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }
            finally
            {
                //Closing the socket
                try
                {
                    socket.close();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }

    }

我是socket编程的新手,所以如果有人能指导我做错了什么,我会感激不尽。 上述功能的输出是

 Message sent to the server : <?xml version='1.0'?>
    <stream:stream to='192.168.1.18' 
        version='1.0' xmlns='jabber:server' 
        xmlns:stream='http://etherx.jabber.org/streams'>
Message received from the server : <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='13712777409692765897' from='192.168.1.18' version='1.0' xml:lang='en'><stream:features><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-one.net/en/ejabberd/' ver='GtsjYPujnBb89z2vC8P/arjzmdE='/><register xmlns='http://jabber.org/features/iq-register'/><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><compression xmlns='http://jabber.org/features/compress'><method>zlib</method></compression><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>SCRAM-SHA-1</mechanism><mechanism>PLAIN</mechanism><mechanism>X-OAUTH2</mechanism></mechanisms></stream:features>
true
Message sent to the server : <iq type='get' to='192.168.1.18' id='auth1'>
    <query xmlns='jabber:iq:auth'>
        <username>junaid</username>
    </query>
</iq>

true
Message received from the server : null

1 个答案:

答案 0 :(得分:0)

请不要重新发明轮子 - 只需使用已经写得很好的XMPP框架。我可以推荐babbler库 - 它会为您提供漂亮而干净的API,您无需手动构建或解析XML