• 杂乱的UDPsocket


    void UdpClient::dataReceived()
    {
    while(udpSocket->hasPendingDatagrams())
    {
    QByteArray datagram;
    datagram.resize(udpSocket->pendingDatagramSize());

    udpSocket->readDatagram(datagram.data(),datagram.size());
    QString msg=datagram.data();
    ReceiveTextEdit->insertPlainText(msg);
    }
    }

    udpSocket = new QUdpSocket(this);
    connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));

    bool result=udpSocket->bind(port);
    if(!result)
    {
    QMessageBox::information(this,tr("error"),tr("udp socket create error!"));
    return;
    }
    port =5555;

    int port;
    QUdpSocket *udpSocket;
    udpSocket = new QUdpSocket(this);
    if((length=udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))!=msg.length())

    直接发送和接收结构体,例如:
    struct A {...};
    struct A objectA;

    发送的时候:
    tcpSocket->write((char *)&objectA, sizeof(objectA));
    接收的时候:
    struct A objectB;
    tcpSocket->read((char *)&objectA, sizeof(objectA));


    udpSocket = new QUdpSocket(this);
    QByteArray datagram = "blog.const.net.cn";
    udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 45454);
    udpSocket->bind(QHostAddress("192.168.1.104"), 45454);

    UDP客户端广播
    QUdpSocket *udpsocket1 = new QUdpSocket(this);
    QByteArray datagram = "http://blog.const.net.cn";
    udpsocket1->writeDatagram(datagram.data(),datagram.size(), QHostAddress::Broadcast,ui->spinBox->text().toInt());


    UDP客户端发送到指定IP
    QUdpSocket *udpsocket1 = new QUdpSocket(this);
    QByteArray datagram = "http://blog.const.net.cn";
    udpsocket1->writeDatagram(datagram.data(),datagram.size(),
    QHostAddress("192.168.1.104"),ui->spinBox->text().toInt());

    收数据
    connect(udpsocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    while (udpsocket->hasPendingDatagrams())
    {
    QByteArray datagram;
    datagram.resize(udpsocket->pendingDatagramSize());
    QHostAddress sender;
    quint16 senderPort;
    udpsocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
    qDebug()<<datagram.data()<<datagram.size()<<senderPort<<sender.toString(); //processTheDatagram(datagram);
    }

  • 相关阅读:
    python__基础数据类型
    python基础之迭代器生成装饰器
    cat命令查看文件显示行号
    VM虚拟机安装ESD版Win10
    CentOS 8修改系统语言为中文且立即生效
    CentOS 8下yum安装LAMP环境
    完美解决CentOS8 yum安装AppStream报错,更新yum后无法makecache的问题
    鬼灭之刃高清电脑桌面壁纸
    记CentOS 8 yum 报AppStream Error排错
    CentOS 8 重启网卡命令
  • 原文地址:https://www.cnblogs.com/IamQtCreator/p/4609067.html
Copyright © 2020-2023  润新知