Qt进程间通信及简单例子

Qt提供了几种在Qt应用程序中实现进程间通信(IPC)的方式。

1. TCP/IP

跨平台的QtNetwork模块提供了可以简单实现网络编程的类。它提供了特定的应用层协议通信的高级别的类(像QNetworkAccessManager,QFtp)和低等级的类(像QTcpSocket,QTcpServer,QSslSocket)。

Qt4.4以后增加了QLocalServer和QLocalSocket两个本地套接字类,可以用于进程间通信。这两个类是事件循环驱动的,同时也提供WaitForxxx.....等一系列阻塞的方法。

Server端程序

头文件

#ifndef LOCALSERVER_H
#define LOCALSERVER_H

#include 
#include 
#include 

class LocalServer : public QObject
{
    Q_OBJECT
public:
    explicit LocalServer(QObject *parent = 0);

    ~LocalServer();

private slots:
    void processNewConnection();

    void disconnect();

    void readData();

private:
    QLocalServer *server;

};

#endif // LOCALSERVER_H

Server端cpp文件 

#include 
#include 
#include 
#include 
#include "localserver.h"

#define SERVER_NAME "InterProcessCom"
#define IPC_RECV_BUF_LEN (1024)

LocalServer::LocalServer(QObject *parent) : QObject(parent)
{
    qDebug() << "LocalServer construct";

    server = new QLocalServer(this);

    QLocalServer::removeServer(SERVER_NAME);

    if(!server->listen(SERVER_NAME))
    {
        qDebug() << "server listen failed!" << server->errorString();
    }
    connect(server, SIGNAL(newConnection()), this, SLOT(processNewConnection()));
}

LocalServer::~LocalServer()
{
    if(server != NULL)
    {
        server->close();
        delete server;
        server = NULL;
    }
}

void LocalServer::processNewConnection()
{
    qDebug() << "LocalServer processNewConnection";

    QLocalSocket *clientConnection = server->nextPendingConnection();
    localSocketList.append(clientConnection);
    connect(clientConnection, SIGNAL(readyRead()),
           this, SLOT(readData()));

    connect(clientConnection, SIGNAL(disconnected()),
            this, SLOT(disconnect()));
}

void LocalServer::disconnect()
{
    for(int i = 0; i < localSocketList.size(); i++)
    {
        if(localSocketList.at(i)->state() != QLocalSocket::ConnectedState)
        {
            localSocketList.at(i)->deleteLater();
            localSocketList.removeAt(i);
        }
    }

    qDebug() <<  "after disconnect localSocketList size:"  << localSocketList.size();
}

void LocalServer::readData()
{
    QLocalSocket *local = static_cast(sender());
    if (!local)
        return ;

//    local->read();
}

 客户端程序

头文件

#ifndef LOCALCLIENT_H
#define LOCALCLIENT_H

#include 
#include 

class LocalClient : public QObject
{
    Q_OBJECT
public:
    explicit LocalClient(QObject *parent = 0);

    ~LocalClient();

private:
    QLocalSocket *socket;

private slots:
    void readData();

    void displayError(QLocalSocket::LocalSocketError socketError);

};

#endif // LOCALCLIENT_H

cpp文件

#include 
#include 
#include "localclient.h"

#define SERVER_NAME "InterProcessCom"
#define IPC_RECV_BUF_LEN (1024)

LocalClient::LocalClient(QObject *parent) : QObject(parent)
{
    socket = new QLocalSocket(this);

    connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));

    connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
            this, SLOT(displayError(QLocalSocket::LocalSocketError)));

    socket->abort();
    socket->connectToServer(SERVER_NAME);
}

LocalClient::~LocalClient()
{
    if(socket != NULL)
    {
        socket->abort();
        delete socket;
        socket = NULL;
    }
}

void LocalClient::readData()
{
//     socket->read();
}


void LocalClient::displayError(QLocalSocket::LocalSocketError socketError)
{
    switch (socketError) {
    case QLocalSocket::ServerNotFoundError:
        break;
    case QLocalSocket::ConnectionRefusedError:
        break;
    case QLocalSocket::PeerClosedError:
        break;
    default:
        break;
    }
}

2.Shared Memory

Qt提供了跨平台的QSharedMemory类,QSharedMemory提供了一系列操作系统共享内存的方法。此外,QSystemSemaphore可用于控制对系统共享资源的访问,以及在进程之间进行通信。

3.DBus

QDBus模块只能用在Unix环境,它把Qt的信号和槽机制扩展到了进程间通信,可以把一个进程的信号连接到另一个进程的槽。

4.Qt COmmunications Protocol (QCOP)

QCopChannel 类实现了一种通过命名管道在进程间传递消息的协议。QCopChannel只能用在嵌入式Linux。像QDbus模块一样,QCopChannel也把信号和槽机制扩展到了进程间通信,不过和QDbus模块不同的是,QCOP不依赖第三方的库。

你可能感兴趣的:(Qt)