VB.net to C++ WORD help

G

Guest

I'm sending a message from VB.net (2003) to a C++ app via TCP sockets of
values 1 to 328. The message is a WORD value where I have to manage both
bytes for the WORD. I'm sending and receiving data from the C++ app with no
problem between the values of 1 to 127 and 256 to 328, but the application
receives garabage between 128 and 255. My current test syntax is as follows:

Dim nodeid As Integer = CType(txtMessage.Text, Integer)

Dim nodes(5) As Char
nodes(0) = Chr(7) '<- 1st WORD, Message Type = 7 in low bit
nodes(1) = Chr(0) '<- 1st WORD, high bit is off
nodes(2) = Chr(6) '<- 2nd WORD, Length of message = 6
nodes(3) = Chr(0) '<- 2nd WORD, high bit off

'Here is where I'm having the problem:
'1-127 in the low bit works great. (nodes(4) = 127, nodes(5) = 0 ) =
127
'256 to 328 works great. (nodes(4) = 0, nodes(5) = 1) = 256

'I'm not sure how to use 128 through 255 for this message
nodes(4) = ChrW(nodeid) '3rd WORD, if we exceed 255 need to push
high bit
nodes(5) = Chr(0) '1 if greater than 256
Send(nodes)
Private Sub Send(ByVal sendData() As Char)
Try
Dim writer As New StreamWriter(MyClient.GetStream)
writer.Write(sendData)
writer.Flush()
Catch ex As Exception
Throw ex
End Try

Any help would be greatly appreciated, thanks.
End Sub
 
B

Bart Mermuys

Hi,

Sisnaz said:
I'm sending a message from VB.net (2003) to a C++ app via TCP sockets of
values 1 to 328. The message is a WORD value where I have to manage both
bytes for the WORD. I'm sending and receiving data from the C++ app with
no
problem between the values of 1 to 127 and 256 to 328, but the application
receives garabage between 128 and 255. My current test syntax is as
follows:

When using binary data then don't use Chars, String or StreamWriter, it will
only complicate things. When a StreamWriter uses ASCII encoding it will
convert any chars >127 to a question mark.

MemoryStream ms = new MemoryStream(6);
ms.Write( BitConverter.GetBytes(Convert.ToUint16(7)), 0, 2 )
ms.Write( BitConverter.GetBytes(Convert.ToUint16(6)), 0, 2 )
ms.Write( BitConverter.GetBytes(UInt16.Parse(txtMessage.Text)), 0, 2 )

SendBinary( ms.ToArray() )

Private Sub SendBinary(ByVal Data() As Byte)
MyClient.GetStream().Write(Data,0, Data.Length)
End Sub

You could also use a BinaryWriter instead of using a BitConverter.
Offcourse, endianess on both sides is important too, so if they don't match,
you can wrap the GetBytes in a Array.Reverse.


HTH,
Greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top