如何用另一个线程监听和接收UDP的数据?

tong 2010-08-08 10:52:36
各位大虾!
用另一个线程来监听和接收UDP数据,该如何实现?
能不能写个例子,或者提供一下思路。谢谢!!!
...全文
595 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wpgdut 2012-07-26
  • 打赏
  • 举报
回复
的确如chenzhoutong说的,,我用线程的时候也会自己跳出这么一句:QThread: Destroyed while thread is still running,,,,不过程序是可以正常运行的,解决办法就是:MyThread *thread = new MyThread; thread->start(); 而不是用MyThread thread 申明就可以了。虽然不知何故,,,但是还是感谢chenzhoutong,,帮我解决了一个问题,多谢了!
wpgdut 2012-07-26
  • 打赏
  • 举报
回复
的确如chenzhoutong说的,,我用线程的时候也会自己跳出这么一句:QThread: Destroyed while thread is still running,,,,不过程序是可以正常运行的,解决办法就是:MyThread *thread = new MyThread; thread->start(); 而不是用MyThread thread 申明就可以了。虽然不知何故,,,但是还是感谢chenzhoutong,,帮我解决了一个问题,多谢了!
BillLeecn 2010-08-25
  • 打赏
  • 举报
回复
另外 QThread 中不应该使用 meta 系统( signals/slots )
Qt 官方文档是这样说的
应该是因为 QThread 对象本身属于创建它的进程,而不是属于 run() 的那个进程
lefttime 2010-08-13
  • 打赏
  • 举报
回复
退出程序时, 没有关闭监听线程所致~``
例如你可以在dialog析构中加入线程等待关闭的指令~``
...
thread.quit()
thread.wait();
...
tong 2010-08-12
  • 打赏
  • 举报
回复
我的想法也是这样:
dialog.h头文件:
#ifndef DIALOG_H
#define DIALOG_H

#include <QtGui/QDialog>
#include "thread.h"
class QLabel;
class QLineEdit;

class Dialog : public QDialog
{
Q_OBJECT

public:
Dialog(QWidget *parent = 0);
~Dialog();

private slots:
void set();
private:
QLabel *label;
QLineEdit *t;
QPushButton *btn;
Thread thread;
};

class Temp
{
public:
static QString str;
};


#endif // DIALOG_H

thread.h头文件:
#ifndef THREAD_H
#define THREAD_H
#include <QThread>
#include <QtGui>
#include <QUdpSocket>
class Thread : public QThread
{
Q_OBJECT

public:
Thread();

protected:
void run();
signals:
void ok();

private slots:
void processPendingDatagrams();

private:
QUdpSocket *udpSocket;
};

#endif // THREAD_H

dialog.cpp源文件:
#include "dialog.h"
#include <QtGui>

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
label=new QLabel(tr("现在时间:"));
t=new QLineEdit;
btn=new QPushButton(tr("close"));

connect(&thread,SIGNAL(ok()),this,SLOT(set()));
connect(btn,SIGNAL(clicked()),this,SLOT(close()));
QHBoxLayout *h=new QHBoxLayout;
h->addWidget(label);
h->addWidget(t);
QVBoxLayout *v=new QVBoxLayout;
v->addLayout(h);
v->addWidget(btn);
setLayout(v);
resize(200,60);
this->setWindowTitle(tr("时钟"));

}

Dialog::~Dialog()
{

}

void Dialog::set()
{
t->setText(Temp::str);
}

thread.cpp源文件:
#include "thread.h"
#include "dialog.h"
#include <QObject>
Thread::Thread()
{
start();
}

void Thread::run()
{
udpSocket=new QUdpSocket(0);
udpSocket->bind(5824);
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));
exec();

}

void Thread::processPendingDatagrams()
{

QByteArray datagram;

do {
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
} while (udpSocket->hasPendingDatagrams());

QDateTime dateTime;
//QString d;

QDataStream in(&datagram, QIODevice::ReadOnly);
in.setVersion(QDataStream::Qt_4_3);
in >> dateTime ;

Temp::str=dateTime.time().toString();
emit ok();

}

mian.cpp源文件:
#include <QtGui/QApplication>
#include "dialog.h"
#include <QtCore/QTextCodec>
#include "thread.h"
QString Temp::str="123";
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8"));
Dialog w;
w.show();

return a.exec();
}

运行是可以运行,也能接收数据。
不过,关闭窗口时,会发生这个提示:QThread: Destroyed while thread is still running
如果把thread声明为指针,然后使用new:thread=new Thread,这样就不会有这个提示.
对于Qt的内存管理机制还不是很清楚,搞不清楚为什么会这样,希望那位高手指点一下!!!
lefttime 2010-08-09
  • 打赏
  • 举报
回复
以QThread为例, 在run()中创建一个QUdpSocket来监听指定的IP和端口, 将一些信号如
readyRead(); 、 bytesWritten(qint64 bytes)...连接到该QThread中处理不就得啦~``

ProgrammerNO1 2010-08-09
  • 打赏
  • 举报
回复
网上没例子??

16,199

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