当前位置:网站首页>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);
}
}
边栏推荐
- Go zero microservice Practice Series (III. API definition and table structure design)
- 五分钟内编写Pytorch模型
- Node red series (24): use MySQL nodes in node red to add, delete, modify and query databases
- Information document management and configuration management
- Cynthia项目缺陷管理系统
- [51nod 3062] n queen problem V2 [bit operation DFS]
- 了解RPC和REST
- ADG备库MRP0状态WAIT_FOR_GAP
- 一篇文章读懂:Spark运行模式
- 虚拟机内存结构简述
猜你喜欢
技术管理进阶——管理者可以使用哪些管理工具
[51nod p2653] interval XOR [bit operation]
Cynthia project defect management system
vivo大规模 Kubernetes 集群自动化运维实践
Idea remote debugging jar submitted by spark submit
Leetcode (question 15) - sum of three numbers
Pxxx local socket communication
Thingsboard tutorial (21): save data after processing using message types and data processing nodes
go-zero微服务实战系列(三、API定义和表结构设计)
Win7 cannot be remote desktop problem
随机推荐
周末赠书:Power BI数据可视化实战
C 11 new feature: static abstract members in interfaces
多线程 从UE4的无锁队列开始 (线程安全)
上周热点回顾(6.6-6.12)
冗余码题型--后面加0的区别
[bearing fault decomposition] ITD bearing fault signal decomposition based on MATLAB [including Matlab source code 1871]
二叉树简介
大O记法解释
go-zero微服务实战系列(三、API定义和表结构设计)
Index query list injects MySQL and executes Oracle
index查list 注入的是mysql 执行的是oracle
Idea remote debugging jar submitted by spark submit
36 krypton launched | built domestic actuarial forecasting engine and other products, and "Shenzhen light technology" completed three consecutive rounds of financing
逐向双碳:东数西算中的绿色需求与竞争焦点
Record several interesting XSS vulnerability discoveries
About the problem of database: it can't be found after repeated inspection
低代码开发一个基础模块
中国SaaS产业全景图谱
Système de gestion des défauts du projet Cynthia
Wait for someone with "source" | openharmony growth plan student challenge registration to start