tags:

views:

124

answers:

4

Hi, 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/"&gt;here&lt;/A&gt;.&lt;/BODY&gt;&lt;/HTML&gt;

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?

+1  A: 

Using google talk from java example

Leniel Macaferi
+2  A: 

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.

Seth
Spark is a web client and Openfire an XMPP server, what you need (for a client) is the Smack API.
Guillaume
@Guillaume - [SparkWeb](http://www.igniterealtime.org/projects/sparkweb/index.jsp) is a web client. [Spark](http://www.igniterealtime.org/projects/spark/index.jsp) is a desktop client.
Seth
Direct link for Smack is here http://www.igniterealtime.org/projects/smack/
Mondain
A: 

Although not directly related, you may need a server to test against and one for which you can see the source. I suggest that you look at what the Vysper guys are doing http://mina.apache.org/vysper/

Mondain
A: 

You have several problems with your code, not counting the stylistic one of not using a DOM before sending (which is a best practice in the XMPP world).

  1. You need to connect to "talk.l.google.com". See the results of "dig +short _xmpp-client._tcp.gmail.com SRV" on the command line to find out what servers to connect to.
  2. In your XML prolog, you're double escaping the single quotes, which will actually send a backslash.
  3. The to attribute in your stream:stream should be "gmail.com", without the port number.

All of that being said, I'll second the other posters with a plea for you to not start another Java client library, but to pitch in on an existing one.

Joe Hildebrand