yangli2007 |
2019-10-23 16:12 |
多线程中UDP无法收到数据是什么原因呢?
我写了个简单的多线程UDP的发送接收测试代码,数据能发出去,但是不能收到数据,各位大神,能帮我解决下嘛? #include "mythread.h"#include<QDebug> Mythread::Mythread(QObject *parent) : QThread(parent){}void Mythread::readmsg(){ qDebug()<<"--Read--"; while (udpsock->hasPendingDatagrams()) { QByteArray array; array.resize(udpsock->bytesAvailable()); //将接收数据的array设置成为要接收数据的大小 QHostAddress recvaddress; quint16 port; udpsock->readDatagram(array.data(),array.size(),&recvaddress,&port); //读取数据 qDebug()<<array;}}void Mythread::run(){ qDebug()<<"--output----"; udpsock = new QUdpSocket(); sendaddrees.setAddress("192.168.100.101"); udpsock->bind(QHostAddress::AnyIPv4,50000); connect(udpsock,SIGNAL(readyRead()),this,SLOT(readmsg()),Qt::DirectConnection); QString str="@00813200100875210\r\n";
udpsock->writeDatagram(str.toUtf8(),sendaddrees,50000);}-------------------------------------------------------------------------------------------------------- #include "mainwindow.h"#include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this);} MainWindow::~MainWindow(){ delete ui;} void MainWindow::on_pushButton_clicked(){ mytest =new Mythread(); mytest->start();}
|
|