日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt学习(五):TCP通信

發(fā)布時間:2025/3/12 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt学习(五):TCP通信 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

知識點

  • 通信套接字和監(jiān)聽套接字
  • 獲取編輯器內(nèi)容,發(fā)送到套接字
    完整項目github地址:
https://github.com/taw19960426/Qt_study/tree/main/QTcpTest

結(jié)果演示

widget.cpp

#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);//為了防止段錯誤tcpServer = NULL;tcpSocket = NULL;setWindowTitle("服務(wù)端(端口:8888)");//監(jiān)聽套接字,指定父對象,讓其自動回收空間tcpServer = new QTcpServer(this);tcpServer->listen(QHostAddress::Any,8888);connect(tcpServer,&QTcpServer::newConnection,[=](){//取出建立好連接的套接字tcpSocket=tcpServer->nextPendingConnection();//獲取對方的IP和端口QString ip=tcpSocket->peerAddress().toString();qint16 port =tcpSocket->peerPort();QString ipDate=QString("ip=%1 port=%2").arg(ip).arg(port);ui->showEdit->setText(ipDate);//從通信套接字中取出內(nèi)容connect(tcpSocket,&QTcpSocket::readyRead,[=](){QByteArray array=tcpSocket->readAll();ui->showEdit->append(array);});}); }Widget::~Widget() {delete ui; }void Widget::on_buttonSend_clicked() {if(NULL == tcpSocket){return;}//獲取編輯區(qū)內(nèi)容QString str=ui->sendEdit->toPlainText();//給對方發(fā)送數(shù)據(jù), 使用套接字是tcpSockettcpSocket->write(str.toUtf8().data()); }void Widget::on_buttonClose_clicked() {if(NULL == tcpSocket){return;}//主動和客戶端端口連接tcpSocket->disconnectFromHost();tcpSocket->close();tcpSocket = NULL; }

widget.h

#ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QTcpServer> //監(jiān)聽套接字 #include <QTcpSocket> //通信套接字namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private slots:void on_buttonSend_clicked();void on_buttonClose_clicked();private:Ui::Widget *ui;QTcpServer *tcpServer; //監(jiān)聽套接字QTcpSocket *tcpSocket; //通信套接字 };#endif // WIDGET_H

clienttcp.cpp

#include "clienttcp.h" #include "ui_clienttcp.h" #include <QHostAddress>ClientTCP::ClientTCP(QWidget *parent) :QWidget(parent),ui(new Ui::ClientTCP) {ui->setupUi(this);setWindowTitle("客戶端");tcpSocket=new QTcpSocket(this);connect(tcpSocket,&QTcpSocket::connected,[=](){ui->textEditRead->setText("與服務(wù)端已經(jīng)成功連接!");});//從通信套接字里面取內(nèi)容connect(tcpSocket,&QTcpSocket::readyRead,[=](){//獲取對方發(fā)送的內(nèi)容QByteArray array = tcpSocket->readAll();//追加到編輯區(qū)中ui->textEditRead->append(array);});}ClientTCP::~ClientTCP() {delete ui; }void ClientTCP::on_buttonConnect_clicked() {//獲取服務(wù)器ip和端口QString ip=ui->lineEditIP->text();qint16 port=ui->lineEditPort->text().toInt();//主動和服務(wù)器建立連接tcpSocket->connectToHost(QHostAddress(ip),port); }void ClientTCP::on_buttonWrite_clicked() {//獲取編輯器內(nèi)容,發(fā)送到套接字//獲取編輯區(qū)內(nèi)容QString str=ui->textEditWrite->toPlainText();//給對方發(fā)送數(shù)據(jù), 使用套接字是tcpSockettcpSocket->write(str.toUtf8().data()); }void ClientTCP::on_buttonClose_clicked() {//主動和客戶端端口連接tcpSocket->disconnectFromHost();tcpSocket->close();}

clienttcp.h

#ifndef CLIENTTCP_H #define CLIENTTCP_H#include <QWidget> #include <QTcpSocket>namespace Ui { class ClientTCP; }class ClientTCP : public QWidget {Q_OBJECTpublic:explicit ClientTCP(QWidget *parent = 0);~ClientTCP();private slots:void on_buttonConnect_clicked();void on_buttonWrite_clicked();void on_buttonClose_clicked();private:Ui::ClientTCP *ui;QTcpSocket *tcpSocket; //通信套接字};#endif // CLIENTTCP_H

總結(jié)

以上是生活随笔為你收集整理的Qt学习(五):TCP通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。