Universal wrapper class, to establish connection and data to send/receive by protocols:
UNIX-sockets, HTTP/HTTPS, TCP/UDP, Websockets.
- All code of the
QUnSocketclass is implemented in one tinyc++header file. - To create a connection to a server, for any protocol other than
UNIX-sockets,connectToServer()method accepts a string in the format:protocol://location:portwhere, the attributeportis not specified if a standard port (80 / 443) is used. For aUNIX-socketsconnection, a string is given in the format:unix:/location - All protocols have a
binary channelfor sending/receiving data. - The WebSockets protocol has an additional
text channelfor sending/receiving data. - Actions for establishing a connection, sending and receiving data, and error handling
for all protocols are
identical. - All protocols have the ability to receive data in three ways:
Synchronous(always buffered)Asynchronous bufferedAsynchronous non-buffered
- The
WebSocketsprotocol, in addition to the methods described above for receivingbinarydata, has also methods for receivingtextdata. - Only the
networkslibrary (QT+=network) is mandatory for including to the Qt-project. If the project will not use sockets of typeWebSocketsthen the librarywebsocketslibrary doesnot needto be included in the project. - Compatible
Qt5/Qt6
+-------------+----------------------------+
| SYNC | ASYNC |
+-------------+-------------+--------------+
| Buffered | non-Buffered |
+--------+-------------+-------------+--------------+
| All | | | |
|sockets | S | AB | ANB |
+--------+-------------+-------------+--------------+
| Web | | | |
|sockets | S' | AB' | ANB' |
+--------+------------------------------------------+
/* pattern S (Synchronous) */
...
socket.waitForReadyRead(...);
... =socket.readAll();
...
/* pattern AB (Asynchronous Buffered) */
connect(&socket, &QUnSocket::readyRead, this, [&] () {
... =socket.readAll();
});
/* pattern ANB (Asynchronous Non-Buffered) */
connect(&socket, &QUnSocket::received, this, [&] (const QByteArray &data) {
... =data;
});
/// Receiving text data (WebSockets only) ///
/* pattern S' (Synchronous) */
...
socket.waitForReadyRead(...);
... =socket.readAllText();
...
/* pattern AB' (Asynchronous Buffered) */
connect(&socket, &QUnSocket::readyRead, this, [&] () {
... =socket.readAllText();
});
/* pattern ANB' (Asynchronous Non-Buffered) */
connect(&socket, &QUnSocket::receivedText, this, [&] (const QString &text) {
... =text;
});Examples of using QUnSocket for all types of sockets can be found in the test project
test-socket.