Hi,
finally the problem is solved thanks to bender86 last post. This is the final code (if it may be useful to somebody else):
{
...
private slots:
void startUpdater();
void readIncomingData();
void newConnectionSlot();
...
private:
...
};
{
myProcess = 0;
...
if (!tcpServer->listen()) {
tr("Unable to start the server: %1.")
.arg(tcpServer->errorString()));
close();
return;
}
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
...
connect(pushButton_1, SIGNAL(clicked()), this, SLOT(startUpdater()));
...
}
void MainApp::startUpdater()
{
args << port.toString();
...
myProcess->startDetached(program, args);
}
void MainApp::newConnectionSlot()
{
while(tcpServer->hasPendingConnections()) {
tcpSocket = tcpServer->nextPendingConnection();
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readIncomingData()));
}
}
void MainApp::readIncomingData()
{
...
in >> string;
// Quit application.
}
}
class MainApp: public QDialog
{
...
private slots:
void startUpdater();
void readIncomingData();
void newConnectionSlot();
...
private:
QProcess *myProcess;
QTcpServer *tcpServer;
QTcpSocket *tcpSocket;
...
};
MainApp::MainApp(QWidget *parent)
:QDialog(parent)
{
myProcess = 0;
...
tcpServer = new QTcpServer(this);
if (!tcpServer->listen()) {
QMessageBox::critical(this, tr("Server"),
tr("Unable to start the server: %1.")
.arg(tcpServer->errorString()));
close();
return;
}
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
...
connect(pushButton_1, SIGNAL(clicked()), this, SLOT(startUpdater()));
...
}
void MainApp::startUpdater()
{
QString program = "updater";
QStringList args;
args << port.toString();
...
myProcess = new QProcess(this);
myProcess->startDetached(program, args);
}
void MainApp::newConnectionSlot()
{
while(tcpServer->hasPendingConnections()) {
tcpSocket = tcpServer->nextPendingConnection();
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readIncomingData()));
}
}
void MainApp::readIncomingData()
{
...
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
QString string;
in >> string;
if(string == QString("quit")) {
// Quit application.
QCoreApplication::instance()->quit();
}
}
To copy to clipboard, switch view to plain text mode
{
...
private slots:
void sendQuitCommand();
...
private:
...
};
Updater
::Updater(char *p,
QWidget *parent
){
port = p;
...
tcpSocket->connectToHost("localhost", port.toInt());
...
}
void Updater::sendQuitCommand()
{
tcpSocket->disconnectFromHost();
}
class Updater: public QDialog
{
...
private slots:
void sendQuitCommand();
...
private:
QTcpSocket *tcpSocket;
...
};
Updater::Updater(char *p, QWidget *parent)
:QDialog(parent)
{
port = p;
...
tcpSocket = new QTcpSocket(this);
tcpSocket->connectToHost("localhost", port.toInt());
...
}
void Updater::sendQuitCommand()
{
QDataStream out(tcpSocket);
out.setVersion(QDataStream::Qt_4_0);
out << QString("quit");
tcpSocket->disconnectFromHost();
}
To copy to clipboard, switch view to plain text mode
Regards
Bookmarks