查看完整版本: [-- 多线程中UDP无法收到数据是什么原因呢? --]

QTCN开发网 -> Qt基础编程 -> 多线程中UDP无法收到数据是什么原因呢? [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

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();}


yangli2007 2019-10-23 16:16
[attachment=20979][attachment=20978]

uidab 2019-10-24 08:21
一看你就没有理解QThread继承的方式

圣域天子 2019-10-24 09:18
run() 里面是不断执行的代码。


查看完整版本: [-- 多线程中UDP无法收到数据是什么原因呢? --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled