我正在尝试编写一个xmpp客户端来发送/接收来自gtalk的消息。
在我实际开始实现之前,我考虑开发一个原型,看看我是否能够通过gtalk获取消息。
我编写了以下代码,现在我已经陷入了应该在开始加密连接之前请求谷歌的部分。
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class XmppConnect {
static String initiate_conn="<?xml version=\"1.0\"?>\n\r<stream:stream to=\"google.com\"\n\rversion=\"1.0\"\n\rxmlns=\"jabber:client\"\n\rxmlns:stream=\"http://etherx.jabber.org/streams\">\n";
static String start_tls="<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>";
public static void main(String [] args)
{
try {
Socket connection = new Socket("talk.google.com", 5222);
DataInputStream input = new DataInputStream(connection.getInputStream());
BufferedReader d = new BufferedReader(new InputStreamReader(input));
OutputStream to_server = null;
String responseLine;
to_server = connection.getOutputStream();
to_server.write(initiate_conn.getBytes());
responseLine = d.readLine();
System.out.println("Server: " + responseLine);
to_server.write(start_tls.getBytes());
responseLine = d.readLine();
System.out.println("Server: " + responseLine);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我可以将以下内容发送到谷歌
<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
并在回复中我希望以下
<proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
但我从服务器上得不到任何回报。
来自wireshark的屏幕截图已附上。
请求您的帮助,请不要告诉我使用现有的xmpp库,因为我只是不想这样做。
此致 马努!
更新
找到了解决方案。工作代码如下:
现在适用的代码如下。
现在我将研究TLS实现并在此处回到任何疑问:):)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class XmppConnect {
static String initiate_conn="<stream:stream to=\"gmail.com\" version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">";
static String start_tls="<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>";
public static void main(String [] args)
{
try {
Socket connection = new Socket("talk.google.com", 5222);
DataInputStream input = new DataInputStream(connection.getInputStream());
BufferedReader d = new BufferedReader(new InputStreamReader(input,"UTF-8"));
BufferedWriter to_server = new BufferedWriter(
new OutputStreamWriter(connection.getOutputStream(),"UTF-8")
);
String responseLine="";
to_server.write(initiate_conn);
to_server.flush();
int in;
while(!(responseLine.contains("</stream:features>")))
{
responseLine += (char)d.read();
}
System.out.println("Server: " + responseLine);
to_server.write(start_tls);
to_server.flush();
responseLine="";
while(!(responseLine.contains("<proceed xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>")))
responseLine += (char)d.read();
System.out.println("Server: " + responseLine);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}