QFile file("example.txt"); if (file.open(QIODevice::ReadOnly)) { QByteArray data = file.readAll(); qDebug() << data; file.close(); }
QTcpSocket socket; socket.connectToHost("localhost", 1234); if (socket.waitForConnected()) { QByteArray data = "Hello, world!"; qint64 bytesWritten = socket.write(data); qDebug() << "Wrote" << bytesWritten << "bytes to socket"; socket.disconnectFromHost(); }This code connects to a socket and writes a message to it. The QTcpSocket class inherits from QIODevice, so it can be used to work with sockets using QIODevice's common interface. The QIODevice class is part of the Qt Core library, which is included with the Qt framework.