使用readbinary时无法读取超出流末尾的内容

时间:2014-06-27 15:07:32

标签: 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)
            {
                Console.WriteLine("Client:" + socketForClient.RemoteEndPoint + " now connected to server.");
                NetworkStream networkStream = new NetworkStream(socketForClient);


                while (true)
                {

                    TimeTableRepository objTimeTableRepository = new TimeTableRepository();
                    SensorRepository objSensorRepository = new SensorRepository();
                    ArrivalTimeRepository objArrivalTimeRepository=new ArrivalTimeRepository();
                    TrainRepository objTrainRepository = new TrainRepository();




                    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();


                    }


                }

                networkStream.Close();



            }
            socketForClient.Close();
            Console.WriteLine("Press any key to exit from server program");
            Console.ReadKey();
        }

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

无法阅读超出流的结尾

为什么?

1 个答案:

答案 0 :(得分:2)

我会重写侦听器方法,看起来像这样。 注意:这是未经测试的代码。

static void Listeners()
    {

        Socket socketForClient = tcpListener.AcceptSocket();
        if (socketForClient.Connected)
        {
            using (var reader = new BinaryReader(new NetworkStream(socketForClient), Encoding.Default, true))
            {
                while (((int)reader.PeekChar()) != -1)
                {                                 
                    List<int> variables = new List<int>();

                    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();
                 }
            }
     }

编辑: 经过仔细检查,我注意到了一些事情。试试这个:

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();

    Thread.Sleep(4000);
}
//Hint : Changes here
binaryWriter.Close();