How to connect to gtalk in java?

I am trying to code a small gtalk XMPP client in java. I know that there are many libraries that will help you, but the RFC is so easy to understand that I decided to write a client myself. I know that the gtalk server is talk.google.comDUC222, 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 linking the specified location, but it does not 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?

+5
source share
4 answers

XMPP is not a trivial protocol to implement, and I don’t think you will get very far by manually sending XML strings to the server.

I would recommend exploring some existing source codes.

Spark OpenFire - XMPP- java.

OpenFire ( ), , .

+2
0

, DOM ( ​​ XMPP).

  • "talk.l.google.com". . "dig + short _xmpp-client._tcp.gmail.com SRV" , , .
  • In your XML prolog, you avoid double quotes twice, which will actually send a backslash.
  • Attribute in your stream: The stream should be "gmail.com", without a port number.

All this, I will make other posters not to start another Java client library, but to place it on an existing one.

0
source

Why are you writing an XML version before writing a stanza? The server expects a stream of a specific format, not an XML structure. Delete this line

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

then it will work for sure.

0
source

All Articles