读取后流返回随机数

时间:2015-08-13 20:28:57

标签: c# tcp stream tcpclient

我的NetworkStream在第一次读/写后返回完全随机的数字 这是我的代码

String message = "";
Stream Stream = client.GetStream();
byte[] buffer = new byte[4096];
int read = Stream.Read(buffer, 0, 4096);
int index = 0;
while (buffer[index] != 0)
{

    message += ((char)buffer[index]);

    index++;
}
Console.WriteLine(message.Trim());
buffer = System.Text.Encoding.UTF8.GetBytes("Received at:" + DateTime.Now);
Stream.Write(buffer, 0, buffer.Length);
Stream.Flush();
Thread.Sleep(300);
Console.WriteLine("Message received at:" + DateTime.Now);
if (message == "")
{
    Run = false;
    Console.WriteLine("Verbinding sluiten");
    Console.ReadLine();
}

1 个答案:

答案 0 :(得分:0)

我发现了什么问题。我显然试图通过网络流将整个消息历史记录发送给主机,这就是随机数来自的地方,但现在已经修复了。