Qt官方示例-TCP客户端/服务器示例

清疚 2023-06-23 14:26 182阅读 0赞

该示例演示了在本地主机上的TCP客户端和服务器是如何通讯的。

demo.gif

客户端

  绑定信号槽。

  1. connect(&tcpClient, &QAbstractSocket::connected, this, &Dialog::startTransfer); /* 连接到服务器时回送消息给服务器 */
  2. connect(&tcpClient, &QIODevice::bytesWritten,
  3. this, &Dialog::updateClientProgress); /* 绑定写数据到服务器的信号槽 */

  连接到服务器。

  1. tcpClient.connectToHost(QHostAddress::LocalHost, tcpServer.serverPort());

  这里比较有意思的是,客户端连接到服务器->客户端(tcpClient)触发startTransfer槽函数->调用tcpClient.write->触发QIODevice::bytesWritten信号->触发updateClientProgress槽函数调用->就一直tcpClient.write,直到if条件不成立后后停止发送。

  1. void Dialog::startTransfer()
  2. {
  3. // called when the TCP client connected to the loopback server
  4. bytesToWrite = TotalBytes - int(tcpClient.write(QByteArray(PayloadSize, '@')));
  5. clientStatusLabel->setText(tr("Connected"));
  6. }
  7. void Dialog::updateClientProgress(qint64 numBytes)
  8. {
  9. // called when the TCP client has written some bytes
  10. bytesWritten += int(numBytes);
  11. // only write more if not finished and when the Qt write buffer is below a certain size.
  12. if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4 * PayloadSize) /* 直到if条件不成立后后停止发送 */
  13. bytesToWrite -= tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@'));
  14. clientProgressBar->setMaximum(TotalBytes);
  15. clientProgressBar->setValue(bytesWritten);
  16. clientStatusLabel->setText(tr("Sent %1MB").arg(bytesWritten / (1024 * 1024)));
  17. }

服务端

  绑定信号槽用于新连接:

  1. connect(&tcpServer, &QTcpServer::newConnection,
  2. this, &Dialog::acceptConnection);

  监听客户端连接。

  1. !tcpServer.isListening() && !tcpServer.listen()

  服务端新连接到来:

  1. void Dialog::acceptConnection()
  2. {
  3. tcpServerConnection = tcpServer.nextPendingConnection();
  4. if (!tcpServerConnection) {
  5. serverStatusLabel->setText(tr("Error: got invalid pending connection!"));
  6. return;
  7. }
  8. connect(tcpServerConnection,
  9. &QIODevice::readyRead,
  10. this,
  11. &Dialog::updateServerProgress); /* 接受客户端数据的槽函数 */
  12. connect(tcpServerConnection,
  13. QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
  14. this,
  15. &Dialog::displayError); /* 错误反馈 */
  16. connect(tcpServerConnection,
  17. &QTcpSocket::disconnected,
  18. tcpServerConnection,
  19. &QTcpSocket::deleteLater); /* 断开反馈 */
  20. serverStatusLabel->setText(tr("Accepted connection"));
  21. tcpServer.close();
  22. }

  接收来自客户端的数据:

  1. void Dialog::updateServerProgress()
  2. {
  3. bytesReceived += int(tcpServerConnection->bytesAvailable());
  4. tcpServerConnection->readAll(); /* 读数据 */
  5. serverProgressBar->setMaximum(TotalBytes);
  6. serverProgressBar->setValue(bytesReceived); /* 设置进度条 */
  7. serverStatusLabel->setText(tr("Received %1MB") /* 显示在界面上 */
  8. .arg(bytesReceived / (1024 * 1024)));
  9. if (bytesReceived == TotalBytes) {
  10. tcpServerConnection->close();
  11. startButton->setEnabled(true);
  12. #ifndef QT_NO_CURSOR
  13. QApplication::restoreOverrideCursor();
  14. #endif
  15. }
  16. }

关于更多

  • QtCreator软件可以找到:

what\_find.png

  • 或在以下Qt安装目录找到:

    C:\Qt{ 你的Qt版本}\Examples{ 你的Qt版本}\network\loopback

  • 相关链接

    https://doc.qt.io/qt-5/qtnetwork-loopback-example.html

  • Qt君公众号回复『Qt示例』获取更多内容。

发表评论

表情:
评论列表 (有 0 条评论,182人围观)

还没有评论,来说两句吧...

相关阅读