🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
本系列所有文章可以在这里查看[http://blog.csdn.net/cloud_castle/article/category/2123873](http://blog.csdn.net/cloud_castle/article/category/2123873) 既然前面都是讲的网络,今天咱们趁热打铁,再来看一个Qt network的例程。 这个例子是Loopback Example(回送网络例程),与之前所有例子都不同的是,在这个例子中是服务器接收客户端数据,不要以为服务器总是发送数据的噢。 照例,看看介绍怎么说,第一时间了解这个例子里面有没我们感兴趣的东西呢 Demonstrates the client-server communication on a local host The example demonstrates how the clients and servers on a local host communicate with each other.一看我们就懂了,噢,这个是讲本地网络(Windows中为127.0.0.1)的~ 那这个本地网络有什么用呢?想一想这个地址都是谁在访问它?对了,本机的各个进程。那么实际中Loopback Address也是经常用在本地机进程间通信的,一旦该进程使用回送地址发送数据,这份数据回立刻被返回,而不进行任何网络传输。当然,测试网络软件的过程中,我们也可以使用这个地址。 这个例子只有3个文件,main.cpp木有亮点,来看dialog.h: dialog.h: ~~~ #include <QDialog> #include <QTcpServer> #include <QTcpSocket> QT_BEGIN_NAMESPACE class QDialogButtonBox; class QLabel; class QProgressBar; class QPushButton; class QTcpServer; class QTcpSocket; class QAction; QT_END_NAMESPACE class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); public slots: void start(); void acceptConnection(); void startTransfer(); void updateServerProgress(); void updateClientProgress(qint64 numBytes); void displayError(QAbstractSocket::SocketError socketError); private: QProgressBar *clientProgressBar; QProgressBar *serverProgressBar; QLabel *clientStatusLabel; QLabel *serverStatusLabel; QPushButton *startButton; QPushButton *quitButton; QDialogButtonBox *buttonBox; QTcpServer tcpServer; // TcpServer是一个允许接收来访的Tcp连接的类 QTcpSocket tcpClient; // QTcpSocket通过QABstractSocket由QIODevice派生,因此它可以使用QDataStream或QTextStream操作 QTcpSocket *tcpServerConnection; // 与Fortune Server不同,这个例子Server端的QTcpSocket对象声明为了全局变量 int bytesToWrite; int bytesWritten; int bytesReceived; }; #endif ~~~ dialog.cpp: ~~~ #include <QtWidgets> #include <QtNetwork> #include "dialog.h" static const int TotalBytes = 50 * 1024 * 1024; // 50 MB static const int PayloadSize = 64 * 1024; // 64 KB Dialog::Dialog(QWidget *parent) : QDialog(parent) { clientProgressBar = new QProgressBar; clientStatusLabel = new QLabel(tr("Client ready")); serverProgressBar = new QProgressBar; serverStatusLabel = new QLabel(tr("Server ready")); startButton = new QPushButton(tr("&Start")); quitButton = new QPushButton(tr("&Quit")); buttonBox = new QDialogButtonBox; buttonBox->addButton(startButton, QDialogButtonBox::ActionRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); connect(startButton, SIGNAL(clicked()), this, SLOT(start())); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(&tcpServer, SIGNAL(newConnection()), // Server三大宝,Listen(),newConnection(),nextPendingConnection() this, SLOT(acceptConnection())); connect(&tcpClient, SIGNAL(connected()), this, SLOT(startTransfer())); // 建立起连接后开始发送数据 connect(&tcpClient, SIGNAL(bytesWritten(qint64)), // bytesWritten(qint 64)是继承自QIODevice的一个信号。注意不是那个全局整型变量bytesWritten。。。但是如果把鼠标放在上面显示的是int Dialog::bytesWritten,并且以红色全局变量的颜色显示。Qt Creator很误导人啊,还是尽量不要这么命名吧。。。参数是触发该信号的写入数据量,这里就是64KB啦 this, SLOT(updateClientProgress(qint64))); connect(&tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError))); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(clientProgressBar); mainLayout->addWidget(clientStatusLabel); mainLayout->addWidget(serverProgressBar); mainLayout->addWidget(serverStatusLabel); mainLayout->addStretch(1); mainLayout->addSpacing(10); mainLayout->addWidget(buttonBox); setLayout(mainLayout); setWindowTitle(tr("Loopback")); } void Dialog::start() { startButton->setEnabled(false); #ifndef QT_NO_CURSOR <span style="font-family: Arial, Helvetica, sans-serif; color: rgb(0, 128, 0);">//</span><span style="font-family: Arial, Helvetica, sans-serif; color: rgb(192, 192, 192);"> </span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="color:#008000;">原来在嵌入式Linux平台下编译经常会加上这个选项,否则屏幕上会出现鼠标。现在Qt5编安卓估计是默认加上了这个选项的</span></span> QApplication::setOverrideCursor(Qt::WaitCursor); // 意即如果没有定义QT_NO_CURSON宏,则将鼠标重载为忙碌样式。也就是说,默认设置的情况下,我们在嵌入式平 台上看不到这个效果,但是在PC机上看的到 #endif bytesWritten = 0; // 已写和已接收的字符数初始化 bytesReceived = 0; while (!tcpServer.isListening() && !tcpServer.listen()) { QMessageBox::StandardButton ret = QMessageBox::critical(this, tr("Loopback"), tr("Unable to start the test: %1.") .arg(tcpServer.errorString()), QMessageBox::Retry | QMessageBox::Cancel); if (ret == QMessageBox::Cancel) return; } serverStatusLabel->setText(tr("Listening")); clientStatusLabel->setText(tr("Connecting")); tcpClient.connectToHost(QHostAddress::LocalHost, tcpServer.serverPort()); // 客户端连接到本地网络,服务器端口 } void Dialog::acceptConnection() { tcpServerConnection = tcpServer.nextPendingConnection(); // 该函数将连接挂起,并返回对应该连接的QTcpSocket对象用于数据通信 connect(tcpServerConnection, SIGNAL(readyRead()), // 注意现在是服务器端在读取数据,当服务器接收到新数据则更新服务器进度条 this, SLOT(updateServerProgress())); connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError))); serverStatusLabel->setText(tr("Accepted connection")); tcpServer.close(); // 连接已经被挂起,服务器可以停止侦听 } void Dialog::startTransfer() { // called when the TCP client connected to the loopback server bytesToWrite = TotalBytes - (int)tcpClient.write(QByteArray(PayloadSize, '@')); // 一个'@'占1Byte,因此是64KB。而且使用qdebug()可以看到,这64KB并不是一 次写入的。bytesToWrite得到剩余需写入的数据量。这里没有使用QDataStream因为回送网络数据的准确性比较可靠。 clientStatusLabel->setText(tr("Connected")); } void Dialog::updateServerProgress() // 每次服务器读取到数据则调用该槽函数 { bytesReceived += (int)tcpServerConnection->bytesAvailable(); tcpServerConnection->readAll(); serverProgressBar->setMaximum(TotalBytes); // 50MB的容量上限 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 } } void Dialog::updateClientProgress(qint64 numBytes) { // callen 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) bytesToWrite -= (int)tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@')); // 很简练的语句。当剩余数据量小于64KB时就不要发64KB了。 clientProgressBar->setMaximum(TotalBytes); clientProgressBar->setValue(bytesWritten); clientStatusLabel->setText(tr("Sent %1MB") .arg(bytesWritten / (1024 * 1024))); } void Dialog::displayError(QAbstractSocket::SocketError socketError) { if (socketError == QTcpSocket::RemoteHostClosedError) return; QMessageBox::information(this, tr("Network error"), tr("The following error occurred: %1.") .arg(tcpClient.errorString())); tcpClient.close(); tcpServer.close(); clientProgressBar->reset(); serverProgressBar->reset(); clientStatusLabel->setText(tr("Client ready")); serverStatusLabel->setText(tr("Server ready")); startButton->setEnabled(true); #ifndef QT_NO_CURSOR QApplication::restoreOverrideCursor(); // 恢复鼠标样式 #endif } ~~~ 好了,这个例子结构挺简单的,但我们还有有必要将它的整个实现过程捋一捋。 首先是初始化,当我们按下start按钮后,start()槽触发,它开启了tcpServer的listen()模式,当有客户端连接该服务器时,tcpServer发射newConnection()信号,并创建了服务器端的QTcpServer对象tcpServerConnection(),同时在接收到有效数据时使用其readyRead()信号连接到Server端更新函数updateServer()。 反观客户端,当成功连接上服务器时,调用了槽函数startTransfer()。这个函数向设备写入了64000个'@',注意并不是一次写入完成的。且该函数只执行了一次。因为这次写入,byteWritten(qint64)信号被触发,所调用的槽函数updateClientProgress(qint64)记录下了此次数据的写入量,并再次写入新的数据,而这次写入又将触发byteWritten(qint64)。。。如此循环。 服务器端就简单了,在每次接收到有效数据后触发readyRead()信号,调用updateServerProgress()记录接收到的数据大小,最后在接收到的数据达到50M后关闭tcpServerConnection(注意tcpServer在创建tcpServerConnection后就已经关闭)。最后恢复鼠标形状。