3

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();
        }
    }
}
Alex K
  • 22,315
  • 19
  • 108
  • 236
Manav
  • 259
  • 6
  • 20

2 Answers2

4

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

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • I am coding my own library so cant use what u have suggested but I'll definitely refer to it when stuck. Thanks – Manav Jan 29 '12 at 09:56
1

Perhaps this will help you:

XMPP IM Client

Don't be so quick to dismiss existing libraries. You can learn from them and then implement your own if you desire. You want to reinvent the wheel without looking at a wheel, using other people to describe a wheel to you.

Paul
  • 19,704
  • 14
  • 78
  • 96
  • Thanks for your reply. I am totally open to referring other libraries but I don't want to use one... The one referred to by you seems to be great and I'll definitely go through it now. Meanwhile, do your think anything is wrong in my code? Thanks, Manu – Manav Jan 27 '12 at 20:25
  • The only thing I can think of is that `readLine` expects a line terminated by `LF`, `CR`, or `CRLF`. Try reading characters from your `InputStream` one by one as a start. – Paul Jan 27 '12 at 20:30
  • But this probably ain't the case cause I can't see a reply coming back from the gtalk server in the wireshark.... Please refer to the attached screenshot in my question above. Thanks, – Manav Jan 27 '12 at 20:36
  • I looked at the screenshot and I see a response - but perhaps I misunderstand the screenshot. – Paul Jan 27 '12 at 20:39
  • Paul you were right. The problem was indeed with readLine. Have solved it now. Thanks a lot... – Manav Jan 28 '12 at 01:51
  • Great...glad to hear it! Please accept my answer as correct by clicking the checkmark next to it. Thanks! – Paul Jan 28 '12 at 03:48