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 :

           System.IO.BinaryWriter binaryWriter =
           new System.IO.BinaryWriter(networkStream);
           binaryWriter.Write(1);
           binaryWriter.Write(2);
           binaryWriter.Flush();

So in other part i need to read the two values i mean 1 and 2;

So in server part i have this code :

  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))
                {
                    for (int i = 0; i < 2; i++)
                    {
                        int t = reader.ReadInt32();
                        variables.Add(t);
                    }
                }

      }
   }
}

As you can see i hold the values in variables list .but it doesn't work .i mean in server part i can't get the values 1 and 2 and my values is like this :841757955

best regards.

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

2 Answers2

2

and my values is like this :841757955

Always worth sticking that number in the Windows calculator and convert that to hex. You get 0x322C3503.

Which looks a lot like ASCII, a string with 3 characters that encodes "5,2". In other words, your real code doesn't resemble your snippet at all, you don't actually use the BinaryWriter.Write(Int32) overload, you used BinaryWriter.Write(String).

Sure, that can't work, you can't write a string and expect it to be readable as raw integers. Fix your code.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • My project is a part of another project ,so i have to adaptive my project with other project – Ehsan Akbar Jun 27 '14 at 10:33
  • 1
    This is rather a drastic failure to communicate. You not understanding this answer at all. And worse, you not talking to the programmer that worked on the "other project". Very hard to talk across a network if you don't know how to communicate. I you wrote the "server" then you'll have to use BinaryReader.ReadString() – Hans Passant Jun 27 '14 at 10:34
  • You know they give us a structure of their message that they send to our application and here you can see :http://stackoverflow.com/questions/24349052/read-variables-of-a-message-that-is-sent-by-a-server-using-socket – Ehsan Akbar Jun 27 '14 at 10:36
  • the picture is the structure of their message – Ehsan Akbar Jun 27 '14 at 10:37
  • 4
    Oh Lord, this involves trains? You know you can kill a hundred people by getting this wrong? Talk to your supervisor, you're in over your head. – Hans Passant Jun 27 '14 at 10:38
  • :).You know it just a monitoring system .our project just gets the location of the trains and calculates the arrivaltime of the trains and show the time to the passenger by LCD monitor – Ehsan Akbar Jun 27 '14 at 10:40
1

As far as I can tell from your code, you are sending data as a string in binary format, this will yield bytes for the characters 1,2.

When you read the data back you try to get Int32 values.

There are two options here:

Read and write data as a string.

 Client code:

 binaryWriter.Write("1,2");

 Server code:

 string text = binaryReader.ReadString(); // "1,2"

OR Read and write data as integers.

Client code:

binaryWriter.Write(10);
binaryWriter.Write(20);

Server code:

int value1 = binaryReader.ReadInt32(); //10
int value2 = binaryReader.ReadInt32(); //20
Bas
  • 26,772
  • 8
  • 53
  • 86