欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Qt中TCP協(xié)議通信詳解

 更新時間:2022年08月19日 17:04:54   作者:碼靈薯  
這篇文章主要為大家詳細介紹了Qt中TCP協(xié)議通信,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

TCP協(xié)議是經(jīng)常使用的通信方式。在QT中做了非常友好的封裝,使用非常方便。

需要添加的模塊:network

Qt中的TCP類:QTcpSocket , QTcpServer

常用函數(shù)介紹

連接目標地址和端口

virtual void QTcpSocket ::connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite);

  • 發(fā)送數(shù)據(jù)

inline qint64 QTcpSocket ::write(const QByteArray &data)

  • 監(jiān)聽某個地址和端口號

bool QTcpServer::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);

  • 有新的連接信號

void QTcpServer::newConnection();

  • 是否有新的連接

virtual bool QTcpServer::hasPendingConnections() const;

  • 獲取新的連接,必須處理完才能繼續(xù)接收到連接

virtual QTcpSocket *QTcpServer::nextPendingConnection();

  • 收到新的數(shù)據(jù)信號

void QTcpSocket::readyRead();

  • 讀取收到的數(shù)據(jù),必須讀取完才能繼續(xù)接收

QByteArray readAll();

使用案例(example)

客戶端

#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();
?? ?}
}

服務端

#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)聽跟本主機相連的所有網(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())//必須處理完所有的連接,否則有新連接時就不會在發(fā)信號過來
?? ?{
?? ??? ?_socket = _server->nextPendingConnection();
?? ??? ?connect(_socket, &QTcpSocket::readyRead, [&]() {
?? ??? ??? ?_textBrowser->append("receive message...");
?? ??? ??? ?QByteArray newMessage = _socket->readAll();
?? ??? ??? ?_textBrowser->append(QString(newMessage));
?? ??? ?});
?? ?}
}

使用效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 解讀C++編程的相關文件操作

    解讀C++編程的相關文件操作

    這篇文章主要介紹了解讀C++編程的相關文件操作,是C++入門學習中的基礎知識,需要的朋友可以參考下
    2015-09-09
  • C語言中char*和char[]用法區(qū)別分析

    C語言中char*和char[]用法區(qū)別分析

    這篇文章主要介紹了C語言中char*和char[]用法區(qū)別,包括使用過程中的誤區(qū)及注意點分析,需要的朋友可以參考下
    2014-09-09
  • C語言結構體的全方面解讀

    C語言結構體的全方面解讀

    C 數(shù)組允許定義可存儲相同類型數(shù)據(jù)項的變量,結構是 C 編程中另一種用戶自定義的可用的數(shù)據(jù)類型,它允許你存儲不同類型的數(shù)據(jù)項
    2021-10-10
  • C++關于指針,繼承和多態(tài)介紹

    C++關于指針,繼承和多態(tài)介紹

    大家好,本篇文章主要講的是C++關于指針,繼承和多態(tài)介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • C++開發(fā)截屏小程序功能

    C++開發(fā)截屏小程序功能

    這篇文章主要介紹了C++開發(fā)截屏小程序功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • c++獲取進程信息列表和進程所調用的dll列表

    c++獲取進程信息列表和進程所調用的dll列表

    這篇文章主要介紹了c++獲取進程信息列表和進程所調用的dll列表,大家參考使用吧
    2013-11-11
  • C語言菜鳥基礎教程之a(chǎn)++與++a

    C語言菜鳥基礎教程之a(chǎn)++與++a

    很多同學在學習c語言的時候是不是會碰到a++和++a都有甚么作用啊。今天我們就來探討下
    2017-10-10
  • C++中nullptr 和 NULL 的區(qū)別及用法

    C++中nullptr 和 NULL 的區(qū)別及用法

    nullptr是常數(shù),nullptr_t是它的類型.在需要分別使用空指針或空指針類型的上下文中使用每一個.今天通過本文給大家介紹C++ nullptr 和 NULL 的使用區(qū)別,需要的朋友參考下吧
    2021-07-07
  • c++中inline的用法分析

    c++中inline的用法分析

    本篇文章是對c++中inline的用法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • c語言二進制數(shù)按位輸出示例

    c語言二進制數(shù)按位輸出示例

    這篇文章主要介紹了c語言二進制數(shù)按位輸出示例,需要的朋友可以參考下
    2014-03-03

最新評論