简体   繁体   English

C#字符串长度错误

[英]C# string length is wrong

I want to read data from socket.我想从套接字读取数据。

The value of the result is the same as the data which socket send.结果的值与套接字发送的数据相同。

but I print debug info,the result length is very large,not string length which I see.但是我打印调试信息,结果长度非常大,而不是我看到的字符串长度。

The length is the same as mySockeet.SendBufferSize.长度与 mySocket.SendBufferSize 相同。

How to change the code to make the length of result variable is correct.如何更改代码使结果变量的长度是正确的。

public string readSocket() {
    String result = "";
    if(theStream == null)
       return "";
    if (theStream.DataAvailable) {
      Byte[] inStream = new Byte[mySocket.SendBufferSize];
      theStream.Read(inStream, 0, inStream.Length);
      result = System.Text.Encoding.UTF8.GetString(inStream);
      Debug.Log(result.Length + " "+ mySocket.SendBufferSize);
    }
    return result;
}

I already fix that Just change to this below我已经解决了只需在下面更改为

int length = theStream.Read(inStream, 0, inStream.Length);
result = System.Text.Encoding.UTF8.GetString(inStream, 0, length);

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

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