本系列所有文章可以在這里查看[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后就已經關閉)。最后恢復鼠標形狀。
- 前言
- 1——Fortune Server/Client
- 2——Multicast Sender/Receiverz
- 3——Broadcast Sender/Receiver
- 4——Blocking Fortune Client
- 5——Threaded Fortune Server
- 5(總結)——Fortune例程的各個實現區別
- 6——Loopback Example
- 7——Analog Clock Example
- 8——Shaped Clock Example
- 9——Analog Clock Window Example
- 10——Qt Quick Particles Examples - Emitters
- 11——Qt Quick Particles Examples - Affectors
- 12——Qt Quick Particles Examples - CustomParticles
- 13——Qt Quick Particles Examples - Image Particles
- 14——Qt Quick Particles Examples - System
- 15——Chapter 1: Creating a New Type
- 16——Chapter 2: Connecting to C++ Methods and Signals
- 17——Chapter 3: Adding Property Bindings
- 18——Chapter 4: Using Custom Property Types
- 19——Chapter 5: Using List Property Types
- 20——Chapter 6: Writing an Extension Plugin
- 21——Extending QML - Adding Types Example
- 22——Extending QML - Object and List Property Types Example
- 23——Extending QML - Inheritance and Coercion Example
- 24——Extending QML - Default Property Example
- 25——Extending QML - Methods Example
- 26——Extending QML - Grouped Properties Example
- 27——Extending QML - Attached Properties Example
- 28——Extending QML - Signal Support Example
- 29——Extending QML - Property Value Source Example
- 30——Extending QML - Binding Example
- 31——StocQt
- 32——Qt Quick Examples - Threading
- 33——Qt Quick Examples - Window and Screen
- 34——Concentric Circles Example
- 35——Music Player
- 36——Wiggly Example
- 37——Vector Deformation