PDA

View Full Version : QProcess+TcpSocket



Fastman
13th November 2007, 07:46
my code:


Connection::Connection(QObject *parent)
: QTcpSocket(parent)
{
QObject::connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
QObject::connect(this, SIGNAL(disconnected()), this, SLOT(Disconnected()));
}


void Connection::processReadyRead()
{

char *szData;
QProcess process;
qint16 nOutState;

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);


if( this->isReadable())
{
qint64 qnSize = this->bytesAvailable();
if( qnSize > 0)
{
szData = new char [qnSize+sizeof(szData)];
qint64 qnReadSize = this->read(szData, qnSize);
szData[qnReadSize]=NULL;
}
}

QString cData(szData);
QStringList cListCmd;
QString cmd;

cListCmd = cData.split(QRegExp("\\s+"));
//For example:
process.start("NOTEPAD.EXE");
nOutState = process.waitForFinished(-1);
delete[] szData;


block = NULL;
block.append(QString::number(nOutState));
this->write(block);


this->disconnectFromHost();
if(this->state() == QAbstractSocket::ConnectedState)
this->waitForDisconnected(5000);
}



void Connection::Disconnected()
{

::_beep(1000,100);
::_beep(1000,100);
::_beep(1000,100);
::_beep(1000,100);

}



QProcess starts the program and should expect its end. The client at this time holds connection and expects end of the program. But at switching-off of the client - I should catch that connection is lost and it is necessary to finish the carried out program. But at me slot Disconnected () works only after closing the program even if the client was disconnected. How to catch the message of separation of the client?

jpn
13th November 2007, 08:01
my code:



this->disconnectFromHost();
if(this->state() == QAbstractSocket::ConnectedState)
this->waitForDisconnected(5000);
}

I would expect the socket more likely to be in QAbstractSocket::ClosingState..

Fastman
13th November 2007, 08:11
Thx. replase this line. But the problem has remained.

wysota
13th November 2007, 09:17
Why don't you try the event oriented approach? Connect appropriate signals to slots and wait for them to be fired. What's the point of this waitForDisconnected() loop? If you have a slot that will fire after the disconnected signal is emited, I don't see the point to wait for it again in a loop.

Fastman
13th November 2007, 09:58
Why don't you try the event oriented approach? Connect appropriate signals to slots and wait for them to be fired. What's the point of this waitForDisconnected() loop? If you have a slot that will fire after the disconnected signal is emited, I don't see the point to wait for it again in a loop.


Thanks for the help! I have made as follows:


...
...
...
while(!(this->state()==QAbstractSocket::UnconnectedState))
{
QCoreApplication::processEvents();

...
}
...
...
...


and all works :)

wysota
13th November 2007, 10:06
But again, what is the point of having such a loop? The code above is the same as the previous one, only that you change the condition.

Fastman
13th November 2007, 10:13
But again, what is the point of having such a loop? The code above is the same as the previous one, only that you change the condition.

I have simply shown all code. Now it is traced there is a connection with the client or not... If connection will be lost before performance of my program (coding of video of a file) that coding will be interrupted, if coding will be normally completed and QProcess will return normal end of the program - the client will receive the answer that all ok:) Thanks for the help:) At you a unique forum where it is possible to receive the fast and exact answer to a question:)

wysota
13th November 2007, 10:27
Don't you think listening for the QProcess::finished() signal or checking QProcess::state() is a better way to do it?

Fastman
13th November 2007, 10:45
Don't you think listening for the QProcess::finished() signal or checking QProcess::state() is a better way to do it?

yes i'am use QProcess::finished() and if get signal finished() send OK state to client.

wysota
13th November 2007, 10:49
So what's the point of monitoring the socket? You don't even need the socket at all... you can use stdin/stdout to transfer all the data you need.

Fastman
13th November 2007, 11:00
So what's the point of monitoring the socket? You don't even need the socket at all... you can use stdin/stdout to transfer all the data you need.

Each connection of the client and performance of process is processed in a separate stream:


Server::Server(QObject *parent):QTcpServer(parent)
{

if (!listen(QHostAddress::Any,1715)) {
return;
}

}

Server::~Server()
{

}

void Server::incomingConnection(int socketDescriptor)
{
FortuneThread *thread = new FortuneThread(socketDescriptor,this);
thread->start();
}



void FortuneThread::run()
{
Connection connection;
if(!connection.setSocketDescriptor(socketDescripto r))
{
emit error(connection.error());
return;
}
connect(&connection, SIGNAL(disconnected()), this, SLOT(deleteConnection()));
exec();
}

wysota
13th November 2007, 11:08
I don't see a relation to what the busy loop here... If a connection drops, you'll get an error trying to write to the socket, I don't see what waitForDisconnected() is needed for... And processing application events from within a worker thread (because I assume it is the thread that calls processEvents()) seems suspicious.

Fastman
13th November 2007, 11:12
I shall explain. The client has an adjustment - how many simultaneously streams to start. The client considers quantity simultaneously started streams and holds connection until then while process of coding of a file will be completed or there will be a mistake. The server is located on unix, and the client on Windows. Earlier this system on C has been written. But I have decided to try with studying QT to copy this part on QT/C ++

Fastman
13th November 2007, 11:16
I don't see a relation to what the busy loop here... If a connection drops, you'll get an error trying to write to the socket, I don't see what waitForDisconnected() is needed for... And processing application events from within a worker thread (because I assume it is the thread that calls processEvents()) seems suspicious.


if i'am comment QCoreApplication::processEvents();
this->state() on that does not change

wysota
13th November 2007, 11:34
I shall explain. The client has an adjustment - how many simultaneously streams to start. The client considers quantity simultaneously started streams and holds connection until then while process of coding of a file will be completed or there will be a mistake. The server is located on unix, and the client on Windows. Earlier this system on C has been written. But I have decided to try with studying QT to copy this part on QT/C ++

But what does waitForDisconnected() do for you? I don't see any point in using it... You have a slot connected to the disconnected() signal, so why wait for the signal in a busy loop?


if i'am comment QCoreApplication::processEvents();
this->state() on that does not change

This is obvious, because timers won't fire and the state won't be able to change.

Maybe processEvents() is wise enough to call the appropriate event queue based on the current thread id and thanks to that your application doesn't crash...