2

I try to transfer a filedata with sockets via a sslStream. It seems that I must send the file data length before the data. The problem is that the code

Byte [] size = BitConverter.GetBytes(fileData.Length)

Returns in little endian but internet protocols are using big endian.

How I can convert this to big endian and write it to stream;

1

1 Answer 1

2

I assume that you want to send just one int value. The order of the bytes should be reversed before transmitting:

byte[] bytes = BitConverter.GetBytes(fileData.Length);
if (BitConverter.IsLittleEndian)
     Array.Reverse(bytes); 

Check this MSDN page to get more information about BitConverted.

You could also convert it using HostToNetworkOrder, for example:

int reversed = IPAddress.HostToNetworkOrder(fileData.Length);
byte[] bytes = BitConverter.GetBytes(reversed);

To send these bytes use the Write method:

stream.Write(bytes, 0, bytes.Length);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.