Qt实现TCP同步与异步读写消息的示例代码
目录
- 一、异步读写
- TCP 服务器端示例(异步)
- TCP 客户端端示例(异步)
- 二、同步读写
- TCP 服务器端示例(同步)
- TCP 客户端端示例(同步)
一、异步读写
在 Qt 中实现 TCP 客户端和服务器的同步和异步读写消息涉及使用 QTcpSocket 和 QTcpServer 类。这两个类提供了用于建立 TCP 连接、发送和接收数据的功能。下面是一个简单的示例,演示了如何在 Qt 中实现 TCP 客户端和服务器的同步和异步读写消息:
TCP 服务器端示例(异步)
#include <QCoreApplication> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpServer server; server.listen(QHostAddress::Any, 1234); // 监听端口 1234 QObject::connect(&server, &QTcpServer::newConnection, [&](){ QTcpSocket* cliepxUOCqntSocket = server.nextPendingConnection(); QObject::connect(clientSocket, &QTcpSocket::readyRead, [&](){ QByteArray requestData = clientSocket->readAll(); qDebug() << "Received request from client:" << requestData; // Echo back the received data clientSocket->write(requestData); }); }); qDebug() << "TCP server started. Listening on port 1234..."; return a.exec(); }
TCP 客户端端示例(异步)
#include <QCoreApplication> #include <QTcpSocket> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpSocket client; cli编程客栈ent.connectToHost("127.0.0.1", 1234); // 连接到服务器的 IP 地址和端口 QObject::connect(&client, &QTcpSocket::connected, [&](){ qDebug() << "Connected to server."; // Send a message to the server client.write("Hello, Server!"); }); QObject::connect(&client, &QTcpSocket::readyRead, [&](){ QByteArray responseData = client.readAll(); qDebug() << "Received response from server:" << responseData; }); return a.exec(); }
示例简单演示了如何在 Qt 中实现 TCP 客户端和服务器的异步读写消息。在异步通信中,使用信号和槽机制来处理数据的接收和发送。
二、同步读写
以下是一个简单的示例,演示了如何在 Qt 中实现 TCP 客户端和服务器的同步读写消息:
TCP 服务器端示例(同步)
#include <QCoreApplication> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpServer server; server.listen(QHostAddress::Any, 1234); // 监听端口 1234 if (!sewww.devze.comrver.isListening()) { qDebug() << "Failed to start TCP server."; return 1; } QTcpSocket* clientSocket = nullptr; while (true) { if (server.hASPendingConnections()) { clientSocket = server.nextPendingConnection(); break; } } if (cl编程客栈ientSocket) { qDebug() << "Client connected."; // Read data from client QByteArpxUOCqray requestData = clientSocket->readAll(); qDebug() << "Received request from client:" << requestData; // Echo back the received data clientSocket->write(requestData); } return a.exec(); }
TCP 客户端端示例(同步)
#include <QCoreApplication> #include <QTcpSocket> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpSocket client; client.connectToHost("127.0.0.1", 1234); // 连接到服务器的 IP 地址和端口 if (!client.waitForConnected()) { qDebug() << "Failed to connect to server."; return 1; } qDebug() << "Connected to server."; // Send a message to the server client.write("Hello, Server!"); if (!client.waitForBytesWritten()) { qDebug() << "Failed to write data to server."; return 1; } if (!client.waitForReadyRead()) { qDebug() << "Failed to read response from server."; return 1; } // Read response from server QByteArray responseData = client.readAll(); qDebug() << "Received response from server:" << responseData; return a.exec(); }
示例简单演示了如何在 Qt 中实现 TCP 客户端和服务器的同步读写消息。在同步通信中,使用 waitForConnected、waitForBytesWritten 和 waitForReadyRead 等方法来等待连接建立、数据写入和数据读取完成。这种方式适用于需要确保数据传输完整性和顺序性的场景。
到此这篇关于Qt实现TCP同步与异步读写消息的示例代码的文章就介绍到这了,更多相关Qt TCP同步与异步读写消息内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论