#include#include int main() { boost::asio::io_service io; // Create a TCP socket boost::asio::ip::tcp::socket socket(io); socket.connect(boost::asio::ip::tcp::endpoint( boost::asio::ip::address::from_string("127.0.0.1"), 8000)); // Create a NetBitStreamWriter and use it to write a message NetBitStreamWriter writer; writer.WriteInt32(42); writer.WriteString("Hello, world!"); // Send the message over the socket boost::asio::write(socket, boost::asio::buffer(writer.GetData(), writer.GetTotalByteLength())); return 0; }
#includeIn the above code, we create a UDP socket and bind it to port 8000. We then wait for a datagram to arrive and read it into a QByteArray. We create a NetBitStreamReader from the QByteArray and use it to read an integer and a string.#include int main() { const int PORT = 8000; // Create a UDP socket and bind it to port 8000 QUdpSocket socket; socket.bind(QHostAddress::LocalHost, PORT); // Wait for a datagram to arrive while (!socket.hasPendingDatagrams()) {} // Read the datagram into a byte array QByteArray data; data.resize(socket.pendingDatagramSize()); socket.readDatagram(data.data(), data.size()); // Create a NetBitStreamReader and use it to read the message NetBitStreamReader reader((uint8_t*)data.data(), data.size()); int value = reader.ReadInt32(); QString message = reader.ReadString(); return 0; }