Creating thread for receiving data using qudpsocket
-
Regarding creation of a thread, here is some notes I added in another post to help you get started:
@
/* QThread notes:
It is important to understand how QThreads work. The general procedure to using the QThreads is:- Make Object to go into thread, assign no parent
- Make thread
- Move object into thead using obj->moveToThread(thread)
- Connect a signal to a slot in the object that will instatiate the object members (if required)
- Start the thread: thread->start()
Now once the object receives the signal to instantiate its members they will be
instantiated within the thread.
Note: if you call an object method directly from outside the thread (or from another thread)
which instatiates object members, then these objects will infact be created from outside the
thread, and you may get warnings saying things like:
"timers cannot be started from another thread"
i.e. member functions are not thread-safe.// Good example:
MyObj myObj = new MyObj(0); // 0 = no parent
QThread* thread = new QThread;
myObj->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), myObj, SLOT(run()));
thread->start();// Bad example:
MyObj myObj = new MyObj(0); // 0 = no parent
QThread* thread = new QThread;
myObj->moveToThread(thread);
thread->start();
myObj->run(); //BAD - anything run() instantiates will be in 'this' thread
*/
@Then for UdpSocket here is some key functions you need (just examples copied out of one of my projects for now:
@
// Create new UDP Socket.
_udpSocket = new QUdpSocket(this);// Connect the signals for the IP connections, read and write events connect(_udpSocket, SIGNAL(readyRead()), this, SLOT(rxDataEvent()), Qt::QueuedConnection); connect(_udpSocket, SIGNAL(connected()), this, SLOT(txIpConnectedEvent()), Qt::QueuedConnection); // Rx connection: check we are not already bound if (_udpSocket->state() != _udpSocket->BoundState) { // Rx not in bound state, attempt to bind _udpSocket->bind(_rxIp.address, _rxIp.port); }
@
See how you get on then if you have more specific questions post back :D
-
My objective is to receive data using thread with udpsocket
I have done like this and am getting error as mentioned belowCan any body suggest how should i do it
//main.cpp
#include <QtCore/QCoreApplication>
#include <iostream>
#include <mythread.h>
int main(int argc, char argv[])
{
QCoreApplication a(argc, argv);
MyThread mythread= new MyThread();
mythread->start();
return a.exec();
}//MyThread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <iostream>
#include <QtNetwork/QUdpSocket>
class MyThread : public QThread
{
private:
QUdpSocket *udpSocket;
public:
virtual void run();
public slots:
};
#endif // MYTHREAD_H//MyThread.cpp
#include <mythread.h>
#include <QtNetwork/QUdpSocket>
void MyThread::run()
{
std::cout<<"am in thread"<<std::endl;
udpSocket = new QUdpSocket(this);
exec();
}error:
collect2: ld returned 1 exit statusAm getting this error when udpSocket = new QUdpSocket(this); statement is given
can anyone help where am wrong nd should i solve it
[quote author="janaki" date="1372758458"]am new to threads
i don't know how to create a thread
i have create udpconnection for receiving data using thread
can any one help me in doingthank u[/quote]
-
hmm... "ld" is the linker, and that is a linker error which suggests that a library cannot be found or something like this.
So, if you remove the line "udpSocket = new QUdpSocket(this);" this works?
Which version of Qt are you using?
you could try:
@#include <QUdpSocket>@instead of
@#include <QtNetwork/QUdpSocket>@ -
Also, can you show your .pro file, you may need to add:
@QT += network@
into it... please paste the whole file :)
-
Infact I quickly tried removing "QT += network" from my project .pro file and I get the same error as you. I am 99% sure that will fix your problem.
You don't need to change "#include <QtNetwork/QUdpSocket>", but also once you specify += network you dont have to use the whole path, you can (if you want) just use "#include <QUdpSocket>"...
-
My objective is to receive data using udpsocket within a thread
I have done like thisCan any body suggest whether it is correct
This is my main.cpp
#include <QtCore/QCoreApplication>
#include <iostream>
#include <mythread.h>
#include <udpClass.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout<<"am in main"<<std::endl;
UdpClass *udpclass;
udpclass=new UdpClass();
return a.exec();
}
My UdpClass.h
#ifndef UDPCLASS_H
#define UDPCLASS_H
#include <QObject>
#include <mythread.h>
class UdpClass : public QObject
{
Q_OBJECT
public:
UdpClass();
private slots:
void update();
};
#endif // UDPCLASS_HMy UdpClass.cpp
#include <udpClass.h>
#include <mythread.h>
#include <iostream>
#include <QtNetwork/QUdpSocket>
UdpClass::UdpClass()
{
MyThread *mythread = new MyThread();
connect(mythread,SIGNAL(started()),this, SLOT(update()),Qt::QueuedConnection);
mythread->start();
}
void UdpClass::update()
{
std::cout<<"signal called"<<std::endl;
QUdpSocket *udpSocket = new QUdpSocket(this);
}Thread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <qobject.h>
#include <iostream>
#include <udpClass.h>
#include <QtNetwork/QUdpSocket>
class UdpClass;
class MyThread :public QThread
{
// Q_OBJECT
signals:
void started();
public:
void run();
};
#endif // MYTHREAD_HThread.cpp
#include <mythread.h>
#include <QtNetwork/QUdpSocket>
void MyThread::run()
{
std::cout<<"am in thread"<<std::endl;
// QUdpSocket *udpSocket = new QUdpSocket(this);
}if am udpsocket object in mythread.cpp it is throwing the error as follows
the output is like this
am in main
am in thread
QObject: cannot create children for a parent that is in different thread
(parent is QThread (0x172d980) parent's thread is QThread (0x832b68) current thread is QThread(0x172d9f0)
signal calledif i create udpsocket in the udpclass it is running as follows
am in main
am in thread
signal calledi wanted to know where should i create the udpsocket for receiving the data
do the way i have done (that is creating in the udpclass is right if so what all the statements and functions i have to use for doing so)please help me
-
Hi janaki,
Its quite difficult to read your code because you are not using the code tags so many lines are "stuck together"
Can you re-paste your code between pairs of "@"
"@"
code goes here
"@"without the quotes", then we can read it!
As to your problem use the function "currentThreadId()" to determine what object lives in which thread.
So to get your "main" thread id and qthread id in main.cpp use:
@
qDebug() << "a thread" << a.thread()->currentThreadId() << endl;
qDebug() << "udpClass thread" << udpclass->thread()->currentThreadId() << endl;
@In your "myThread" class use:
@
qDebug() << "udpSocket thread" << udpSocket->thread()->currentThreadId() << endl;
qDebug() << "this thread" << this->thread()->currentThreadId() << endl;
@You may need to instantiate the udpSocket with no parent and then move it to the thread you want:
@
QUdpSocket *udpSocket = new QUdpSocket(0);
udpSocket->moveToThread(aQThread);
@or even...
@udpSocket->moveToThread(this->thread());@
-
Why would you want to receive data in a thread?
QUpdsocket has a nice asynchronous API and can be used nicely in the main thread. So why do you need to introduce threading? A thread always has some overhead and can be avoided 9 times out of 10 when handling the task of retrieving data from the net.