当前位置: 首页 > news >正文

TCP/UDP与网络调试助手通信

1.UDP通信

    //UDP{Parameterflag=1;this->udpSocket=new QUdpSocket(this);bool ok = udpSocket->bind(QHostAddress::Any, 8088); // 绑定到所有可用的网络接口上的12345端口if (!ok) {// 处理绑定失败的情况qDebug()<<"bind err";}connect(udpSocket, &QUdpSocket::readyRead, this, &MainWindow::readPendingDatagrams);this->timer=new QTimer(this);connect(timer, &QTimer::timeout, this, &MainWindow::sendDatagram);timer->start(1000);}

相对应的信号与槽函数连接,使用定时器进行不同时连接;

//UDP  发送连接对应地址和端口号void MainWindow::sendDatagram()
{Parameterflag=1;QByteArray datagram = "Hello, UDP!";QHostAddress receiver("192.168.52.1"); // 目标地址quint16 receiverPort = 9999; // 目标端口udpSocket->writeDatagram(datagram, receiver, receiverPort);qDebug() << "Sent datagram:" << datagram << "to" << receiver.toString() << ":" << receiverPort;
}

接收信息打印连接槽函数

void MainWindow::readPendingDatagrams()
{Parameterflag=1;//    qDebug() <<"qerrttip";while (udpSocket->hasPendingDatagrams()) {QByteArray datagram;datagram.resize(int(udpSocket->pendingDatagramSize()));QHostAddress sender;quint16 senderPort;//        data:接收到发送端的 功能码。//        maxSize:功能码长度//        sender(可选):发送方的IP地址。//        senderPort(可选):发送方的端口号。udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);qDebug() << "Received datagram:" << datagram << "from" << sender.toString() << ":" << senderPort;}
}

2. TCP服务器端连接

    //TCP服务器//    {//        tcpServer=new QTcpServer(this);//        connect(tcpServer, &QTcpServer::newConnection,

http://www.mrgr.cn/news/50609.html

相关文章:

  • Cortex-A7:如何切换ARM和Thumb状态
  • k8s worker 节点关机 sts 管理的 pod 无法迁移
  • kubernetes--配置管理和持久化存储
  • CentOS 7上安装MySQL客户端并进行配置
  • 景区展览馆客流统计系统实时显示在场人数
  • 深度学习实战94-基于图卷积神经网络GCN模型的搭建以及在金融领域的场景
  • 人工智能AI与机器学习ML基础入门
  • Linux 内核中USB鼠标枚举失败问题总结
  • 深入探索阿里巴巴关键字搜索商品API的返回值
  • 什麼是高速HTTP代理?
  • 数据库达人必备:MySQL面试精华1/100,解锁职场新高度
  • lwip多线程使用说明,注意事项
  • SQL 注入漏洞 - 学习手册
  • CM-connected
  • Linux初识:基本指令
  • Python爬虫必备的8大技巧,学习爬虫技巧必看!
  • 提升 C++ std::string 操作效率:善用 pop_back()
  • Qt-界面优化盒子模型(71)
  • 意外断电 导致docker 部署禅道 的mariadb 启动报错
  • FPGA中的双向信号inout与三态门