0

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 ?

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

1 Answers1

2

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();
Aelphaeis
  • 2,593
  • 3
  • 24
  • 42
  • i changed it but when my client try to connect to the server ,it can't the server shows this error :Press any key to exit from server program---please see the update,i update the post,and i write the whole listener method – Ehsan Akbar Jun 27 '14 at 15:35
  • Based on what I see, it seems as though the problem is that you're using a poor mechanism for what you want. You should redesign and take a look at this http://msdn.microsoft.com/en-us/library/bbx2eya8(v=vs.110).aspx – Aelphaeis Jun 27 '14 at 15:45
  • So i am using a thread in my app ,so what is the different between these two method i mean my method and the msdn method – Ehsan Akbar Jun 27 '14 at 15:49
  • you method is like this .am i right ?http://www.codeproject.com/Articles/1608/Asynchronous-socket-communication – Ehsan Akbar Jun 27 '14 at 15:56
  • @EA yes; however, I want you to try my edit before you do that as I think it would be simpler. – Aelphaeis Jun 27 '14 at 15:57
  • You put close method out of foreach loop ,i try that but same error – Ehsan Akbar Jun 27 '14 at 16:07
  • .this project is a part of train monitoring ,i need some help could you give me some help in chat discussion ? – Ehsan Akbar Jun 27 '14 at 16:11
  • You know my main question is why i should use Async method instead my current method ,what is the problem of my method ?because you said it a poor method – Ehsan Akbar Jun 27 '14 at 16:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56434/discussion-between-e-a-and-aelphaeis). – Ehsan Akbar Jun 27 '14 at 16:17