开发者

How to connect to gtalk in java?

开发者 https://www.devze.com 2023-01-14 15:10 出处:网络
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 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><开发者_如何学GoHEAD><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?


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.


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/


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.


Why are you writing an XML version before writing the stream stanza? The server is expecting a stream of defined format, and not an XML structure. Remove this line

"out.println("< ? xml version=\\'1.0\\' encoding=\\'utf-8\\' ?>")" 

then it will work for sure.

0

精彩评论

暂无评论...
验证码 换一张
取 消