Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to gtalk in java?

Tags:

java

sockets

xmpp

i am trying to code a small XMPP gtalk client in java. I know there is a lot of libraries that help you that but the RFC is so easy to understand that i decide to write a client by myself. I know that the gtalk server is talk.google.com:5222 but when i try this small program i get this result :

 HTTP/1.1 302 Found
Location: http://www.google.com/talk/
Content-Type: text/html
Content-Length: 151

<HTML><HEAD><TITLE>302 Moved</TITLE></HEAD><BODY><H1>302 Moved</H1>The document has moved <A HREF="http://www.google.com/talk/">here</A>.</BODY></HTML>

I also tried to connect the location specified but it doesn't work. Here is my code in java :

    package fr.grosdim.myjabber;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocketFactory;

/**
 * Hello world!
 * 
 */
public class App {
    public static void main(String[] args) {
        SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory
                .getDefault();
        try {


            Socket s = new Socket("talk.google.com", 5222);

            PrintWriter out = new PrintWriter(s.getOutputStream());
            out.println("<?xml version=\\'1.0\\' encoding=\\'utf-8\\' ?>");
            out
                    .println("<stream:stream to='talk.google.com:5222' "
                            + "xmlns='jabber:client'"
                            + " xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
            out.flush();

            BufferedReader reader = new BufferedReader(new InputStreamReader(s
                    .getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);

            }

            out.println("</stream>");
            s.close();

        } catch (SSLPeerUnverifiedException e) {
            System.out.println(" Erreur d'auth :" + e.getLocalizedMessage());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }

    }
}

How can i connect to the gtalk server?

like image 770
Dimitri Avatar asked Nov 05 '22 09:11

Dimitri


1 Answers

XMPP isn't a trivial protocol to implement, and I don't think you'll get very far by sending hand-crafted XML strings to the server.

I'd recommend studying some existing source code.

Spark and OpenFire are one example of a nice open source XMPP client and server implementation in java.

You might try getting OpenFire running locally in a debugger (or with verbose logging turned on) so you can get an idea of what it's doing with your packets.

like image 61
Seth Avatar answered Nov 11 '22 03:11

Seth