PDA

View Full Version : QTcpServer + QThread



Alex Snet
14th April 2009, 20:04
Hi 2 all.
I have a small problem. Now I make my personal HTTP server and I have some trouble with developing.

There are sources of it: http://alexsnet.ru/temp/HTTPServ.zip

When I compile - all is good, but when I connect to it debugger writes:


QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x5716b8), parent's thread is QThread(0x509c80), current thread is sockThread(0x5716b0)



How I can solve this problem?

Alex Snet
14th April 2009, 21:03
Problem solved. But I have another one...

How to explode (as in php) QByteArray by "\n" (endl);



sockThread::sockThread(int socketDescriptor, QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor)
{
}

void sockThread::run()
{
QTcpSocket tcpSocket;
if (! tcpSocket.setSocketDescriptor(socketDescriptor) ) {
emit error(tcpSocket.error());
return;
}

tcpSocket.waitForReadyRead();

QByteArray request = tcpSocket.readAll(); // HTTP request headers from client browser

QByteArray response;
response.append( request );

tcpSocket.write( response );
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}


And also one question: can I recieve request headers by line to line?

wysota
14th April 2009, 21:46
1. QString::split().

2. No, a TCP connection is a stream of bytes. But you can buffer the incoming data and read it line by line as next lines are ready.