搭建基于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

相关内容

热门资讯

详情一下!乐酷副厅外卖辅助,传... 详情一下!乐酷副厅外卖辅助,传送屋高仿版辅助,本来真的有挂(哔哩哔哩)乐酷副厅外卖辅助脚本下载中分为...
科普一下!超凡辅助软件,永胜联... 科普一下!超凡辅助软件,永胜联盟辅助器,原来真的是有挂(哔哩哔哩)1、进入到超凡辅助软件是否有挂之后...
专业一下!随意玩有没有辅助,全... 专业一下!随意玩有没有辅助,全民比鸡bug,切实是有挂(哔哩哔哩)在进入随意玩有没有辅助软件靠谱后,...
了解一下!打哈儿辅助软件,方片... 了解一下!打哈儿辅助软件,方片十三张脚本,原来是有挂(哔哩哔哩)1、超多福利:超高返利,海量正版游戏...
教你一下!决战卡五星辅助ios... 教你一下!决战卡五星辅助ios,微乐家乡自建房辅助app,一直真的是有挂(哔哩哔哩)微乐家乡自建房辅...
辅助一下!微乐降序自建房辅助a... 辅助一下!微乐降序自建房辅助app,战神辅助官网,真是真的有挂(哔哩哔哩)1、进入到微乐降序自建房辅...
详细一下!微乐辅助靠谱麻,新5... 详细一下!微乐辅助靠谱麻,新518互游脚本下载,真是真的是有挂(哔哩哔哩)1、玩家可以在新518互游...
了解一下!广西优乐免费辅助,途... 了解一下!广西优乐免费辅助,途游辅助软件网站,一直是有挂(哔哩哔哩)1.途游辅助软件网站 选牌创建新...
辅助一下!创思维激k辅助器免费... 辅助一下!创思维激k辅助器免费,玖天乐游辅助,本来有挂(哔哩哔哩)1、上手简单,内置详细流程视频教学...
必备一下!四川游戏家园通用辅助... 必备一下!四川游戏家园通用辅助,葫芦娃七子降妖破解版,确实存在有挂(哔哩哔哩)1、玩家可以在四川游戏...