搭建基于QT的TCP服务器与客户端
创始人
2024-09-25 05:51:31
0

1、实现功能

1、服务器和客户端能够建立连接

2、服务器可以给客户端发送信息

3、客户端可以给服务器发送信息

2、server

2-1、widget.h

#ifndef WIDGET_H #define WIDGET_H  #include  #include  #include  QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE  class Widget : public QWidget {     Q_OBJECT  public:     Widget(QWidget *parent = nullptr);     ~Widget(); public slots:     void readData(); private slots:     void on_send_btn_clicked();  private:     Ui::Widget *ui;     QTcpServer *server;     QTcpSocket *client; }; #endif // WIDGET_H 

2-2、widget.cpp

#include "widget.h" #include "ui_widget.h"  Widget::Widget(QWidget *parent)     : QWidget(parent)     , ui(new Ui::Widget) {     ui->setupUi(this);     //设置一下窗口标题     this->setWindowTitle("server");     //实例化服务器对象     this->server = new QTcpServer(this);     //创建监听队列并邦定ip和port     //本地主机ip,端口号为1024~49151     this->server->listen(QHostAddress("192.168.12.47"), 9999);     //等待客户端连接,做信号与槽函数关联     connect(this->server, &QTcpServer::newConnection, this, [=]()     {         this->client = this->server->nextPendingConnection();         //得到了客户端,就可以进行通信         connect(this->client, &QTcpSocket::readyRead, this, &Widget::readData);     }); }  Widget::~Widget() {     delete ui; }  //从客户端读数据 void Widget::readData() {     //从客户端中读数据     QByteArray data = this->client->readAll();     //读到的数据添加到recv_edit中     this->ui->recv_edit->append(data); }  //给客户端写数据 void Widget::on_send_btn_clicked() {     //获取到需要发送给客户端的数据     QString data = this->ui->send_edit->text();     //发送给客户端     this->client->write(data.toUtf8());     //清空输入框     this->ui->send_edit->clear(); }  

2-3、widget.ui

 

3、client

3-1、widget.h

#ifndef WIDGET_H #define WIDGET_H  #include  #include  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_connect_btn_clicked();     void on_send_btn_clicked();  public slots:     void readData(); private:     Ui::Widget *ui;     QTcpSocket *client;     int flag;     int src_size;     QByteArray src_data; }; #endif // WIDGET_H 

3-2、widget.cpp

#include "widget.h" #include "ui_widget.h" #include  #include  Widget::Widget(QWidget *parent)     : QWidget(parent)     , ui(new Ui::Widget) {     ui->setupUi(this);     this->setWindowTitle("client");      this->flag = 0;     this->src_size = 0;     this->src_data.clear();      //实例化客户端套接字对象     this->client = new QTcpSocket(this);     //成功连接与否,信号与槽的关联     connect(this->client, &QTcpSocket::connected, this, [=]()     {         QMessageBox::information(this, "提示", "成功建立连接");     });     //进行通信,读是被动的,需要做信号与槽关联     connect(this->client, &QTcpSocket::readyRead, this, &Widget::readData); }  Widget::~Widget() {     delete ui; }  //连接服务器 void Widget::on_connect_btn_clicked() {     //获取ip和port     QString ip = this->ui->ip_edit->text();     uint port = this->ui->port_edit->text().toUInt();     //连接服务器     this->client->connectToHost(QHostAddress(ip), port); }  //从客户端读数据 void Widget::readData() { //    //从客户端中读数据 //    QByteArray data = this->client->readAll(); //    //读到的数据添加到recv_edit中 //    this->ui->recv_edit->append(data);     char buf[256] = {0};     if(this->flag == 0)     {         this->client->read(buf, 256);  //FILE_SIZE#文件大小         QStringList info = QString(buf).split("#");         if(info.front() == "FILE_SIZE")         {              //获取到图片文件的大小              this->src_size = info.back().toInt();              this->flag = 1;         }     }     else if(this->flag == 1) //开始接收真正的图像数据     {          QByteArray data = this->client->readAll();          //保存下来          this->src_data.append(data);          if(this->src_data.size() == this->src_size)          {               //下载成功               QPixmap pix;               pix.loadFromData(this->src_data);               this->ui->label->setPixmap(pix.scaled(this->ui->label->size()));               this->flag = 0;               this->src_size = 0;               this->src_data.clear();          }     } }  //给客户端写数据 void Widget::on_send_btn_clicked() {     //获取到需要发送给客户端的数据     QString data = this->ui->send_edit->text();     //发送给客户端     this->client->write(data.toUtf8());     //清空输入框     this->ui->send_edit->clear(); }  

3-3、widget.ui

相关内容

热门资讯

四分钟普及!wepoker怎么... 四分钟普及!wepoker怎么看底牌(透视)外挂透明挂辅助插件(德州论坛)-哔哩哔哩;1、wepok...
2分钟介绍!we poker辅... 2分钟介绍!we poker辅助器(辅助挂)外挂透视脚本辅助app(第三方教程)-哔哩哔哩;we p...
7分钟解密!wepoker透视... 7分钟解密!wepoker透视有没有(透视)外挂透明挂辅助器安装(攻略教程)-哔哩哔哩一、wepok...
7分钟细节!wpk透视辅助靠谱... 7分钟细节!wpk透视辅助靠谱吗(透视辅助)外挂透明挂辅助插件(玩家教你)-哔哩哔哩;1、在wpk透...
八分钟介绍!德普之星辅助工具如... 八分钟介绍!德普之星辅助工具如何设置(透视)透视脚本辅助神器(爆料教程)-哔哩哔哩1、游戏颠覆性的策...
7分钟规律!hhpoker德州... 7分钟规律!hhpoker德州有挂吗(透视)外挂透明挂辅助插件(靠谱教程)-哔哩哔哩1、完成hhpo...
两分钟分析!aapoker安装... 两分钟分析!aapoker安装包怎么使用(透视脚本)外挂透明挂辅助器安装(规律教程)-哔哩哔哩1、打...
三分钟分享!wepoker网页... 三分钟分享!wepoker网页版透视方法(透视)外挂透视脚本辅助脚本(切实教程)-哔哩哔哩;1、we...
6分钟脚本!wepoker透视... 6分钟脚本!wepoker透视脚本免费下载pc(透视脚本)外挂透明挂辅助神器(2025新版教程)-哔...
7分钟分享!哈糖大菠萝挂法(辅... 7分钟分享!哈糖大菠萝挂法(辅助挂)外挂透视脚本辅助器安装(总结教程)-哔哩哔哩1、下载好哈糖大菠萝...