PDA

View Full Version : QtService and QLocalSocket



matteo.ceruti
11th January 2012, 17:22
I developed a service with QTService with this function

class DataManage:public QObject
{
public:
DataManage(QObject* parent = 0);
void ReceiveData(void);
private:
QLocalSocket* lpLocalSocket;
private slots:
void ReadData(void);
void displayError(QLocalSocket::LocalSocketError socketError);
};


void SettingWidget::SendDataToService(void)
{
QLocalSocket *lpClientConnection = lpLocalServer->nextPendingConnection();
connect(lpClientConnection, SIGNAL(disconnected()),
lpClientConnection, SLOT(deleteLater()));
strcpy(s,"eqw\n");
lpClientConnection->write(s);
lpClientConnection->flush();
lpClientConnection->disconnectFromServer();
}

void DataManage::ReceiveData(void)
{
lpLocalSocket = new QLocalSocket(this);
connect(lpLocalSocket, SIGNAL(readyRead()), this, SLOT(ReadData()));
connect(lpLocalSocket, SIGNAL(error(QLocalSocket::LocalSocketError)),
this, SLOT(displayError(QLocalSocket::LocalSocketError)) );
lpLocalSocket->abort();
lpLocalSocket->connectToServer("aaaa");
}

void DCSPlusDataManager::processCommand(int code)
{
logMessage("processCommand", QtServiceBase::Information );
switch (code)
{
case 1:
DataManage* lpDataManage = new DataManage();
lpDataManage->ReceiveData();
break;
}
}

From the main application I send the command that activate processCommand() of the service. It runs.
Then I send the data to the socket:

void SettingWidget::on_pushButtonApply_clicked(bool)
{
lpLocalServer = new QLocalServer(this);
if (lpLocalServer->listen("aaaa"))
{
connect(lpLocalServer, SIGNAL(newConnection()), this, SLOT(SendDataToService()));
SC_SendCommand(1);
}
}
//----------------------------------------------------------------------------

void SettingWidget::SendDataToService(void)
{
QLocalSocket *lpClientConnection = lpLocalServer->nextPendingConnection();
connect(lpClientConnection, SIGNAL(disconnected()),
lpClientConnection, SLOT(deleteLater()));

strcpy(s,"eqw\n");
lpClientConnection->write(s);
lpClientConnection->flush();
lpClientConnection->disconnectFromServer();

}

The function SendDataToService() is called, although the lpClientConnection->write(s) doesn't call the ReadData() slot connected with readyRead() sognal.
Where can be the probem?

Thanks

Willi
23rd January 2014, 18:45
Hi, how did you defined the SocketOptions of Your QLocalServer? I needed to define theam as follows:

lpLocalServer->setSocketOptions(QLocalServer::WorldAccessOption);

So that a Server running as Service can be contacted by a User-Program