0
votes

I am trying to understand basic xmpp client server communication. I have setup ejabberd server and tested with jitsi. Now from Java I am able to send first stream xml and get response from 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'>

And server responds with

<?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>

As per XMPP programming clients guide line https://wiki.xmpp.org/web/Programming_XMPP_Clients, after server response the client has to send iq for registration i.e

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

And server will respond with

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

After which client will again send iq with username and password. Now I am trying to do the same thing but after first response, the further server response is null. My code is as follows

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();
                }
            }

    }

I am new to socket programming so if anyone could guide me what I am doing wrong, I shall be thankful. Output of above function is

 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

1 Answers

0
votes

Please, don't reinvent the wheel - just use already well-written XMPP frameworks. I can recommend babbler library - it will give you nice and clean API and you will no need to manually construct or parse XML