QT網(wǎng)絡(luò)通信TCP客戶端實(shí)現(xiàn)詳解
本文實(shí)例為大家分享了QT網(wǎng)絡(luò)通信TCP客戶端實(shí)現(xiàn)的具體代碼,供大家參考,具體內(nèi)容如下
QT中基于TCP套接字的網(wǎng)絡(luò)通信需要用到兩個(gè)類
- QTcpServer:服務(wù)器類,用于監(jiān)聽(tīng)客戶端連接和客戶端建立連接
- QTcpSocket:通信套接字類,客戶端和服務(wù)端都需要使用*
這兩個(gè)類都屬于網(wǎng)絡(luò)通信的network
需要在工程路徑下添加network
QT += core gui network
服務(wù)器
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : ? ? QMainWindow(parent), ? ? ui(new Ui::MainWindow) { ? ? ui->setupUi(this); ? ? ui->port->setText("8899"); ? ? ui->ip->setText("127.0.0.1"); ? ? setWindowTitle("客戶端"); ? ? m_tcp = new QTcpSocket(this); ? ? connect(m_tcp,&QTcpSocket::readyRead,this,[=](){ ? ? ? ? QByteArray data = m_tcp->readAll(); ? ? ? ? ui->record->append("服務(wù)端:"+data); ? ? }); ? ? ?connect(m_tcp,&QTcpSocket::disconnected,this,[=]() ? ? { ? ? ? ? ?ui->connect->setEnabled(true); ? ? ? ? ?ui->disconnect->setDisabled(true); ? ? ? ? m_tcp->close(); ?// ? ? ? m_tcp->deleteLater();//狀態(tài)釋放 ? ? ? ? m_status->setPixmap(QPixmap(":/red.png").scaled(20,20)); ? ? ? ? ui->record->append("斷開(kāi)連接"); ? ? }); ? ? ?connect(m_tcp,&QTcpSocket::connected,this,[=](){ ? ? ? ?m_status->setPixmap(QPixmap(":/green.png").scaled(20,20)); ? ? ? ?ui->connect->setDisabled(true); ? ? ? ?ui->disconnect->setEnabled(true); ? ? ? ?ui->record->append("連接成功"); ? ? ?}); ? ? // ? ? ui->disconnect->setDisabled(true); ? ? m_status = new QLabel; ? ? m_status->setPixmap(QPixmap(":/red.png").scaled(20,20)); ? ? ui->statusBar->addWidget(new QLabel("連接狀態(tài):")); ? ? ui->statusBar->addWidget(m_status); } MainWindow::~MainWindow() { ? ? delete ui; } void MainWindow::on_sendMsg_clicked() { ? ?QString msg = ui->message->toPlainText(); ? ? m_tcp->write(msg.toUtf8()); ? ? ui->record->append("客戶端:"+msg); } void MainWindow::on_connect_clicked() { ? ? QString ip=ui->ip->text(); ? ? unsigned short port=ui->port->text().toUShort(); ? ? m_tcp->connectToHost(QHostAddress(ip),port); ? ? ui->connect->setEnabled(false); ? ? ui->disconnect->setDisabled(false); } void MainWindow::on_disconnect_clicked() { ? ? m_tcp->close(); ? ? ui->connect->setEnabled(true); ? ? ui->disconnect->setDisabled(true); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTcpSocket> #include <Qlabel> #include <QHostAddress> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { ? ? Q_OBJECT public: ? ? explicit MainWindow(QWidget *parent = nullptr); ? ? ~MainWindow(); private slots: ? ? void on_sendMsg_clicked(); ? ? void on_connect_clicked(); ? ? void on_disconnect_clicked(); private: ? ? Ui::MainWindow *ui; ? ? QTcpSocket ?*m_tcp; ? ? QLabel ? ? *m_status; }; #endif // MAINWINDOW_H
ui文件
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> ?<class>MainWindow</class> ?<widget class="QMainWindow" name="MainWindow"> ? <property name="geometry"> ? ?<rect> ? ? <x>0</x> ? ? <y>0</y> ? ? <width>428</width> ? ? <height>606</height> ? ?</rect> ? </property> ? <property name="windowTitle"> ? ?<string>MainWindow</string> ? </property> ? <widget class="QWidget" name="centralWidget"> ? ?<layout class="QVBoxLayout" name="verticalLayout"> ? ? <item> ? ? ?<widget class="QWidget" name="widget" native="true"> ? ? ? <layout class="QGridLayout" name="gridLayout"> ? ? ? ?<item row="1" column="0"> ? ? ? ? <widget class="QLabel" name="label_3"> ? ? ? ? ?<property name="text"> ? ? ? ? ? <string>IP:</string> ? ? ? ? ?</property> ? ? ? ? </widget> ? ? ? ?</item> ? ? ? ?<item row="0" column="0"> ? ? ? ? <widget class="QLabel" name="label"> ? ? ? ? ?<property name="text"> ? ? ? ? ? <string>端口:</string> ? ? ? ? ?</property> ? ? ? ? </widget> ? ? ? ?</item> ? ? ? ?<item row="1" column="1"> ? ? ? ? <widget class="QLineEdit" name="ip"/> ? ? ? ?</item> ? ? ? ?<item row="1" column="2"> ? ? ? ? <widget class="QPushButton" name="disconnect"> ? ? ? ? ?<property name="text"> ? ? ? ? ? <string>斷開(kāi)連接</string> ? ? ? ? ?</property> ? ? ? ? </widget> ? ? ? ?</item> ? ? ? ?<item row="0" column="1"> ? ? ? ? <widget class="QLineEdit" name="port"/> ? ? ? ?</item> ? ? ? ?<item row="0" column="2"> ? ? ? ? <widget class="QPushButton" name="connect"> ? ? ? ? ?<property name="text"> ? ? ? ? ? <string>連接服務(wù)器</string> ? ? ? ? ?</property> ? ? ? ? </widget> ? ? ? ?</item> ? ? ? </layout> ? ? ?</widget> ? ? </item> ? ? <item> ? ? ?<widget class="QGroupBox" name="groupBox"> ? ? ? <property name="title"> ? ? ? ?<string>歷史信息</string> ? ? ? </property> ? ? ? <layout class="QHBoxLayout" name="horizontalLayout_2"> ? ? ? ?<item> ? ? ? ? <widget class="QTextEdit" name="record"/> ? ? ? ?</item> ? ? ? </layout> ? ? ?</widget> ? ? </item> ? ? <item> ? ? ?<widget class="QGroupBox" name="groupBox_2"> ? ? ? <property name="title"> ? ? ? ?<string>發(fā)送信息</string> ? ? ? </property> ? ? ? <layout class="QHBoxLayout" name="horizontalLayout_3"> ? ? ? ?<item> ? ? ? ? <widget class="QTextEdit" name="message"/> ? ? ? ?</item> ? ? ? </layout> ? ? ?</widget> ? ? </item> ? ? <item> ? ? ?<widget class="QWidget" name="widget_2" native="true"> ? ? ? <layout class="QHBoxLayout" name="horizontalLayout_4"> ? ? ? ?<item> ? ? ? ? <spacer name="horizontalSpacer_2"> ? ? ? ? ?<property name="orientation"> ? ? ? ? ? <enum>Qt::Horizontal</enum> ? ? ? ? ?</property> ? ? ? ? ?<property name="sizeHint" stdset="0"> ? ? ? ? ? <size> ? ? ? ? ? ?<width>136</width> ? ? ? ? ? ?<height>20</height> ? ? ? ? ? </size> ? ? ? ? ?</property> ? ? ? ? </spacer> ? ? ? ?</item> ? ? ? ?<item> ? ? ? ? <widget class="QPushButton" name="sendMsg"> ? ? ? ? ?<property name="text"> ? ? ? ? ? <string>發(fā)送信息</string> ? ? ? ? ?</property> ? ? ? ? </widget> ? ? ? ?</item> ? ? ? ?<item> ? ? ? ? <spacer name="horizontalSpacer"> ? ? ? ? ?<property name="orientation"> ? ? ? ? ? <enum>Qt::Horizontal</enum> ? ? ? ? ?</property> ? ? ? ? ?<property name="sizeHint" stdset="0"> ? ? ? ? ? <size> ? ? ? ? ? ?<width>135</width> ? ? ? ? ? ?<height>20</height> ? ? ? ? ? </size> ? ? ? ? ?</property> ? ? ? ? </spacer> ? ? ? ?</item> ? ? ? </layout> ? ? ?</widget> ? ? </item> ? ?</layout> ? </widget> ? <widget class="QMenuBar" name="menuBar"> ? ?<property name="geometry"> ? ? <rect> ? ? ?<x>0</x> ? ? ?<y>0</y> ? ? ?<width>428</width> ? ? ?<height>23</height> ? ? </rect> ? ?</property> ? </widget> ? <widget class="QToolBar" name="mainToolBar"> ? ?<attribute name="toolBarArea"> ? ? <enum>TopToolBarArea</enum> ? ?</attribute> ? ?<attribute name="toolBarBreak"> ? ? <bool>false</bool> ? ?</attribute> ? </widget> ? <widget class="QStatusBar" name="statusBar"/> ?</widget> ?<layoutdefault spacing="6" margin="11"/> ?<resources/> ?<connections/> </ui>
ui界面
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Qt中TCP協(xié)議通信詳解
- QT實(shí)現(xiàn)簡(jiǎn)單TCP通信
- Qt實(shí)現(xiàn)簡(jiǎn)單的TCP通信
- QT編寫(xiě)tcp通信工具(Client篇)
- Qt?TCP網(wǎng)絡(luò)通信學(xué)習(xí)
- Qt網(wǎng)絡(luò)編程實(shí)現(xiàn)TCP通信
- QT5實(shí)現(xiàn)簡(jiǎn)單的TCP通信的實(shí)現(xiàn)
- 基于QT的TCP通信服務(wù)的實(shí)現(xiàn)
- QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)的即時(shí)通信實(shí)例
- Qt TCP實(shí)現(xiàn)簡(jiǎn)單通信功能
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)消消樂(lè)小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)消消樂(lè)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12Matlab利用遺傳算法GA求解非連續(xù)函數(shù)問(wèn)題詳解
遺傳算法起源于對(duì)生物系統(tǒng)所進(jìn)行的計(jì)算機(jī)模擬研究。其本質(zhì)是一種高效、并行、全局搜索的方法,能在搜索過(guò)程中自動(dòng)獲取和積累有關(guān)搜索空間的知識(shí),并自適應(yīng)地控制搜索過(guò)程以求得最佳解。本文將利用其求解非連續(xù)函數(shù)問(wèn)題,需要的可以參考一下2022-09-09淺談C語(yǔ)言的字節(jié)對(duì)齊 #pragma pack(n)2
下面小編就為大家?guī)?lái)一篇淺談C語(yǔ)言的字節(jié)對(duì)齊 #pragma pack(n)2。小編覺(jué)得挺不錯(cuò)的現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01