my code:
Connection
::Connection(QObject *parent
){
QObject::connect(this,
SIGNAL(readyRead
()),
this,
SLOT(processReadyRead
()));
QObject::connect(this,
SIGNAL(disconnected
()),
this,
SLOT(Disconnected
()));
}
Connection::Connection(QObject *parent)
: QTcpSocket(parent)
{
QObject::connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
QObject::connect(this, SIGNAL(disconnected()), this, SLOT(Disconnected()));
}
To copy to clipboard, switch view to plain text mode
void Connection::processReadyRead()
{
char *szData;
qint16 nOutState;
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;
}
}
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();
this->waitForDisconnected(5000);
}
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);
}
To copy to clipboard, switch view to plain text mode
void Connection::Disconnected()
{
::_beep(1000,100);
::_beep(1000,100);
::_beep(1000,100);
::_beep(1000,100);
}
void Connection::Disconnected()
{
::_beep(1000,100);
::_beep(1000,100);
::_beep(1000,100);
::_beep(1000,100);
}
To copy to clipboard, switch view to plain text mode
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?
Bookmarks