QT socket通讯客户端与服务端实例
qt通讯很简单,主要是利用socket通讯,网上有很多案例,案例稍有漏洞,我自己稍加改善,可实现一对一的收发,如下
QT += network
具体效果如下
client.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QTcpSocket> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_pushButton_3_clicked(); void on_pushButton_clicked(); void on_pushButton_2_clicked(); void connectToServer(); void readInformation(); private: Ui::Widget *ui; QTcpSocket *tcpsocket= NULL; }; #endif // WIDGET_H
client.cpp
#include "widget.h" #include "ui_widget.h" #include <QHostAddress> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; } //写socket void Widget::on_pushButton_clicked() { QString str = ui->textEditWrite->toPlainText(); tcpsocket->write(str.toUtf8().data()); } //主动断开链接 void Widget::on_pushButton_2_clicked() { if(tcpsocket == NULL)return; tcpsocket->disconnectFromHost();//主动和客户端断开连接 tcpsocket->close(); tcpsocket = NULL; //if(tcpsocket == NULL) ui->textEditRead->append("已断开链接"); } //已经连接到服务器 void Widget::connectToServer() { ui->textEditRead->setText("成功和服务器进行连接"); } //从通道读取数据 void Widget::readInformation() { QByteArray temp = tcpsocket->readAll(); ui->textEditRead->append(temp); } //创建链接 void Widget::on_pushButton_3_clicked() { QString ip = ui->lineEditIpAddress->text(); qint16 port = ui->lineEditPort->text().toInt(); if(tcpsocket != NULL){tcpsocket->deleteLater();tcpsocket=NULL;} tcpsocket = new QTcpSocket(); connect(tcpsocket, &QTcpSocket::connected, this, &Widget::connectToServer); connect(tcpsocket, &QTcpSocket::readyRead, this, &Widget::readInformation); connect(tcpsocket, &QTcpSocket::disconnected, this, [this](){ ui->textEditRead->append("已断开链接"); }); //主动和服务器进行连接 tcpsocket->connectToHost((QHostAddress)ip, port); }
server.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QTcpServer> #include <QTcpSocket> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); void connectToClient(); void readInformation(); void on_pushButton_3_clicked(); private: Ui::Widget *ui; QTcpServer *tcpserver = NULL ; QTcpSocket *tcpsocket = NULL; }; #endif // WIDGET_H
server.cpp
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; } //创建链接 void Widget::connectToClient() { if(tcpsocket != NULL){tcpsocket->deleteLater();tcpsocket=NULL;} tcpsocket = tcpserver->nextPendingConnection(); connect(tcpsocket, &QTcpSocket::readyRead, this, &Widget::readInformation); connect(tcpsocket, &QTcpSocket::disconnected, this, [this](){ ui->textEditRead->append("已断开链接"); }); QString ip = tcpsocket->peerAddress().toString(); qint16 port = tcpsocket->peerPort(); ui->textEditRead->setText(QString("[%1:%2]连接成功").arg(ip).arg(port)); } //从通道读取数据 void Widget::readInformation() { QByteArray temp = tcpsocket->readAll(); ui->textEditRead->append(temp); } //向通道写数据 void Widget::on_pushButton_clicked() { QString str = ui->textEditWrite->toPlainText(); tcpsocket->write(str.toUtf8().data()); } //客户端主动断开链接 void Widget::on_pushButton_2_clicked() { if(tcpsocket == NULL)return; tcpsocket->disconnectFromHost();//主动和客户端断开连接 tcpsocket->close(); tcpsocket->deleteLater(); tcpsocket = NULL; //if(tcpsocket == NULL) ui->textEditRead->append("已断开链接"); } //创建监听 void Widget::on_pushButton_3_clicked() { if(tcpserver!=NULL){on_pushButton_2_clicked();tcpserver->deleteLater();tcpserver = NULL;} //if(tcpserver!=NULL){tcpserver->deleteLater();tcpserver = NULL;} //if(tcpserver!=NULL){tcpserver = NULL;} tcpserver = new QTcpServer(this); int port = ui->lineEditListenPort->text().toInt(); tcpserver->listen(QHostAddress::Any, port); connect(tcpserver, &QTcpServer::newConnection, this, &Widget::connectToClient); }
下载链接:
https://files.cnblogs.com/files/RYSBlog/socket_demo.zip
如果觉得可以的话,给个一键三连呗