Qt(进阶3)

Qt下的TCP通信过程

代码实现

界面展示

服务器端

  1. 在.pro文件中加入

    QT += network
  2. server.h

    #ifndef SERVER_H
    #define SERVER_H
    #include <QWidget>
    #include <QTcpServer>
    #include <QTcpSocket>
    namespace Ui {
    class Server;
    }
    class Server : public QWidget
    {
     Q_OBJECT
    public:
     explicit Server(QWidget *parent = nullptr);
     ~Server();
    private slots:
     void on_pushButton_clicked();
    
     void on_pushButton_2_clicked();
    private:
     Ui::Server *ui;
     QTcpServer *tcpServer;
     QTcpSocket *tcpSocket;
    };
    #endif // SERVER_H
  3. server.cpp

    #include "server.h"
    #include "ui_server.h"
    Server::Server(QWidget *parent) :
     QWidget(parent),
     ui(new Ui::Server)
    {
     ui->setupUi(this);
     tcpServer = NULL;
     tcpSocket = NULL;
     tcpServer = new QTcpServer(this);//监听套接字
     tcpServer ->listen(QHostAddress::Any,8888);//监听,指定端口
     setWindowTitle("服务器:8888");
     connect(tcpServer,&QTcpServer::newConnection,[=](){
         //取出创建好连接的套接字
         tcpSocket = tcpServer->nextPendingConnection();
         //获取对方的IP、端口
         QString ip= tcpSocket->peerAddress().toString();
         qint16 port=tcpSocket->peerPort();
         QString temp = QString("[%1:%2]:成功连接.").arg(ip).arg(port);
         ui->read_edit->setText(temp);
    
         connect(tcpSocket,&QTcpSocket::readyRead,[=](){
             QByteArray array = tcpSocket->readAll();
             ui->write_edit->setText(array); //服务器发送来的信息写在write_edit里
    
         });
     });
    }
    Server::~Server()
    {
     delete ui;
    }
    void Server::on_pushButton_clicked()
    {
     if(NULL == tcpSocket){
         return;
     }
     QString str = ui->write_edit->toPlainText();
     //给客户端发送数据
     tcpSocket->write(str.toUtf8().data());
    }
    void Server::on_pushButton_2_clicked()
    {
     if(tcpSocket == NULL){
         return;
     }
     //主动与客户端断开连接、
     tcpSocket->disconnectFromHost();
     tcpSocket->close();
     tcpSocket=NULL;
    }

客户端

  1. client.h

    #ifndef CLIENT_H
    #define CLIENT_H
    #include <QTcpSocket>
    #include <QWidget>
    namespace Ui {
    class Client;
    }
    class Client : public QWidget
    {
     Q_OBJECT
    public:
     explicit Client(QWidget *parent = nullptr);
     ~Client();
    private slots:
     void on_pushButton_3_clicked();
    
     void on_pushButton_clicked();
    
     void on_pushButton_2_clicked();
    private:
     Ui::Client *ui;
     QTcpSocket *tcpSocket;
    };
    #endif // CLIENT_H
  2. client.cpp

    #include "client.h"
    #include "ui_client.h"
    #include <QHostAddress>
    Client::Client(QWidget *parent) :
     QWidget(parent),
     ui(new Ui::Client)
    {
     ui->setupUi(this);
    }
    Client::~Client()
    {
     delete ui;
    }
    void Client::on_pushButton_3_clicked()
    {
     tcpSocket= NULL;
     tcpSocket = new QTcpSocket(this);
     connect(tcpSocket,&QTcpSocket::connected,[=](){
         ui->textEdit->setText("成功与服务器建立连接");
     });
     connect(tcpSocket,&QTcpSocket::readyRead,[=](){
         QByteArray array = tcpSocket->readAll();
         ui->textEdit_2->setText(array);//将服务器发送的内容写在textEdit_2中
     });
    //获取服务器IP和端口
     QString ip = ui->ip_line->text();
     qint16 port = ui->port_line->text().toInt();
     //主动与服务器建立连接
     tcpSocket->connectToHost(QHostAddress(ip),port);
    }
    void Client::on_pushButton_clicked()
    {
     QString str = ui->textEdit_2->toPlainText();
     tcpSocket->write(str.toUtf8().data());//给服务器端发送信息
    }
    void Client::on_pushButton_2_clicked()
    {
     tcpSocket->disconnectFromHost();
     tcpSocket->close();
    }

   转载规则


《Qt(进阶3)》 fightingtree 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录