简体   繁体   English

如何在java中连接到gtalk?

[英]How to connect to gtalk in java?

i am trying to code a small XMPP gtalk client in java. 我试图在java中编写一个小的XMPP gtalk客户端。 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. 我知道有很多库可以帮助你,但RFC很容易理解,我决定自己编写一个客户端。 I know that the gtalk server is talk.google.com:5222 but when i try this small program i get this result : 我知道gtalk服务器是talk.google.com:5222但是当我尝试这个小程序时,我得到了这个结果:

 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 : 这是我在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? 我如何连接到gtalk服务器?

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. XMPP并不是一个很容易实现的协议,我认为通过向服务器发送手工制作的XML字符串,你不会走得太远。

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. Spark和OpenFire是java中一个很好的开源 XMPP客户端和服务器实现的一个例子。

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. 您可以尝试在调试器中本地运行OpenFire(或打开详细日志记录),这样您就可以了解它对数据包执行的操作。

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/ 我建议你看看Vysper的人在做什么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). 您的代码有几个问题,不计算在发送之前不使用DOM的风格(这是XMPP世界中的最佳实践)。

  1. You need to connect to "talk.l.google.com". 您需要连接到“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. 在命令行上查看“dig + short _xmpp-client._tcp.gmail.com SRV”的结果,找出要连接的服务器。
  2. In your XML prolog, you're double escaping the single quotes, which will actually send a backslash. 在您的XML序言中,您将双重转义单引号,实际上会发送反斜杠。
  3. The to attribute in your stream:stream should be "gmail.com", without the port number. 你的流中的to属性:stream应该是“gmail.com”,没有端口号。

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. 所有这一切,我会在其他海报上请求你不要启动另一个Java客户端库,而是投入一个现有的。

Why are you writing an XML version before writing the stream stanza? 为什么在编写流节之前编写XML版本? The server is expecting a stream of defined format, and not an XML structure. 服务器期望定义格式的流,而不是XML结构。 Remove this line 删除此行

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

then it will work for sure. 然后它肯定会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM