By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

C#/ TCP/IP write int

P: 4
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:

Expand|Select|Wrap|Line Numbers
  1. TcpClient bob = new TcpClient("localhost", 61734); 
  2. NetworkStream bill = bob.GetStream();
  3. BinaryWriter binWriter = new BinaryWriter(bill);
  4.  
  5. Int32 num = 987;
  6. binWriter.Write(num);
  7. binWriter.Close();
  8.  
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.
Jan 26 '09 #1
Share this Question
Share on Google+
8 Replies


Plater
Expert 5K+
P: 7,872
You might want to check the byte order that gets sent out.
987 in one endian might be 620560384 in the other endian.
Jan 26 '09 #2

P: 4
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.

Expand|Select|Wrap|Line Numbers
  1. Socket socket = new Socket()
  2. NetworkStream stream = new NetworkStream(socket)
  3. BinaryWriter writer = new BinaryWriter(stream)
  4. {
  5.     int myValue = 42;
  6.     writer.Write(IPAddress.HostToNetworkOrder(myValue));
  7. }
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.NetworkToHostOrder()

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).
Jan 27 '09 #3

Plater
Expert 5K+
P: 7,872
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.
Jan 27 '09 #4

P: 4
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.
Jan 27 '09 #5

Curtis Rutland
Expert 2.5K+
P: 3,256
Please keep the language clean, ToneyBoney.

MODERATOR
Jan 28 '09 #6

P: 4
Yes Mother..................
Jan 29 '09 #7

P: 1
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.HostToNetworkOrder ?
Jan 27 '10 #8

Plater
Expert 5K+
P: 7,872
Well I made little functions that handle it, but roughly yes
Feb 1 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.