简体   繁体   English

NetworkStream.read()读取所有字节,但不会转换为字符串

[英]NetworkStream.read() reads all bytes but won't convert to string

I am trying to read the response of the server when attempting to log on using networkStream.read() using the following code: 我尝试使用以下代码尝试使用networkStream.read()登录时读取服务器的响应:

if (connectionStream.DataAvailable && connectionStream.CanRead)
{
    byte[] myReadBuffer = new byte[64];
    string responseMessage = string.Empty;
    int numberOfBytesRead = 0;
    do
    {
        numberOfBytesRead = connectionStream.Read(myReadBuffer, 0, myReadBuffer.Length);
        responseMessage = Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead);

    } while (connectionStream.DataAvailable);

    Debug.Log("Message:" + responseMessage);

    #breakpoint 
    if (responseMessage.Contains("OK"))
    {
        Debug.Log("logon sucessful");
    }
    else
    {
        Debug.LogError("Logon denied!");
    }
}

By inspecting my local variables at breakpoint i know the Read() is excecuted without problem as numberOfBytesRead is set to 32, and myReadBuffer is filled with 32 bytes (all bytes in myReadBuffer match the bytes sent by the server). 通过在breakpoint处检查我的局部变量,我知道Read()被正确执行,因为numberOfBytesRead设置为32,并且myReadBuffer填充了32个字节( myReadBuffer中的所有字节与服务器发送的字节匹配)。 However after trying to extract the string from myReadbuffer using Encoding.ASCII.GetString() the string is still empty (Visual studio also says it is empty at the breakpoint), even though myReadBuffer isn't. 但是,在尝试使用Encoding.ASCII.GetString()myReadbuffer提取字符串之后,即使myReadBuffer不是,该字符串仍然为空(Visual Studio也说它在断点处为空)。

The bytes in myReadBuffer read: myReadBuffer的字节读取为:

32 0 0 0
1 0 0 0
0 0 0 0
76 79 71 79
78 58 32 48
59 79 75 59
32 83 83 61
54 66 67 0

which translates to: _ _ _ _ _ _ _ _ _ _ LOGON : 0 ; OK ; SS = 5 A 8 _ 转换为: _ _ _ _ _ _ _ _ _ _ LOGON : 0 ; OK ; SS = 5 A 8 _ _ _ _ _ _ _ _ _ _ _ LOGON : 0 ; OK ; SS = 5 A 8 _

Any suggestions as to what can cause this? 关于什么会导致这种情况的任何建议?

The response from the server contains some null ('\\0') characters. 服务器的响应包含一些null ('\\0')字符。 Despite what the docs on Strings (C#) say about null termination in C#: 尽管Strings(C#)上的文档对C#中的空终止都说过:

There is no null-terminating character at the end of a C# string; C#字符串的末尾没有以null结尾的字符; therefore a C# string can contain any number of embedded null characters ('\\0'). 因此,C#字符串可以包含任意数量的嵌入式空字符('\\ 0')。

Unity does not seem to comply to this, and actually does terminate a string after a null character has been encountered. Unity似乎不符合此要求,实际上在遇到null字符后终止字符串。 Though I couldn't find any references to this in the unity docs. 虽然我在统一文档中找不到对此的任何引用。

The fix i ended up going with was replacing the null characters by spaces (Could also remove the null characters completely, but i want to know the characters were there at some point) like so: responseMessage = responseMessage.Replace('\\x0', '\\x0020'); 我最终解决的问题是用空格替换空字符(也可以完全删除空字符,但我想知道字符在某个时候在那里),如下所示: responseMessage = responseMessage.Replace('\\x0', '\\x0020');

While creating this post i figured it out, but coulnd't find any other posts on SO describing my problem. 在创建此帖子时,我已经弄明白了,但是找不到关于SO的其他任何描述我的问题的帖子。 So answering it myself for future references. 因此,请自己回答,以备将来参考。 If anyone has any other/better solutions or additional information i'd still be glad to hear/accept that. 如果有人有其他/更好的解决方案或其他信息,我仍然很高兴听到/接受。

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

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