Thank you for the suggestion.
I do however still have some issues to get the inter process communication to work. This is how I handle it. The application is launched succesfully, but it does not seem to receive any data. I use a QFile constructed with stdin in my server to capture the input from the startserver program.
The main.cpp of my startserver program:
#include <QCoreApplication>
#include "startserver.h"
int main(int argc, char *argv[])
{
Startserver w;
return a.exec();
}
#include <QCoreApplication>
#include "startserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Startserver w;
return a.exec();
}
To copy to clipboard, switch view to plain text mode
The startserver class:
#include <QDir>
#include <QFile>
#include <QProcess>
#include <QString>
#include <QStringList>
{
Q_OBJECT
public:
~Startserver();
private slots:
void serverFinished
(int,
QProcess::ExitStatus);
private:
};
{
process_->start(appName);
process_->waitForStarted();
connect(process_,
SIGNAL(finished
(int,
QProcess::ExitStatus)),
this,
SLOT(serverFinished
(int,
QProcess::ExitStatus)));
//Start a timer, which sends data to the server each second
timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(sendData()));
timer->start();
}
Startserver::~Startserver()
{
}
void Startserver
::serverFinished(int exitCode,
QProcess::ExitStatus exitStatus
) {
exit(exitCode);;
}
void Startserver::sendData()
{
//Send some data to server
//This slot is effictively called each second !!
out << (quint32)0;
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
//Data is sent to stream each time, since bytesToWrite returns same amount each time
process_->write(block);
while (process_->bytesToWrite() > 0)
{
if (!process_->waitForBytesWritten())
break;
}
}
#include <QDir>
#include <QFile>
#include <QProcess>
#include <QString>
#include <QStringList>
class Startserver : public QObject
{
Q_OBJECT
public:
Startserver(QObject *parent = 0);
~Startserver();
private slots:
void serverFinished(int, QProcess::ExitStatus);
private:
QProcess* process_;
};
Startserver::Startserver(QObject* parent) : QObject(parent)
{
QString appName = QString("myapplication.exe");
process_ = new QProcess(this);
process_->start(appName);
process_->waitForStarted();
connect(process_, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(serverFinished(int, QProcess::ExitStatus)));
//Start a timer, which sends data to the server each second
QTimer* timer = new QTimer(this);
timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(sendData()));
timer->start();
}
Startserver::~Startserver()
{
}
void Startserver::serverFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
exit(exitCode);;
}
void Startserver::sendData()
{
//Send some data to server
//This slot is effictively called each second !!
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_3);
out << (quint32)0;
out << QString("Hello server");
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
//Data is sent to stream each time, since bytesToWrite returns same amount each time
process_->write(block);
while (process_->bytesToWrite() > 0)
{
if (!process_->waitForBytesWritten())
break;
}
}
To copy to clipboard, switch view to plain text mode
And here is what happens in the main class of my server program. The code is a lot bigger, so I only show what I think to be the relevant parts.
#include <QByteArray>
#include <QList>
#include <QtNetwork>
#include <QtCore>
#include <QMessageBox>
#include "ConnectionThread.h"
{
Q_OBJECT
public:
~OctoServer();
signals:
//...
public slots:
void readInput();
//...
private:
};
{
//Open a file pointing to the stdin and connect incoming data signal to custom slot
connect(stdinFile, SIGNAL(readyRead()), this, SLOT(readInput()));
//...
}
void OctoServer::readInput()
{
//This slot is never called !!
}
OctoServer::~OctoServer()
{
delete directory_;
}
#include <QByteArray>
#include <QList>
#include <QtNetwork>
#include <QtCore>
#include <QMessageBox>
#include "ConnectionThread.h"
class OctoServer : public QTcpServer
{
Q_OBJECT
public:
OctoServer(QObject *parent = 0);
~OctoServer();
signals:
//...
public slots:
void readInput();
//...
private:
QString* directory_;
};
OctoServer::OctoServer(QObject* parent) : QTcpServer(parent)
{
QFile* stdinFile = new QFile(this);
directory_ = new QString();
//Open a file pointing to the stdin and connect incoming data signal to custom slot
stdinFile->open(stdin, QIODevice::ReadOnly);
connect(stdinFile, SIGNAL(readyRead()), this, SLOT(readInput()));
//...
}
void OctoServer::readInput()
{
//This slot is never called !!
QMessageBox::information(0, QString("Data incoming"), QString("Data")) ;
}
OctoServer::~OctoServer()
{
delete directory_;
}
To copy to clipboard, switch view to plain text mode
Bookmarks