Qt服务器端与客户端交互

devtools/2024/9/22 20:11:56/

Qt客户端与服务器端交互第一步引入network

第一步引入network后继续编程首先界面设计

创建server和socket

引入QTcpServer,QTcpSocket

MainWindow.h代码如下

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();QTcpServer *tcpserver;QTcpSocket *tcpsocket;QList<QTcpSocket*> listClient;void onButtonClicked();void onButtonClicked1();
private slots:void on_connectbt_clicked();void on_disconnectbt_clicked();void on_sendbt_clicked();void newConnection_Slot();void readyRead_Slot();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

然后绑定IP和端口号:

tcpserver->listen(QHostAddress::Any,ui->portnum->text().toUInt());//监听端口号
    qDebug()<<"服务器已经打开;端口号是"<<ui->portnum->text();
tcpserver=new QTcpServer(this);
    tcpsocket=new QTcpSocket(this);
    connect(tcpserver,SIGNAL(newConnection()),this,SLOT(newConnection_Slot()));

服务器端代码MainWindow.cpp如下,包括socket连接,读,写

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QComboBox>
#include<QLineEdit>
#include<QPushButton>
#include<QTcpSocket>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);tcpserver=new QTcpServer(this);tcpsocket=new QTcpSocket(this);connect(tcpserver,SIGNAL(newConnection()),this,SLOT(newConnection_Slot()));connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on_sendbt_clicked);connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::on_connectbt_clicked);connect(ui->pushButton_3, &QPushButton::clicked, this, &MainWindow::on_disconnectbt_clicked);connect(ui->pushButton_4, &QPushButton::clicked, this, &MainWindow::onButtonClicked);connect(ui->pushButton_5, &QPushButton::clicked, this, &MainWindow::onButtonClicked1);//connect(ui->pushButton_3, &QPushButton::clicked, this, &MainWindow::on_disconnectbt_clicked);
}
//单发命令void MainWindow::newConnection_Slot(){tcpsocket=tcpserver->nextPendingConnection();listClient.append(tcpsocket);qDebug() << listClient.count();//qDebug() << tcpsocket;ui->comboBox->addItem("客户端"+QString::number(listClient.count())+"号");//for(int i=0;i<listClient.count();i++)//{//tcpsocket=listClient.at(i);qDebug()<<tcpsocket;//connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readyRead_Slot()));connect(tcpsocket, &QTcpSocket::readyRead,[=](){//从通信套接字中取出内容QString buf;buf=tcpsocket->readAll();ui->receivewd->append(buf);});//}
}
void MainWindow::readyRead_Slot()
{//第四步:读取套接字的内容//从socket中读出数据QString buf;buf=tcpsocket->readAll();ui->receivewd->append(buf);/*或QByteArray baArray = tcpsocket->readAll();QString sMsg = baArray;ui->receivewd->appendPlainText(receivewd);*/
}
void MainWindow::on_connectbt_clicked()//连接服务器
{//第二部步:listen------监听是否有新的连接进来tcpserver->listen(QHostAddress::Any,ui->portnum->text().toUInt());//监听端口号qDebug()<<"服务器已经打开;端口号是"<<ui->portnum->text();
}
void MainWindow::on_disconnectbt_clicked()//关闭服务器
{tcpserver->close();
}void MainWindow::on_sendbt_clicked()//发送信息
{
//    toLatin1()
//    tcpsocket->write(ui->sendwd->text().toLatin1());
//    tcpsocket->write(ui->sendwd->text().toLocal8Bit(),ui->sendwd->text().length());for(int i=0;i<listClient.count();i++){tcpsocket=listClient.at(i);tcpsocket->write(ui->sendwd->text().toLocal8Bit().data());}
}
MainWindow::~MainWindow()
{delete ui;
}

客户端代码如下:客户端界面

MainWindow.h文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();QTcpSocket *tcpsocket;
private slots:void on_openclient_clicked();void connected_SLOT();void readyRead_Slot();void on_closeclient_clicked();void on_sent_clicked();
private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

