Sending 4 byte message length header

  • Thread starter Thread starter Winston Nimchan
  • Start date Start date
W

Winston Nimchan

Hi:

I'm currently developing a socket application and would like to precede the
data being sent with a 4 byte message length header (bin4). Can anyone help

Regards

Winston
 
Winston,

If you have the socket open, what is to prevent you from calling Send on
the socket before the data that you want to send?
 
My problem is sending the 4 byte message header. Let me explain with an
example...

I have a 90 bytes array with data and am trying to precede this data with
the length (90) converted to hex then to 4 bytes before sending the data so
preceding my 90 bytes will be
h[0] = 0
h[1] = 0
h[2] = 0
h[3] = 5A

Any idea on how I can accomplish this?

thanks

winston


Nicholas Paldino said:
Winston,

If you have the socket open, what is to prevent you from calling Send on
the socket before the data that you want to send?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Winston Nimchan said:
Hi:

I'm currently developing a socket application and would like to precede the
data being sent with a 4 byte message length header (bin4). Can anyone help

Regards

Winston
 
Winston,

You seem to have this solved already. You know how to convert to a
four-byte array. Once you have that, make a call to Send with the four byte
array. Then make another call to Send, passing the actual data.

If you are sending a four byte unsigned integer, you could always call
the static GetBytes method on the BitConverter class, which will return the
four byte array that you are seeking (if you pass it an unsigned integer).
However, you will have to change the order, by calling the static Reverse
method on the Array class.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Winston Nimchan said:
My problem is sending the 4 byte message header. Let me explain with an
example...

I have a 90 bytes array with data and am trying to precede this data with
the length (90) converted to hex then to 4 bytes before sending the data so
preceding my 90 bytes will be
h[0] = 0
h[1] = 0
h[2] = 0
h[3] = 5A

Any idea on how I can accomplish this?

thanks

winston


message news:eiWAc%[email protected]...
Winston,

If you have the socket open, what is to prevent you from calling
Send
on
the socket before the data that you want to send?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Winston Nimchan said:
Hi:

I'm currently developing a socket application and would like to
precede
the
data being sent with a 4 byte message length header (bin4). Can anyone help

Regards

Winston
 
first of all, there is no such thing as converting it to hex. hex is just a
string representation of a bunch of bits. you can't store anything as hex.

there are a number of ways to accomplish what you want.
1. use bitand and shift
2. BitConverter.GetBytes, but you need to reverse the byte array that it
returns
2. unsafe pointer to look at bytes of the int in reverse order

Winston Nimchan said:
My problem is sending the 4 byte message header. Let me explain with an
example...

I have a 90 bytes array with data and am trying to precede this data with
the length (90) converted to hex then to 4 bytes before sending the data so
preceding my 90 bytes will be
h[0] = 0
h[1] = 0
h[2] = 0
h[3] = 5A

Any idea on how I can accomplish this?

thanks

winston


Nicholas Paldino said:
Winston,

If you have the socket open, what is to prevent you from calling Send on
the socket before the data that you want to send?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Winston Nimchan said:
Hi:

I'm currently developing a socket application and would like to precede the
data being sent with a 4 byte message length header (bin4). Can anyone help

Regards

Winston
 
Back
Top