QTcpSocket 和 QUdpSocket 来实现基于 TCP 和 UDP 的网络通信
在 Qt 中,您可以通过 QTcpSocket 和 QUdpSocket 来实现基于 TCP 和 UDP 的网络通信。以下是如何使用 Qt 实现这两种通信方式的简要示例。
1. TCP 网络通信
TCP 是面向连接的协议,确保数据的可靠传输。下面是一个简单的 TCP 客户端和服务器示例。
TCP 服务器 (使用 QTcpServer 和 QTcpSocket)
#include <QTcpServer>
#include <QTcpSocket>
#include <QCoreApplication>
#include <QTextStream>
#include <QDebug>class TcpServer : public QTcpServer {Q_OBJECT
public:TcpServer() {if (!this->listen(QHostAddress::Any, 1234)) {qDebug() << "Server could not start!";return;}qDebug() << "Server started on port 1234!";}protected:void incomingConnection(qintptr socketDescriptor) override {QTcpSocket *clientConnection = new QTcpSocket(this);if (clientConnection->setSocketDescriptor(socketDescriptor)) {connect(clientConnection, &QTcpSocket::readyRead, this, &TcpServer::onDataReceived);connect(clientConnection, &QTcpSocket::disconnected, clientConnection, &QTcpSocket::deleteLater);qDebug() << "New client connected!";} else {delete clientConnection;}}private slots:void onDataReceived() {QTcpSocket *client = qobject_cast<QTcpSocket *>(sender());if (client) {QByteArray data = client->readAll();qDebug() << "Received data:" << data;client->write("Hello from server!");}}
};int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);TcpServer server;return a.exec();
}
#include "main.moc"
TCP 客户端 (使用 QTcpSocket)
#include <QTcpSocket>
#include <QCoreApplication>
#include <QTextStream>
#include <QDebug>class TcpClient : public QObject {Q_OBJECT
public:TcpClient() {socket.connectToHost("127.0.0.1", 1234);if (socket.waitForConnected()) {qDebug() << "Connected to server!";socket.write("Hello from client!");socket.flush();socket.waitForBytesWritten();socket.waitForReadyRead();qDebug() << "Received from server:" << socket.readAll();} else {qDebug() << "Connection failed!";}}private:QTcpSocket socket;
};int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);TcpClient client;return a.exec();
}#include "main.moc"
2. UDP 网络通信
UDP 是无连接的协议,数据传输不保证可靠性,因此可以用于实时传输,如视频流或游戏通信。
UDP 服务器 (使用 QUdpSocket)
#include <QUdpSocket>
#include <QCoreApplication>
#include <QTextStream>
#include <QDebug>class UdpServer : public QObject {Q_OBJECT
public:UdpServer() {socket.bind(QHostAddress::Any, 1234);connect(&socket, &QUdpSocket::readyRead, this, &UdpServer::onDataReceived);qDebug() << "UDP server started!";}private slots:void onDataReceived() {QByteArray datagram;QHostAddress sender;quint16 senderPort;while (socket.hasPendingDatagrams()) {datagram.resize(int(socket.pendingDatagramSize()));socket.readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);qDebug() << "Received from" << sender.toString() << ":" << senderPort << "Data:" << datagram;}}private:QUdpSocket socket;
};int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);UdpServer server;return a.exec();
}#include "main.moc"
UDP 客户端 (使用 QUdpSocket)
#include <QUdpSocket>
#include <QCoreApplication>
#include <QTextStream>
#include <QDebug>class UdpClient : public QObject {Q_OBJECT
public:UdpClient() {QByteArray datagram = "Hello from UDP client!";socket.writeDatagram(datagram, QHostAddress("127.0.0.1"), 1234);qDebug() << "Datagram sent to server!";}private:QUdpSocket socket;
};int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);UdpClient client;return a.exec();
}#include "main.moc"
关键点:
- TCP 通信使用 QTcpSocket 和 QTcpServer,QTcpServer 负责监听连接,QTcpSocket 用于客户端和服务器之间的数据传输。
- UDP 通信使用 QUdpSocket,它不需要建立连接,适用于快速且实时的数据传输。数据通过 writeDatagram 方法发送,接收使用 readyRead 信号。
常见注意事项: - TCP 适用于需要可靠数据传输的场合,例如文件传输、聊天应用等。
- UDP 更适用于对延迟要求较高、但不需要保证每个数据包都到达的应用,例如视频流、游戏等。