我正在为一个大学项目编写XMPP客户端,该客户端应该从其他客户端发送和接收消息。我对XMPP及其语法有基本的了解,但似乎无法连接到gtalk服务器。免责声明-我不能使用Smack或任何其他库。
Socket s = new Socket("talk.l.google.com", 5222);
PrintWriter out = new PrintWriter(s.getOutputStream());
out.println("<?xml version='1.0' encoding='utf-8' ?>");
out.println("<stream:stream "
+ "xmlns='jabber:client' "
+" from='example@gmail.com' to='gmail.com' "
+ " xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
out.flush();
当我收到此响应时,我认为连接成功:
<stream:stream from="gmail.com" id="E3A7EFC5647601B3" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client"><stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>X-OAUTH2</mechanism><mechanism>X-GOOGLE-TOKEN</mechanism></mechanisms></stream:features>
如果我尝试发送一些xml数据包(例如,一条消息),显然会得到
<stream:error><not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-streams"/></stream:error>
我这样发送消息:
out.println("<message to='07xcpozn829nd25ivx1gpp3dug@public.talk.google.com'"
+"from='example@gmail.com'>\r\n"
+"<body>Wherefore art thou?</body>\r\n"
+"</message>");
I: <?xml version='1.0'?>
<stream:stream
from='juliet@im.example.com'
to='im.example.com'
version='1.0'
xml:lang='en'
xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'>
R: <?xml version='1.0'?>
<stream:stream
from='im.example.com'
id='++TR84Sm6A3hnt3Q065SnAbbk3Y='
to='juliet@im.example.com'
version='1.0'
xml:lang='en'
xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'>
答案 0 :(得分:0)
您一定在 SASL 协商过程中遗漏了身份验证部分。
中的客户端到服务器示例