50929

Unable to read beyond the end of the stream is caused when use readbinary

Question:

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 ?

Answer1:

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

Recommend

  • Amazon api not deleting my products from inventory
  • WSO2 ESB - REST API - Response Does Not Come
  • WaveOut versus WaveOutEvent
  • Update Merchant Order ID in Amazon Marketplace
  • Error: Illegal wire type for field Message.Field .protobuf.MessageTypeAck.sourceModuleID: 1 (0 expec
  • How to apply the same function over a series of columns with a specific string in their names?
  • Reading comments from .proto files using a Protocol Buffers descriptor object
  • Overloading a method in com visible dll
  • Google directions api web service - show the response on map. Is it possible?
  • Consecutive calls/evaluations in a form?
  • R - Phylogram labels to vector
  • Learn Ruby Hard Way ex. 48
  • JQueryMobile: SelectWidget not showing after second page visit
  • Convert ctime to unicode and unicode to ctime python
  • How to decode route points from the JSON Output Data?
  • Using SQL stored function gives invalid SQL statement
  • Get temperature from NVidia GPU using NVAPI
  • Lync Database - Mobile vs PC users
  • GDB skips over my code!
  • Is it possible to embed the sidebar with Google Maps?
  • Input buffer flush
  • Touch events (touchesMoved) not working on UIViews inside UIScrollView
  • how to calculate distance between two points in google maps?
  • Put percent labels next to legend instead of in the slice
  • HABTM associations in Rails : collecting and counting the categories of a model's children
  • How to handle empty space in url when downloading image from web?
  • Guava how to copy all files from one directory to another
  • How to Save JSON data to SQL server database in C#?
  • Is there a way to do normal logging with EureakLog?
  • PHP buffered output depending on server setting?
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Optimizing database types to compact database (SQLite)
  • How to add date and time under each post in guestbook in google app engine
  • TFS: Get latest causes slow project reloading
  • Running a C# exe file
  • coudnt use logback because of log4j
  • Reading document lines to the user (python)