C#/ TCP/IP write int

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ToneyBoney
    New Member
    • Jan 2009
    • 4

    C#/ TCP/IP write int

    Hello all,

    I'm kinda new to the world of C#, so please bear with me. I am trying to write a small application that will write an int to server and then get a reply in the form of a double.

    The following is my code to write to the server:

    [code=c#]
    TcpClient bob = new TcpClient("loca lhost", 61734);
    NetworkStream bill = bob.GetStream() ;
    BinaryWriter binWriter = new BinaryWriter(bi ll);

    Int32 num = 987;
    binWriter.Write (num);
    binWriter.Close ();
    [/code]

    However when I read the value in at the server it comes out as -620560384 not 987.

    So my question is:
    Is this the correct way to write an int over C# TCP/IP sockets?

    Also the server code is written in java, does C# do anything weird to bytes before it send them?

    Cheers,
    Tony.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You might want to check the byte order that gets sent out.
    987 in one endian might be 620560384 in the other endian.

    Comment

    • ToneyBoney
      New Member
      • Jan 2009
      • 4

      #3
      Hey Plater,

      Yeah the order in which bytes were sent was the problem. Microsoft being a pack of bastards send all primitive data types in little-endian format whereas my java server (and the rest of the internet) use big-endian.

      The following code forces C# sockets to send primitive data types in big-endian format.

      Code:
      Socket socket = new Socket()
      NetworkStream stream = new NetworkStream(socket)
      BinaryWriter writer = new BinaryWriter(stream)
      {
          int myValue = 42;
          writer.Write(IPAddress.HostToNetworkOrder(myValue));
      }
      edit by mod:
      Please use [CODE] tags, not [B] tags for code
      --insertAlias


      Also as a side note if you want to read in a value in big-endian format, you can use

      IPAddress.Netwo rkToHostOrder()

      This method doesn't let you read in some primitive data types such as double. So to convert this to big-endian format just use a for loop to assign byte[0] to byte[length -1] and so on and so on. (The byte.Reverse() didn't work for me so I had to do it this way).

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Yeah, I figured there had to be a htons function in .NET but wasn't sure of it. I've always manually populated my byte[] for data transfer over sockets, ran in to so many endian issues that I just just did it by hand.

        Comment

        • ToneyBoney
          New Member
          • Jan 2009
          • 4

          #5
          Ha ha, yeah man I know what you mean. It's almost like microsoft don't want their **** to interop with other systems.

          Anyway thanks for the help.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Please keep the language clean, ToneyBoney.

            MODERATOR

            Comment

            • ToneyBoney
              New Member
              • Jan 2009
              • 4

              #7
              Yes Mother......... .........

              Comment

              • pvecchio
                New Member
                • Jan 2010
                • 1

                #8
                So Plater, by doing it manually, do you mean something like this?

                byte[] msgPrefix = new byte[8];

                msgPrefix[0] = 0;
                msgPrefix[1] = 0;
                msgPrefix[2] = 0;
                msgPrefix[3] = 0x44;
                msgPrefix[4] = 0;
                msgPrefix[5] = 0x3C;
                msgPrefix[6] = 0;
                msgPrefix[7] = 0;

                Or do I still need to use some sort of code with this:
                IPAddress.HostT oNetworkOrder ?

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Well I made little functions that handle it, but roughly yes

                  Comment

                  Working...