简体   繁体   English

UWP,如何从StreamSocket读取所有数据

[英]UWP, how to read ALL data from StreamSocket

I'm trying to implement a XMPP client for UWP in C# and to achieve this I'm using StreamSocket for TCP connection. 我正在尝试在C#中为UWP实现XMPP客户端并实现这一点我正在使用StreamSocket进行TCP连接。

The problem is that for handshake and SASL auth I need a sequence of "writing and reading" from the client by the server and without a delay I can't read all data sent by server; 问题是,对于握手和SASL auth我需要服务器从客户端“写入和读取”序列,没有延迟我无法读取服务器发送的所有数据; I want to remove the delay part. 我想删除延迟部分。

This is my code: 这是我的代码:

// Start handshake
await XmppWrite("<?xml version='1.0'?><stream:stream to='" + serverHostName.Domain + "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
var responseStream = await XmppRead();

SaslPlain saslPlain = new SaslPlain(Username, Password);
responseStream = null;
String xml = null;
while (!saslPlain.IsCompleted && (xml = saslPlain.GetNextXml(responseStream)) != null)
{
    await XmppWrite(xml);
    responseStream = await XmppRead();
}
if (!saslPlain.IsSuccess) return;

// Request bind
await XmppWrite("<?xml version='1.0'?><stream:stream to='" + serverHostName.Domain + "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
responseStream = await XmppRead();

// Bind
await XmppWrite("<iq id='" + GenerateId() + "' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>");
responseStream = await XmppRead();
if (responseStream.Jid == null) return;

Jid = responseStream.Jid;

// Open session
await XmppWrite("<iq id='" + GenerateId() + "' type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>");
responseStream = await XmppRead();

The XmppRead() method is this: XmppRead()方法是这样的:

private async Task<XmppStream> XmppRead()
{
    await Task.Delay(100); // Things don't works without this...
    var x = await dataReader.LoadAsync(BUFFER_SIZE);
    if (dataReader.UnconsumedBufferLength > 0)
    {
        Debug.WriteLine(x + ", " + dataReader.UnconsumedBufferLength);
        var stream = new XmppStream(dataReader.ReadString(dataReader.UnconsumedBufferLength));
        if (stream.IsError)
        {
            if (Error != null)
            {
                Error(this, new XmppClientEventArgs(stream));
            }
            throw new Exception(stream.Error);
        }
        return stream;
    }
    return new XmppStream();
}

So, I think the problem is that i try to read before the server has finished to send all its data, so I can read only a partial message. 所以,我认为问题是我尝试在服务器完成之前读取所有数据,因此我只能读取部分消息。 How can i check if there are some other data without block? 如何检查是否有其他数据没有阻止?

Thanks. 谢谢。

(I was searching for something else and saw this question) (我正在寻找别的东西并看到了这个问题)

What happens when you set the DataReader.InputOptions to Partial? 将DataReader.InputOptions设置为Partial时会发生什么? Right now the LoadAsync (AFAICT) won't return it's got all BUFFER_SIZE bytes. 现在,LoadAsync(AFAICT)将不会返回它的所有BUFFER_SIZE字节。 By setting the input stream options to partial, you'll get whatever is available. 通过将输入流选项设置为partial,您将获得可用的任何内容。

DataReader docs are here and InputStreamOptions value are here DataReader文档在这里 ,InputStreamOptions值在这里

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

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