当前位置:网站首页>QTcpServer. QTcpSocket. Differences between qudpsockets
QTcpServer. QTcpSocket. Differences between qudpsockets
2022-06-13 10:24:00 【Just a little bit】
1. QTcpServer
Gu Mingsi means , Just do it Tcp For server ,Qt It's all encapsulated , Just call it directly
Usage method :new An object , Binding address and port , Then through the signal and slot , Handle incoming connections
m_server = new QTcpServer();
// Listening address and port number
m_server->listen(QHostAddress::Any, 8888);
connect(m_server, &QTcpServer::newConnection, this, &myServer::handleSocket);Processing function
void myServer::handleSocket()
{
QTcpSocket* socket = m_server->nextPendingConnection();
......
}Write data through write that will do

Read data through readAll
QByteArray messageByte = socket->readAll();2. QTcpSocket The connection is reliable , It's one-on-one
QTcpSocket The connection is reliable , It's one-on-one
Usage method :
1.new One QTcpSocket
m_Tcpsocket = new QTcpSocket();
2. Connect to the specified ip And port
m_Tcpsocket->connectToHost("127.0.0.1", 8888);3. Just wait for the connection
if(m_Tcpsocket->waitForConnected(3000))
{
ui->textBrowser->append(" Successfully connected to server !");
connect(m_Tcpsocket, &QTcpSocket::readyRead,[=]()
{
ui->textBrowser->append(m_Tcpsocket->readAll());
});
connect(m_UdpSocket, &QUdpSocket::readyRead, this, &Dialog::receive);
}3. QUcpSocket
QUcpSocket Your connection is unreliable , It is one to many or many to many , It's very efficient , The packet loss rate is also high
Usage method :new One QUdpSocket, If you just write data to send , If you don't read the data , No use bind() Method to bind the address and port
m_udpSocket = new QUdpSocket();Binding port , When reading data, you need to specify the port , And address
m_UdpSocket->bind(5555, QUdpSocket::ShareAddress);When writing data , Designated port , Generally, it is sent to the designated port number by broadcasting
m_udpSocket->writeDatagram(message.toLatin1().data(), message.size(),
QHostAddress::Broadcast, 5555);When reading data , Read the data sent by binding the port number , Can't take QTcpSocket How to read data (readAll) Reading data , You can't read it
Use the following methods :
connect(m_UdpSocket, &QUdpSocket::readyRead, this, &Dialog::receive);
void Dialog::receive()
{
while(m_UdpSocket->hasPendingDatagrams()) // Judge udpSocket Is there any data waiting to be read
{
QByteArray datagram;
datagram.resize(m_UdpSocket->pendingDatagramSize());// Resize
m_UdpSocket->readDatagram(datagram.data(), datagram.size());// Read the first datagram The length of
QString msg=datagram.data();
ui->textBrowser->append(msg);
}
}边栏推荐
- Analysis and implementation of variable parameters in C language
- Node red series (25): integrate Gaode map and realize 3D map and track playback
- PyTorch基础(二)-- 张量与梯度
- Docker deployment MySQL
- Knowledge points of silicon steel sheet
- LeetCode 2016. Maximum difference between incremental elements
- MySQL中redo日志和undo日志简述
- Cynthia project defect management system
- 23. Lottery
- About the problem of database: it can't be found after repeated inspection
猜你喜欢

Cynthia project defect management system

Node-RED系列(二四):在Node-RED中使用mysql节点实现数据库的增删改查

SQL Server创建windows登录账户找不到用户或组
![[poj1845] sumdiv [number theory]](/img/83/eb34f6be27d6c7735f923adb23359b.jpg)
[poj1845] sumdiv [number theory]

五分钟内编写Pytorch模型

Thingsboard tutorial (21): save data after processing using message types and data processing nodes

Install Kubernetes 1.24

MySQL到底怎么优化?

C 11 new feature: static abstract members in interfaces

逐向双碳:东数西算中的绿色需求与竞争焦点
随机推荐
一文读懂简单查询代价估算【这次高斯不是数学家】
Install Kubernetes 1.24
June training (day 13) - two way linked list
C language structure
架构师必备:系统容量现状checklist
简述请求过程
Node red series (27): instructions for S7 node of the extension node
虚拟机内存结构简述
Double carbon in every direction: green demand and competition focus in the calculation from the east to the West
类文件结构和类加载过程执行引擎简述
六月集训(第13天) —— 双向链表
周末赠书:Power BI数据可视化实战
Oracle custom data type question
【ELM分类】基于粒子群优化卷积神经网络CNN结合极限学习机ELM实现数据分类附matlab代码
中国SaaS产业全景图谱
低代码开发一个基础模块
实战模拟│企业微信机器人实时报错预警
架构师必备:系统容量现状checklist
The rise of cloud computing enterprises and the shaking of Oracle database market dominance
China SaaS industry panorama