我想连接我的jabber服务器并阅读服务器答案:
DnsEndPoint host = new DnsEndPoint("talk.google.com", 5222);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
public void start()
{
string message = "<?xml version=1.0?><stream:streamto='gmail.com'xmlns='jabber:client'xmlns:stream='http://etherx.jabber.org/streams'version='1.0'>";
var buffer = Encoding.UTF8.GetBytes(message);
args.RemoteEndPoint = host;
args.Completed += SocketAsyncEventArgs_Completed;
args.SetBuffer(buffer, 0, buffer.Length);
bool completesAsynchronously = socket.ConnectAsync(args);
if (!completesAsynchronously)
{
SocketAsyncEventArgs_Completed(args.ConnectSocket, args);
}
}
private void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
{
if (e.SocketError != SocketError.Success)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show("Error during socket operation: " + e.SocketError));
return;
}
byte[] buffer = e.Buffer;
XDocument temp = ConvertByteArrayToXml(buffer);
}
XDocument ConvertByteArrayToXml(byte[] data)
{
XmlReaderSettings settings = new XmlReaderSettings();
using (MemoryStream stream = new MemoryStream(data))
using (XmlReader reader = XmlReader.Create(stream, settings))
{
return XDocument.Load(reader);
}
}
在返回XDocument.Load(reader)
时,我获得了'1.0' is an unexpected token. The expected token is '"' or '''
。怎么解决呢?
为什么我想要它?因为当我授权自己并更改状态时,它不会影响我的帐户。
答案 0 :(得分:4)
请不要从头开始编写自己的XMPP库,而是选择其中一个existing ones。
您不会成功地将您收到的XML视为文件,这是您Unexpected end of file has occurred
错误的来源。您必须逐步解析XML。
答案 1 :(得分:1)
这(在message
的声明中):
<?xml version=1.0?>
是无效的XML声明。根据例外情况,您需要:
<?xml version="1.0" ?>
请注意,这与Jabber或套接字无关,而与XML有关。注意异常消息和堆栈跟踪很重要,这样您就可以自己诊断这类事情了:您需要能够隔离问题的区域,这样您才能解决这个问题。孤立一点。
(顺便说一句,其余的XML看起来也很破旧。)