×
Namespaces

Variants
Actions

Qt IPC using QLocalSocket & QLocalServer

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Emulator / desktop / device
Compatibility
Platform(s): All Qt Supported
Symbian
Article
Keywords: IPC
Created: skumar_rao (23 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Overview

Qt has more then one type of Inter­-Process Communication (IPC) available to use

  • D-Bus
  • QSystemSemaphore
  • QSharedMemory
  • QLocalSocket & QLocalServer

In this article we will try the lightweight and very cross-platform IPC mechanism using QLocalSocket & QLocalServer

Let us create the Client class that we will use for sending messages to other process's Server


Source

Header File

class LocalSocketIpcClient : public QObject
{
Q_OBJECT
public:
LocalSocketIpcClient(QString remoteServername, QObject *parent = 0);
~LocalSocketIpcClient();
 
signals:
 
public slots:
void send_MessageToServer(QString message);
 
void socket_connected();
void socket_disconnected();
 
void socket_readReady();
void socket_error(QLocalSocket::LocalSocketError);
 
private:
QLocalSocket* m_socket;
quint16 m_blockSize;
QString m_message;
QString m_serverName;
};

Source File

LocalSocketIpcClient::LocalSocketIpcClient(QString remoteServername, QObject *parent) :
QObject(parent) {
 
m_socket = new QLocalSocket(this);
m_serverName = remoteServername;
 
connect(m_socket, SIGNAL(connected()), this, SLOT(socket_connected()));
connect(m_socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected()));
 
connect(m_socket, SIGNAL(readyRead()), this, SLOT(socket_readReady()));
connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
this, SLOT(socket_error(QLocalSocket::LocalSocketError)));
}
 
LocalSocketIpcClient::~LocalSocketIpcClient() {
m_socket->abort();
delete m_socket;
m_socket = NULL;
}
 
void LocalSocketIpcClient::send_MessageToServer(QString message) {
m_socket->abort();
m_message = message;
m_socket->connectToServer(m_serverName);
}
 
 
void LocalSocketIpcClient::socket_connected(){
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << m_message;
out.device()->seek(0);
m_socket->write(block);
m_socket->flush();
}
 
void LocalSocketIpcClient::socket_disconnected() {
qDebug() << "socket_disconnected";
}
 
 
void LocalSocketIpcClient::socket_readReady() {
qDebug() << "socket_readReady";
}
 
void LocalSocketIpcClient::socket_error(QLocalSocket::LocalSocketError) {
qDebug() << "socket_error";
}

The QLocalSocket class provides a local socket. Although QLocalSocket is designed for use with an event loop, it's possible to use it without one.


Now we will create Server class that will receive messages from other processes.

Header File

class LocalSocketIpcClient : public QObject
{
Q_OBJECT
public:
LocalSocketIpcServer(QString servername, QObject *parent);
~LocalSocketIpcServer();
 
signals:
void messageReceived(QString);
 
public slots:
void socket_new_connection();
 
private:
QLocalServer* m_server;
};

Source File

LocalSocketIpcServer::LocalSocketIpcServer(QString servername, QObject *parent)
:QObject(parent) {
m_server = new QLocalServer(this);
if (!m_server->listen(servername)) {
qDebug() << "Not able to start the Server";
}
 
connect(m_server, SIGNAL(newConnection()), this, SLOT(socket_new_connection()));
}
 
LocalSocketIpcServer::~LocalSocketIpcServer() {
 
}
 
 
void LocalSocketIpcServer::socket_new_connection() {
 
QLocalSocket *clientConnection = m_server->nextPendingConnection();
 
while (clientConnection->bytesAvailable() < (int)sizeof(quint32))
clientConnection->waitForReadyRead();
 
 
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
 
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_0);
if (clientConnection->bytesAvailable() < (int)sizeof(quint16)) {
return;
}
QString message;
in >> message;
 
emit messageReceived(message);
}

The QLocalServer class provides a local socket based server. This class makes it possible to accept incoming local socket connections. waitForReadyRead() is used to blocks until the operation is complete or the timeout expires. Call listen() to have the server start listening for incoming connections on a specified key. The newConnection() signal is then emitted each time a client connects to the server.Call nextPendingConnection() to accept the pending connection as a connected QLocalSocket. The function returns a pointer to a QLocalSocket that can be used for communicating with the client.


The Examples attached below shows how to use both the classes for IPC.

File:LocalSocketIPC.zip File:Testlocalsocketipc.zip

--skumar_rao 17:50, 23 November 2010 (UTC)

This page was last modified on 11 October 2012, at 04:18.
352 page views in the last 30 days.
×