How do I send binary data over the network using VB?
All properties of ActiveX controls are Unicode, with 16 bit wide characters, on the other side TCP/IP specifications are for byte streams.
The new version of IPWorks toolkit provides byte-array properties for all data sending properties such as DataToSend, EOL etc. The names of these byte array properties are derived from the base property by appending a 'B' such as DataToSendB, EOLB etc.
An example of use would be:
Dim A(0:5) as Byte 'remember to dim explicitly "0 to len-1"
For i=0 to 5
a(i) = chr(127+i)
next i
IPPort1.DataToSendB = A
A similar problem arises when receiving data. In that case there's ONLY ONE byte-array property called DataIn
Private Sub IPDaemon1_DataIn(ConnectionId As Integer, Text As String, EOL As Boolean)
' the Text parameter contains the received data which, sometimes,
' may have been converted incorrectly -- especially in Unicode environments.
' the DataInB property has always the correct bytes.
Dim A() As Variant
' get the array's value when you first enter the event. -- VERY Important
with IPDaemon
A = IPDaemon1.DataInTextB
'only now you may manipulate it ...
Debug.Print Text
For I = LBound(A) To UBound(A)
Debug.Print A(I)
Next I
End Sub
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.