javaxmppgoogle-talk

Implementing an xmpp client in java - stuck in the first step


I am trying to write an xmpp client to send/recieve messages from gtalk.

Before I actually started with the implementation I thought of developing a prototype to see if I am able to to get a message through to gtalk.

I wrote the following code and am now stuck in the part where I am supposed to request google before starting an encrypted connection.

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class XmppConnect {
static String initiate_conn="<?xml version=\"1.0\"?>\n\r<stream:stream to=\"google.com\"\n\rversion=\"1.0\"\n\rxmlns=\"jabber:client\"\n\rxmlns:stream=\"http://etherx.jabber.org/streams\">\n";
static String start_tls="<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>";
public static void main(String [] args)
    {
        try {
            Socket connection = new Socket("talk.google.com", 5222);
            DataInputStream input = new DataInputStream(connection.getInputStream());
            BufferedReader d = new BufferedReader(new InputStreamReader(input));
            OutputStream to_server = null;
            String responseLine;
            to_server = connection.getOutputStream();
            to_server.write(initiate_conn.getBytes());
            responseLine = d.readLine();       
            System.out.println("Server: " + responseLine);
            to_server.write(start_tls.getBytes());
            responseLine = d.readLine();       
            System.out.println("Server: " + responseLine);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

I am able to send the following to google

<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>

and in reply I expect the following

<proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>

But I don't get anything back from the server.

The screenshot from wireshark is as attached.

Request your help and please don't tell me to use already existing xmpp libraries cause I just don't want to.

Regards, Manu!

Screenshot of WireShark

Update

Have found the solution. The working code is as under:

The code which works for now is as under.

Now I will work on the TLS implementation and get back here incase of any doubts :) :)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class XmppConnect {
static String initiate_conn="<stream:stream to=\"gmail.com\" version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">";
static String start_tls="<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>";
public static void main(String [] args)
    {
        try {
            Socket connection = new Socket("talk.google.com", 5222);
            DataInputStream input = new DataInputStream(connection.getInputStream());
            BufferedReader d = new BufferedReader(new InputStreamReader(input,"UTF-8"));
            BufferedWriter to_server = new BufferedWriter(
                    new OutputStreamWriter(connection.getOutputStream(),"UTF-8")
                    );
            String responseLine="";
            to_server.write(initiate_conn);
            to_server.flush();
            int in;
            while(!(responseLine.contains("</stream:features>")))
            {
                responseLine += (char)d.read();   
            }
            System.out.println("Server: " + responseLine);
            to_server.write(start_tls);
            to_server.flush();
            responseLine="";
            while(!(responseLine.contains("<proceed xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>")))
                responseLine += (char)d.read();   
            System.out.println("Server: " + responseLine);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Solution

  • Check the SMACK which provides a nice XMPP stack for Java. It's well documented and easy to use.