1

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

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

  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.

为什么 ?

4

2 回答 2

5

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

  • 将阅读器移出循环,或
  • 根本不使用阅读器(只是与流交谈)

但是,如果你需要在循环中使用 reader,你可以使用new BinaryReader(Stream, Encoding, bool)构造函数重载并传入truefinal 参数来告诉它不要关闭底层的Stream. 默认情况下,假定读者将对Stream; 的生命周期负责。该leaveOpen参数改变了这种行为。

于 2014-06-27T14:40:36.227 回答
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();
            }
        }   
    }
}
于 2014-06-27T14:41:42.783 回答