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

相关内容

热门资讯

透视app!aapoker怎么... 透视app!aapoker怎么设置抽水(透视)可以开辅助器(一贯是真的有挂)一、aapoker怎么设...
透视教程!哈糖大菠萝软件下载,... 透视教程!哈糖大菠萝软件下载,wepoker脚本(透视)原先有挂(科技教程)透视教程!哈糖大菠萝软件...
透视总结(WPK)确实有挂(透... 透视总结(WPK)确实有挂(透视)wpk辅助软件(攻略方法);1、wpk辅助软件透视辅助简单,wpk...
透视挂!pokernow辅助工... 透视挂!pokernow辅助工具,哈糖大菠萝怎么挂,真是是真的有挂(攻略教程)1、任何哈糖大菠萝怎么...
透视辅助!aapoker辅助工... 透视辅助!aapoker辅助工具安全吗(透视)发牌逻辑(总是有挂)1、超多福利:超高返利,海量正版游...
透视私人局!hh poker插... 透视私人局!hh poker插件下载,约局吧德州真的有透视挂吗(透视)起初存在有挂(技巧教程)1、构...
透视透视(WPK)切实真的有挂... 透视透视(WPK)切实真的有挂(透视)wpk有作弊吗(攻略方法)1)wpk有作弊吗辅助挂:进一步探索...
透视攻略!epoker有透视吗... 透视攻略!epoker有透视吗,拱趴大菠萝机器人,其实有挂(新2025教程);1、下载好拱趴大菠萝机...
透视科技!aapoker公共底... 透视科技!aapoker公共底牌(透视)辅助器是真的(竟然是有挂)1、每一步都需要思考,不同水平的挑...
透视好友!wepoker有没有... 透视好友!wepoker有没有挂,hhpoker辅助软件(透视)起初是真的有挂(解密教程)1、hhp...