qt write函数_Qt官方示例TCP客户端/服务器示例

这个示例展示了如何使用Qt进行TCP通信,包括客户端如何连接到服务器并发送数据,以及服务端如何监听和处理来自客户端的数据。在客户端,连接到服务器会触发一系列信号和槽函数交互,直到发送完成。服务端则通过绑定信号槽来处理新连接和接收数据。
摘要由CSDN通过智能技术生成

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

1097686724fec37ec8dbc5551b0f6f39.gif

客户端

  绑定信号槽。

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

  连接到服务器。

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

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

void Dialog::startTransfer()
{
// called when the TCP client connected to the loopback server
bytesToWrite = TotalBytes - int(tcpClient.write(QByteArray(PayloadSize, '@')));
clientStatusLabel->setText(tr("Connected"));
}
void Dialog::updateClientProgress(qint64 numBytes)
{
// called when the TCP client has written some bytes
bytesWritten += int(numBytes);

// only write more if not finished and when the Qt write buffer is below a certain size.
if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4 * PayloadSize) /* 直到if条件不成立后后停止发送 */
bytesToWrite -= tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@'));

clientProgressBar->setMaximum(TotalBytes);
clientProgressBar->setValue(bytesWritten);
clientStatusLabel->setText(tr("Sent %1MB").arg(bytesWritten / (1024 * 1024)));
}

服务端

  绑定信号槽用于新连接:

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

  监听客户端连接。

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

  服务端新连接到来:

void Dialog::acceptConnection()
{
tcpServerConnection = tcpServer.nextPendingConnection();
if (!tcpServerConnection) {
serverStatusLabel->setText(tr("Error: got invalid pending connection!"));
return;
}

connect(tcpServerConnection,
&QIODevice::readyRead,
this,
&Dialog::updateServerProgress); /* 接受客户端数据的槽函数 */
connect(tcpServerConnection,
QOverload<:socketerror>::of(&QAbstractSocket::error),this,
&Dialog::displayError); /* 错误反馈 */
connect(tcpServerConnection,
&QTcpSocket::disconnected,
tcpServerConnection,
&QTcpSocket::deleteLater); /* 断开反馈 */
serverStatusLabel->setText(tr("Accepted connection"));
tcpServer.close();
}

  接收来自客户端的数据:

void Dialog::updateServerProgress()
{
bytesReceived += int(tcpServerConnection->bytesAvailable());
tcpServerConnection->readAll(); /* 读数据 */

serverProgressBar->setMaximum(TotalBytes);
serverProgressBar->setValue(bytesReceived); /* 设置进度条 */
serverStatusLabel->setText(tr("Received %1MB") /* 显示在界面上 */
.arg(bytesReceived / (1024 * 1024)));

if (bytesReceived == TotalBytes) {
tcpServerConnection->close();
startButton->setEnabled(true);
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
}
}

关于更多

  • QtCreator软件可以找到:

5fa038e4d7debea362baafbdae6e387c.png

  • 或在以下Qt安装目录找到:
C:\Qt\{你的Qt版本}\Examples\{你的Qt版本}\network\loopback
  • 相关链接
https://doc.qt.io/qt-5/qtnetwork-loopback-example.html
  • Qt君公众号回复『Qt示例』获取更多内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值