使用数组循环读出networkstream怎么就停止不动了?

来源:百度知道 编辑:UC知道 时间:2024/07/08 02:32:48
while ((i = ns.Read(str, 0, str.Length)) != 0)
走了一边只会再次循环到这步就不动了线程就死了

====================

if (!tc.Connected)
{
tc.Connect(IPAddress.Parse("127.0.0.1"), 12311);
}
ns = tc.GetStream();
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();

string data = KID_TextL.Text;
byte[] bytes = converter.GetBytes(data);
//byte[] bytes = EN.GetBytes(data);
ns.Write(bytes, 0, bytes.Length);
byte[] str = new byte[256];
int i;
while ((i = ns.Read(str, 0, str.Length)) != 0)
{
Control.CheckForIllegalCrossThreadCalls = false;
ContentL.Text = converter.GetString(str);
}

要初始化的...

可能是当前位置没有复位:
在while ((i = ns.Read(str, 0, str.Length)) != 0) 之前加上:
ns.Position = 0;
或者ns.Seek(0,SeekOrigin.Begin);

while 循环里没有去改变条件的值,就是死循环。