QT module learning-TCP communication (working process)

QT module learning-TCP communication (working process)

work process

For communication, in layman's terms, the client first establishes communication with the server, that is, sends a request, and the server that has been listening at this time responds to the request and agrees to connect after receiving the request, and then sends it. After the transmission is successful, readyRead () will be processed accordingly.
Insert picture description here

Code

server

The server needs two sockets, listening socket and communication socket

 QTcpServer *tcpsever;
    QTcpSocket *tcpsocket;

ui design:
server:
Insert picture description here

#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    
    
    ui->setupUi(this);
    tcpsever=NULL;
    tcpsocket=NULL;
    setWindowTitle("端口号为8888的服务器");
   tcpsever = new QTcpServer(this);
   tcpsever->listen(QHostAddress::Any,8888);
   connect(tcpsever,&QTcpServer::newConnection,
           [=](){
    
    
       tcpsocket = tcpsever->nextPendingConnection();
       //获得对方的IP和端口
       QString ip = tcpsocket->peerAddress().toString();
       qint16 port = tcpsocket->peerPort();
       QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
       ui->textEdit_2->setText(temp);
       connect(tcpsocket, &QTcpSocket::readyRead,
               [=](){
    
    
          QByteArray array=tcpsocket->readAll();
          ui->textEdit_2->append(array);
       });
   });
}


Widget::~Widget()
{
    
    
    delete ui;
}




void Widget::on_pushButton_send_clicked()
{
    
    
    if(NULL==tcpsocket){
    
    
        return;
    }
    //获取编辑区内容
    QString str = ui->textEdit->toPlainText();
    tcpsocket->write(str.toUtf8().data());
}


void Widget::on_pushButton_2_clicked()
{
    
    
    tcpsocket->disconnectFromHost();//
    tcpsocket->close();
}
Client

Only need a communication socket
ui interface:
Insert picture description here

#include "client.h"
#include "ui_client.h"
#include <QHostAddress>
client::client(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::client)
{
    
    
    ui->setupUi(this);


    tcpsocket = NULL;
    tcpsocket = new QTcpSocket(this);


    connect(tcpsocket, QTcpSocket::connected,
            [=]()
    {
    
    
       ui->read->setText("成功与服务器建立连接");
    }
    );


    connect(tcpsocket,&QTcpSocket::readyRead,
            [=](){
    
    
       QByteArray array = tcpsocket->readAll();
       ui->read->setText(array);
    });
}


client::~client()
{
    
    
    delete ui;
}


void client::on_pushButton_3_clicked()
{
    
    
    qint16  port = ui ->lineEdit->text().toInt();
    QString ip = ui->lineEdit_2->text();
    tcpsocket->connectToHost(QHostAddress(ip),port);//请求通讯
}
void client::on_pushButton_clicked()
{
    
    
    QString str = ui->write->toPlainText();
    tcpsocket->write(str.toUtf8().data());
    ui->write->clear();
}
void client::on_pushButton_2_clicked()
{
    
    
    tcpsocket->disconnectFromHost();
    tcpsocket->close();
}

Guess you like

Origin blog.csdn.net/m0_50210478/article/details/108595637