UWP c#从StreamSocket中读取XML
我试图通过TCP网络套接字( StreamSocket
)与XMPP(Jabber)服务器通信,并使用以下代码来读取服务器发送给我的信息:
StreamSocket tcpSocket;
StreamReader reader;
int BUFFER_SIZE = 4096;
// Connecting to a remote XMPP server ....
reader = new StreamReader(tcpSocket.InputStream.AsStreamForRead());
string result;
while (true)
{
result = "";
while (true)
{
char[] buffer = new char[BUFFER_SIZE];
await reader.ReadAsync(buffer, 0, BUFFER_SIZE);
string data = new string(buffer);
// Detecting if all elements in the buffer array got replaced => there is more to read
if (data.IndexOf(" ") >= 0 || reader.EndOfStream)
{
result + data.Substring(0, data.IndexOf(" "));
break;
}
result += data;
}
Debug.WriteLine(result);
}
我的代码适用于长度<4096字符的字符串,但只要字符串获取的时间超过4096个字符,它就会失败(不会检测到消息结束)。 它会等待,直到它收到一个新的字符串<4096个字符,连接这两个字符串并将它们返回为一个字符串。
有没有办法获得一个字符串的实际长度并连续读取它们?
您已将4096设置为BUFFER_SIZE
并将其设置为StreamReader.ReadAsync
和char阵列中的count参数。 当字符串包含超过4096个字符时,它将失败。
您应该能够获得Stream中的实际长度,我们可以使用Stream.Length
以字节为单位获取流的长度。 Array的最后一个字符是“ 0”。 当你创建char数组时,你应该能够设置Stream.Length加一个char数组。
例如:
StreamSocket socket;
StreamSocket tcpSocket;
StreamReader reader;
reader = new StreamReader(tcpSocket.InputStream.AsStreamForRead());
var BUFFER_SIZE=(int)(tcpSocket.InputStream.AsStreamForRead()).Length;
string result;
while (true)
{
result = "";
while (true)
{
char[] buffer = new char[BUFFER_SIZE+1];
await reader.ReadAsync(buffer, 0, BUFFER_SIZE);
string data = new string(buffer);
if (data.IndexOf(" ") >= 0 || reader.EndOfStream)
{
result = data.Substring(0, data.IndexOf(" "));
break;
}
result += data;
}
Debug.WriteLine(result);
}
如果要从当前位置读取所有字符到流的末尾,可以使用StreamReader.ReadToEnd
或StreamReader.ReadToEndAsync
方法。
我终于想出了读长信息:我不得不使用DataReader
和DataWriter
来代替StreamReader
和StreamWriter
。
/// <summary>
/// How many characters should get read at once max.
/// </summary>
private static readonly int BUFFER_SIZE = 4096;
private StreamSocket socket;
private DataReader dataReader;
private DataWriter dataWriter;
public string readNextString() {
string result = "";
readingCTS = new CancellationTokenSource();
try {
uint readCount = 0;
// Read the first batch:
Task < uint > t = dataReader.LoadAsync(BUFFER_SIZE).AsTask();
t.Wait(readingCTS.Token);
readCount = t.Result;
if (dataReader == null) {
return result;
}
while (dataReader.UnconsumedBufferLength > 0) {
result +=dataReader.ReadString(dataReader.UnconsumedBufferLength);
}
// If there is still data left to read, continue until a timeout occurs or a close got requested:
while (!readingCTS.IsCancellationRequested && readCount >= BUFFER_SIZE) {
t = dataReader.LoadAsync(BUFFER_SIZE).AsTask();
t.Wait(100, readingCTS.Token);
readCount = t.Result;
while (dataReader.UnconsumedBufferLength > 0) {
result += dataReader.ReadString(dataReader.UnconsumedBufferLength);
}
}
}
catch(AggregateException) {}
catch(NullReferenceException) {}
return result;
}
链接地址: http://www.djcxy.com/p/48295.html