客户端代码MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);tcpsocket=new QTcpSocket(this);connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on_sent_clicked);connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::on_openclient_clicked);connect(ui->pushButton_3, &QPushButton::clicked, this, &MainWindow::on_closeclient_clicked);
}MainWindow::~MainWindow()
{delete ui;
}
void MainWindow::connected_SLOT()
{QObject::connect(tcpsocket, &QTcpSocket::readyRead, this, &MainWindow::readyRead_Slot);
//    connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readyRead_Slot()));//将信号连接到槽,书写比较明确}
void MainWindow::readyRead_Slot()//定义接收信号的槽
{QString buf;buf=tcpsocket->readAll();int num=buf.toInt();switch(num){case 1:break;}ui->receivewd->append(buf);//接收由tcp发送过来的信息
//    ui->receivewd->appendPlainText(buf.toUtf8());//接收由tcp发送过来的信息
}void MainWindow::on_openclient_clicked()
{//第一步:创建套接字,与服务端的IP地址和端口号连接.//注:这里的端口号和上面服务端绑定的那个端口号是一样的,别搞错了.//连接服务端tcpsocket->connectToHost(ui->ipnum->text(),ui->portnum->text().toInt());//转为无符号,连接服务器端口connect(tcpsocket,SIGNAL(connected()),this,SLOT(connected_SLOT()));qDebug() << "连接服务器端成功IP是"<<ui->ipnum->text()<<"端口号是:"<<ui->portnum->text();/*//成功连接返回true,错误返回falseif(m_tsTcpSocket->waitForConnected()){qDebug() << "connect success";}*/
}void MainWindow::on_closeclient_clicked()
{tcpsocket->close();printf("关闭客户端 ");
}void MainWindow::on_sent_clicked()
{tcpsocket->write(ui->sendwd->text().toLocal8Bit().data(),ui->sendwd->text().length());//丽丽
}

客户端与服务器端交互,主要就是连接,读和写内容。


http://www.ppmy.cn/devtools/32694.html

相关文章

使用 Docker-Compose 部署 ZooKeeper + Kafka + Kafka-UI

使用 Docker-Compose 部署 Kafka + ZooKeeper 1. 无密码验证部署1.1 启动 ZooKeeper1.2 查看 zookeeper 状态1.3 启动 Kafka1.4 Kafka 配置文件1.4 使用命令操作 Kafak 生产、消费1.4.1 创建topic1.4.2 查看某个 topic1.4.3 获取所有 topic1.4.4 删除 topic1.4.4 发送消息1.4.5…

计算机网络——Dijkstra路由算法

实验目的 实现基于 Dijkstra 算法的路由软件 实验内容 网络拓扑如图所示 实验过程 先编写开辟应该图的空间&#xff0c;然后给点映射数字&#xff0c;构建图。程序获取用户输入的学号&#xff0c;构建图中边的权值。接下来程序从用户输入获取最短路径的搜索起点&#xff0…

用vsCode开发uni-app(vue + ts)项目流程

提示:记录项目创建流程 文章目录 前言一、安装 uni-app 插件二、ts 类型校验1.安装类型声明文件2.配置 tsconfig,json三、json 注释问题四、组件引入1. 安装 uni-app2. 组件自动引入3. 配置 ts 类型五、小程序端 Pinia 持久化六、uni.request 请求封装七、请求成功提取数据和设…

华为鸿蒙系统(Huawei HarmonyOS)

华为鸿蒙系统&#xff08;华为技术有限公司开发的分布式操作系统&#xff09; 华为鸿蒙系统&#xff08;HUAWEI HarmonyOS&#xff09;&#xff0c;是华为公司在2019年8月9日于东莞举行的华为开发者大会&#xff08;HDC.2019&#xff09;上正式发布的分布式操作系统。 华为鸿蒙…

Android 安装过程三 MSG_ON_SESSION_SEALED、MSG_STREAM_VALIDATE_AND_COMMIT的处理

Android 安装过程一 界面跳转 知道&#xff0c;在InstallInstalling Activity中&#xff0c;PackageInstallerSession对象创建之后&#xff0c;接着会打开它&#xff0c;然后将安装文件进行拷贝&#xff0c;拷贝完成之后&#xff0c;会对Session对象确认。   从Session对象确…

8. Django 表单与模型

8. 表单与模型 表单是搜集用户数据信息的各种表单元素的集合, 其作用是实现网页上的数据交互, 比如用户在网站输入数据信息, 然后提交到网站服务器端进行处理(如数据录入和用户登录注册等).网页表单是Web开发的一项基本功能, Django的表单功能由Form类实现, 主要分为两种: dj…

Servlet_JSP

1.一些回顾 对于Tomcat部署中 我们有一些补充的点需要在此说明一下 1.如果我们想要查询MINEType的话 可以到TOMCAT_HOME/conf/web.xml中进行查询 里面记录了不同类型对应的MINEType 2.我们客户端发送请求数据给服务器之后 服务器会调用父类中的service方法 然后在内部决定调用…

python常用库函数

Python标准库 0内置函数模块 一部分常用的的内置函数。 函数说明abs返回一个数的绝对值&#xff0c;例如&#xff1a;abs(-1.3)会返回1.3。bin把一个整数转换成以0b开头的二进制字符串&#xff0c;例如&#xff1a;bin(123)会返回0b1111011。chr将Unicode编码转换成对应的字…