使用readbinary时导致流不可读

时间:2014-06-27 14:26:14

标签: c# sockets binaryreader binarywriter

我正在尝试通过套接字发送一个值。所以我的项目客户端和服务器中有两个部分。

客户端使用以下代码向服务器发送值:

  NetworkStream networkStream = socketForServer.GetStream();
            System.IO.BinaryWriter binaryWriter =
               new System.IO.BinaryWriter(networkStream);

            //------
            int messageSource = 0;
            int messageDesitination = 0;
            int interlockingId = 0;
            int trackId = 0;
            int trainId = 2;
            int direction = 0;
            int messageType = 0;
            int informationType = 0;
            int dateTime = 0;

foreach (Sensor LeftSensorList in LeftSensor)
                {

                    binaryWriter.Write(messageSource);
                    binaryWriter.Write(messageDesitination);
                    binaryWriter.Write(interlockingId);
                    binaryWriter.Write(trackId);
                    binaryWriter.Write(trainId);
                    binaryWriter.Write(direction);
                    binaryWriter.Write(messageType);
                    binaryWriter.Write(informationType);
                    binaryWriter.Write(dateTime);

                    binaryWriter.Flush();
                    binaryWriter.Close();
                    Thread.Sleep(4000);

                }

在服务器部分,我应该读取二进制值:

static void Listeners()
        {

            Socket socketForClient = tcpListener.AcceptSocket();
            if (socketForClient.Connected)
            {

                NetworkStream networkStream = new NetworkStream(socketForClient);

                while (true)
                {                                
                    List<int> variables = new List<int>();
                    using (var reader = new BinaryReader(networkStream))
                    {

                        int messageSource = reader.ReadInt32();
                        int messageDesitination = reader.ReadInt32();
                        int interlockingId = reader.ReadInt32();
                        int trackId = reader.ReadInt32();
                        int trainId = reader.ReadInt32();
                        int direction = reader.ReadInt32();
                        int messageType =reader.ReadInt32();
                        int informationType = reader.ReadInt32();
                        int dateTime = reader.ReadInt32();
                         reader.Close();

                    }
            }
         }

因此,当客户端发送一个值时,我可以第一次获取服务器部分中的值,当客户端向我的服务器发送另一个值时,我收到此错误:

Stream was not readable.

为什么?

2 个答案:

答案 0 :(得分:5)

您正在关闭循环中的读取器,这将关闭流。最简单的解决方法是:

  • 将阅读器移到
  • 外面,或
  • 根本不使用读者(只是与流说话)

但是,如果你需要在循环中使用阅读器,你可以使用new BinaryReader(Stream, Encoding, bool)构造函数重载并在最后一个参数中传递true告诉它不要关闭基础Stream。默认情况下,假设读者将负责Stream的生命周期; leaveOpen参数会更改此行为。

答案 1 :(得分:2)

这是因为BinaryReader假定它拥有流并在使用它时将其关闭。如果您使用的是4.5,则可以使用此重载http://msdn.microsoft.com/en-us/library/gg712804(v=vs.110).aspx

对你来说应该是这样的。

static void Listeners()
{

    Socket socketForClient = tcpListener.AcceptSocket();
    if (socketForClient.Connected)
    {
        NetworkStream networkStream = new NetworkStream(socketForClient);
        while (true)
        {                                
            List<int> variables = new List<int>();

            //Hint : This Line
            using (var reader = new BinaryReader(networkStream, Encoding.Default, true))
            {
                int messageSource = reader.ReadInt32();
                int messageDesitination = reader.ReadInt32();
                int interlockingId = reader.ReadInt32();
                int trackId = reader.ReadInt32();
                int trainId = reader.ReadInt32();
                int direction = reader.ReadInt32();
                int messageType =reader.ReadInt32();
                int informationType = reader.ReadInt32();
                int dateTime = reader.ReadInt32();
                reader.Close();
            }
        }   
    }
}