How Do I Send Binary Data Over the Network Using C++?


In the C++ edition, binary data is sent just like any other data. Methods that send binary data, such as the SendBytes method of the TCPClient component, take two arguments: a pointer to the data, and the length of the data in bytes. Since the length is specified explicitly, the data may contain any byte values, including null bytes.

TCPClient tcpclient; int ret = tcpclient.ConnectTo("server.example.com", 7777); if (ret == 0) { // Send five bytes of binary data, including a null byte. const char data[] = { 0x00, 0x01, 0x7F, (char)0x80, (char)0xFF }; ret = tcpclient.SendBytes(data, sizeof(data)); } if (ret != 0) { printf("Error %d: %s\n", ret, tcpclient.GetLastError()); }

Note that in older versions of IPWorks, data was sent using the SetDataToSend method, which took the same two arguments.

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