您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Qt中TCP協(xié)議通信怎么使用”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Qt中TCP協(xié)議通信怎么使用”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
TCP協(xié)議是經(jīng)常使用的通信方式。在QT中做了非常友好的封裝,使用非常方便。
需要添加的模塊:network
Qt中的TCP類(lèi):QTcpSocket , QTcpServer
連接目標(biāo)地址和端口
virtual void QTcpSocket ::connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite);
發(fā)送數(shù)據(jù)
inline qint64 QTcpSocket ::write(const QByteArray &data)
監(jiān)聽(tīng)某個(gè)地址和端口號(hào)
bool QTcpServer::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
有新的連接信號(hào)
void QTcpServer::newConnection();
是否有新的連接
virtual bool QTcpServer::hasPendingConnections() const;
獲取新的連接,必須處理完才能繼續(xù)接收到連接
virtual QTcpSocket *QTcpServer::nextPendingConnection();
收到新的數(shù)據(jù)信號(hào)
void QTcpSocket::readyRead();
讀取收到的數(shù)據(jù),必須讀取完才能繼續(xù)接收
QByteArray readAll();
客戶(hù)端
#include <QTcpSocket> #include <QWidget> #include <QLineEdit> class Client : public QWidget { Q_OBJECT public: Client(QWidget *parent); public slots: void slotSendButtonClick(); private: QTcpSocket *_socket; QLineEdit *_lineEdit; bool _isConnected; };
#include "client.h" #include <QPushButton> #include <QHBoxLayout> Client::Client(QWidget *parent) : QWidget(parent) { _socket = new QTcpSocket(this); _lineEdit = new QLineEdit(this); QPushButton *sendButton = new QPushButton("send"); connect(sendButton, SIGNAL(clicked()), this, SLOT(slotSendButtonClick())); connect(_lineEdit, SIGNAL(returnPressed()), this, SLOT(slotSendButtonClick())); QHBoxLayout *lay = new QHBoxLayout(this); lay->addWidget(_lineEdit); lay->addWidget(sendButton); _isConnected = false; } void Client::slotSendButtonClick() { if (!_isConnected) { _socket->connectToHost("127.0.0.1", 9988); _isConnected = true; } QString text = _lineEdit->text(); if (!text.isEmpty()) { _socket->write(text.toUtf8());//發(fā)送數(shù)據(jù) _lineEdit->clear(); } }
服務(wù)端
#include <QWidget> #include <QTcpServer> #include <QTcpSocket> class QTextBrowser; class Server :public QWidget { Q_OBJECT public: Server(QWidget *parent); public slots: void slotCurrentIndexChanged(const QString&); void slotNewConnection(); private: QTcpServer *_server; QTcpSocket *_socket; QTextBrowser *_textBrowser; };
#include "server.h" #include <QHostAddress> #include <QTextBrowser> #include <QByteArray> #include <QGridLayout> #include <QNetworkInterface> #include <QComboBox> Server::Server(QWidget *parent) { _server = new QTcpServer(this); //_server->listen(QHostAddress::Any, 9988);//監(jiān)聽(tīng)跟本主機(jī)相連的所有網(wǎng)口 connect(_server, SIGNAL(newConnection()),this, SLOT(slotNewConnection()) ); QList<QHostAddress> addrList = QNetworkInterface::allAddresses(); QComboBox *comboBox = new QComboBox; for (const QHostAddress& addr : addrList) { quint32 ipAddr = addr.toIPv4Address(); if (ipAddr != 0) { comboBox->addItem(QHostAddress(ipAddr).toString()); } } connect(comboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(slotCurrentIndexChanged(const QString&))); _textBrowser = new QTextBrowser(this); QGridLayout *lay = new QGridLayout(this); lay->addWidget(comboBox, 0, 0); lay->addWidget(_textBrowser,1,0); } void Server::slotCurrentIndexChanged(const QString& item) { if (!_server->isListening()) { _server->listen(QHostAddress(item), 9988); } _textBrowser->append("listen..."); } void Server::slotNewConnection() { _textBrowser->append("connecting..."); while (_server->hasPendingConnections())//必須處理完所有的連接,否則有新連接時(shí)就不會(huì)在發(fā)信號(hào)過(guò)來(lái) { _socket = _server->nextPendingConnection(); connect(_socket, &QTcpSocket::readyRead, [&]() { _textBrowser->append("receive message..."); QByteArray newMessage = _socket->readAll(); _textBrowser->append(QString(newMessage)); }); } }
使用效果
讀到這里,這篇“Qt中TCP協(xié)議通信怎么使用”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。