Unable to read beyond the end of the stream is cau

2019-09-08 18:51发布

问题:

I am trying to send a value by socket .So i have two parts in my project Client and server .

The client sends a value to server using this code :

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

In server part i should read the binary values :

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

So when the client send a value i can get the value in server part just for first time ,when a client sends another values to my server i got this error :

Unable to read beyond the end of the stream

Why ?

回答1:

I'd rewrite the listener Method to look something like this. NB : This is untested code.

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

Edit : Upon closer inspection I noticed a few things. Try this:

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