PDA

View Full Version : how to operate tcpsocket in thread again



succulent_lily
26th November 2008, 09:25
I establish a multi-thread server,but encouter some problems,after declare tcpsocket in thread::run,I want to operate the tcpsocket in slot ,such as write or read,but i couldnot.
how to resolve this problem,forgive my poor english,3x

the thread.h as follows


class FortuneThread : public QThread
{
Q_OBJECT

public:
FortuneThread(int socketDescriptor, QObject *parent);

void run();

signals:
void error(QTcpSocket::SocketError socketError);
void toserver(const QString info);

private:
int socketDescriptor;
QTcpSocket *tcpSocket;
bool stopped;
private slots:
void recfserver(const QString peeradd);
};


the thread.cpp as follows


FortuneThread::FortuneThread(int socketDescriptor, const QString &fortune,
QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor)
{

};

void FortuneThread::run()
{
tcpSocket = new QTcpSocket();

if (!((*tcpSocket).setSocketDescriptor(socketDescript or))) {
emit error((*tcpSocket).error());
return;
}

(*tcpSocket).waitForDisconnected();

}

void FortuneThread::recfserver(const QString peeradd)
{

QString text=tr("transfer success!");
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);

out << (quint16)0;
out << text;

(*tcpSocket).write(block);

};


when i debug,the error as follows

QObject cannot create children for a parent that is in a different thread.
(parent is QNativeSocketEngine,parent's thread is FortuneThread,current thread
is QThread)

caduel
26th November 2008, 10:51
i) use [code] tags
ii) with Qt signals & slots and you do not really need to put that code into a separate thread
iii) if using QThread: put your code into a separate class (not the QThread subclass!) and create that in your thread's run() function. You should not add slots/signals to the QThread subclass itself. There are some postings that elaborate on that. Search for them, please.
(The QThread belongs to the thread that created it, not to the thread it is executing itself.)

HTH

succulent_lily
27th November 2008, 01:49
i) use [code] tags
ii) with Qt signals & slots and you do not really need to put that code into a separate thread
iii) if using QThread: put your code into a separate class (not the QThread subclass!) and create that in your thread's run() function. You should not add slots/signals to the QThread subclass itself. There are some postings that elaborate on that. Search for them, please.
(The QThread belongs to the thread that created it, not to the thread it is executing itself.)

HTH

thanks for your help,but i think i don't explain my desigh explicility

firstly,I want to create a thread,and creat a socket in the thread for some client's connection.
secondly,I want the gui-mainthread to notify the created thread to send some message to the client..So I use signal/slot for it,just like i clicked a button,then emit a signal which send by gui to the created thread,it is a blocked operation.
now the slot recfserveris useful for accept the signal,then i want to operate tcpsocket in the slot.can i into the run for the operation?
could you tell me what other methods which can realize the function
thank you very much!

succulent_lily
27th November 2008, 05:57
thanks for your help,but i think i don't explain my desigh explicility

firstly,I want to create a thread,and creat a socket in the thread for some client's connection.
secondly,I want the gui-mainthread to notify the created thread to send some message to the client..So I use signal/slot for it,just like i clicked a button,then emit a signal which send by gui to the created thread,it is a blocked operation.
now the slot recfserveris useful for accept the signal,then i want to operate tcpsocket in the slot.can i into the run for the operation?
could you tell me what other methods which can realize the function
thank you very much!

i subclass Qtcpsocket,and transfer the signal to the thread ,then to the socket,the problem is solved ,thanks for your help