搭建基于QT的TCP服务器与客户端

[复制链接]
发表于 2026-1-28 03:24:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1、实现功能

1、服务器和客户端可以或许创建毗连
2、服务器可以给客户端发送信息
3、客户端可以给服务器发送信息


2、server

2-1、widget.h
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QTcpServer>
  5. #include <QTcpSocket>
  6. QT_BEGIN_NAMESPACE
  7. namespace Ui { class Widget; }
  8. QT_END_NAMESPACE
  9. class Widget : public QWidget
  10. {
  11.     Q_OBJECT
  12. public:
  13.     Widget(QWidget *parent = nullptr);
  14.     ~Widget();
  15. public slots:
  16.     void readData();
  17. private slots:
  18.     void on_send_btn_clicked();
  19. private:
  20.     Ui::Widget *ui;
  21.     QTcpServer *server;
  22.     QTcpSocket *client;
  23. };
  24. #endif // WIDGET_H
复制代码
2-2、widget.cpp
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. Widget::Widget(QWidget *parent)
  4.     : QWidget(parent)
  5.     , ui(new Ui::Widget)
  6. {
  7.     ui->setupUi(this);
  8.     //设置一下窗口标题
  9.     this->setWindowTitle("server");
  10.     //实例化服务器对象
  11.     this->server = new QTcpServer(this);
  12.     //创建监听队列并邦定ip和port
  13.     //本地主机ip,端口号为1024~49151
  14.     this->server->listen(QHostAddress("192.168.12.47"), 9999);
  15.     //等待客户端连接,做信号与槽函数关联
  16.     connect(this->server, &QTcpServer::newConnection, this, [=]()
  17.     {
  18.         this->client = this->server->nextPendingConnection();
  19.         //得到了客户端,就可以进行通信
  20.         connect(this->client, &QTcpSocket::readyRead, this, &Widget::readData);
  21.     });
  22. }
  23. Widget::~Widget()
  24. {
  25.     delete ui;
  26. }
  27. //从客户端读数据
  28. void Widget::readData()
  29. {
  30.     //从客户端中读数据
  31.     QByteArray data = this->client->readAll();
  32.     //读到的数据添加到recv_edit中
  33.     this->ui->recv_edit->append(data);
  34. }
  35. //给客户端写数据
  36. void Widget::on_send_btn_clicked()
  37. {
  38.     //获取到需要发送给客户端的数据
  39.     QString data = this->ui->send_edit->text();
  40.     //发送给客户端
  41.     this->client->write(data.toUtf8());
  42.     //清空输入框
  43.     this->ui->send_edit->clear();
  44. }
复制代码
2-3、widget.ui

 

3、client

3-1、widget.h
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QTcpSocket>
  5. QT_BEGIN_NAMESPACE
  6. namespace Ui { class Widget; }
  7. QT_END_NAMESPACE
  8. class Widget : public QWidget
  9. {
  10.     Q_OBJECT
  11. public:
  12.     Widget(QWidget *parent = nullptr);
  13.     ~Widget();
  14. private slots:
  15.     void on_connect_btn_clicked();
  16.     void on_send_btn_clicked();
  17. public slots:
  18.     void readData();
  19. private:
  20.     Ui::Widget *ui;
  21.     QTcpSocket *client;
  22.     int flag;
  23.     int src_size;
  24.     QByteArray src_data;
  25. };
  26. #endif // WIDGET_H
复制代码
3-2、widget.cpp
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QHostAddress>
  4. #include <QMessageBox>
  5. Widget::Widget(QWidget *parent)
  6.     : QWidget(parent)
  7.     , ui(new Ui::Widget)
  8. {
  9.     ui->setupUi(this);
  10.     this->setWindowTitle("client");
  11.     this->flag = 0;
  12.     this->src_size = 0;
  13.     this->src_data.clear();
  14.     //实例化客户端套接字对象
  15.     this->client = new QTcpSocket(this);
  16.     //成功连接与否,信号与槽的关联
  17.     connect(this->client, &QTcpSocket::connected, this, [=]()
  18.     {
  19.         QMessageBox::information(this, "提示", "成功建立连接");
  20.     });
  21.     //进行通信,读是被动的,需要做信号与槽关联
  22.     connect(this->client, &QTcpSocket::readyRead, this, &Widget::readData);
  23. }
  24. Widget::~Widget()
  25. {
  26.     delete ui;
  27. }
  28. //连接服务器
  29. void Widget::on_connect_btn_clicked()
  30. {
  31.     //获取ip和port
  32.     QString ip = this->ui->ip_edit->text();
  33.     uint port = this->ui->port_edit->text().toUInt();
  34.     //连接服务器
  35.     this->client->connectToHost(QHostAddress(ip), port);
  36. }
  37. //从客户端读数据
  38. void Widget::readData()
  39. {
  40. //    //从客户端中读数据
  41. //    QByteArray data = this->client->readAll();
  42. //    //读到的数据添加到recv_edit中
  43. //    this->ui->recv_edit->append(data);
  44.     char buf[256] = {0};
  45.     if(this->flag == 0)
  46.     {
  47.         this->client->read(buf, 256);  //FILE_SIZE#文件大小
  48.         QStringList info = QString(buf).split("#");
  49.         if(info.front() == "FILE_SIZE")
  50.         {
  51.              //获取到图片文件的大小
  52.              this->src_size = info.back().toInt();
  53.              this->flag = 1;
  54.         }
  55.     }
  56.     else if(this->flag == 1) //开始接收真正的图像数据
  57.     {
  58.          QByteArray data = this->client->readAll();
  59.          //保存下来
  60.          this->src_data.append(data);
  61.          if(this->src_data.size() == this->src_size)
  62.          {
  63.               //下载成功
  64.               QPixmap pix;
  65.               pix.loadFromData(this->src_data);
  66.               this->ui->label->setPixmap(pix.scaled(this->ui->label->size()));
  67.               this->flag = 0;
  68.               this->src_size = 0;
  69.               this->src_data.clear();
  70.          }
  71.     }
  72. }
  73. //给客户端写数据
  74. void Widget::on_send_btn_clicked()
  75. {
  76.     //获取到需要发送给客户端的数据
  77.     QString data = this->ui->send_edit->text();
  78.     //发送给客户端
  79.     this->client->write(data.toUtf8());
  80.     //清空输入框
  81.     this->ui->send_edit->clear();
  82. }
复制代码
3-3、widget.ui

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表