Handling Binary Data with Byte Array Properties


When working with binary data, string-based properties and parameters may alter byte values due to encoding or character conversion. To preserve the original data, IPWorks components provide byte-array equivalents for sending and receiving data.

Sending Binary Data

To send binary data, use the methods and properties that accept byte arrays. For example, the TCPClient and TCPServer components provide the SendBytes method for sending raw bytes, and properties that may hold binary data (such as EOL) have byte-array versions whose names end with B (e.g., EOLB).

// Send six bytes of binary data. byte[] data = new byte[] { 127, 128, 129, 130, 131, 132 }; tcpclient.SendBytes(data);

Receiving Binary Data

When receiving data, string parameters (such as Text) may not accurately represent binary content. Instead, use the corresponding byte-array parameter (e.g., TextB) to access the exact byte values.

tcpserver.OnDataIn += (s, e) => { // Use TextB rather than Text to access the raw bytes. byte[] data = e.TextB; foreach (byte b in data) { Console.WriteLine(b); } };

ActiveX (COM) Edition

In the ActiveX edition, event parameters are strings, so the byte-array version of the data is available through a separate property instead. For example, the DataInTextB property of the TCPServer component holds the raw bytes of the data received in the DataIn event, and should be read from within the event.

Private Sub TCPServer1_DataIn(ConnectionId As Integer, Text As String, EOL As Boolean) Dim A() As Byte A = TCPServer1.DataInTextB ' Capture the bytes immediately. For I = LBound(A) To UBound(A) Debug.Print A(I) Next I End Sub

Note that in older versions of IPWorks, the TCPClient and TCPServer components were named IPPort and IPDaemon, respectively, and data was sent using the DataToSend and DataToSendB properties.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.