码迷,mamicode.com
首页 > 其他好文 > 详细

QT QLocalServer服务端与node IPC通信

时间:2020-07-08 12:58:03      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:define   this   qstring   text   ring   end   服务器   bug   and   

QT 服务端代码:

.h

#ifndef LOCALSERVER_H
#define LOCALSERVER_H

#include <QObject>
#include <QtNetwork>

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

    bool RunServer(QString name);

signals:

public slots:
    void serverNewConnectionHandler();
    void socketReadyReadHandler();

private:
    QLocalServer *m_localServer;
    QLocalSocket *m_socket;
};

#endif // LOCALSERVER_H

.cpp

#include "localserver.h"

#include <QDebug>

LocalServer::LocalServer(QObject *parent) :
    QObject(parent), m_localServer(nullptr), m_socket(nullptr)
{
    m_localServer = new QLocalServer();
    connect(m_localServer, &QLocalServer::newConnection, this, &LocalServer::serverNewConnectionHandler);
}

LocalServer::~LocalServer() {
    m_localServer->close();
    delete m_localServer;
}

bool LocalServer::RunServer(QString name) {
    QLocalServer::removeServer(name);
    return m_localServer->listen(name);
}

void LocalServer::serverNewConnectionHandler() {
    qDebug() << "new connction";
    m_socket = m_localServer->nextPendingConnection();
    connect(m_socket, &QLocalSocket::readyRead, this, &LocalServer::socketReadyReadHandler);
    connect(m_socket, &QLocalSocket::disconnected, m_socket, &QLocalSocket::deleteLater);
    connect(m_socket, &QLocalSocket::disconnected, [=]() {
        qDebug() << "disconnected";
    });
}

void LocalServer::socketReadyReadHandler() {
    QTextStream stream(m_socket);
    qDebug() << "DATA:" << stream.readAll();
}

 

nodejs 服务端代码:

let net = require(‘net‘);
let path = require(‘path‘);

let server = net.createServer(function (connect) {
    connect.on(‘error‘,function(err){
        console.log(‘socket error:‘ + err);
        connect.end();
    });
    connect.on(‘close‘,function(data){
        console.log(‘client closed:‘, data);
    });
    connect.on("data",function (data) {
        console.log(‘data:‘, data);
    })
}).listen(path.join(‘\\\\?\\pipe‘,‘\\tmp\\echo.sock‘));

server.on("error",function(err){
    console.log("error:" + err);
});

 

nodejs 客户端代码:

let net = require(‘net‘);
let path = require(‘path‘);

let client = new net.Socket();

let strPath = path.join(‘\\\\?\\pipe‘, ‘/tmp/echo.sock‘);
console.log(strPath);
client.connect(strPath, () => {
  console.log("已连接到服务器"); 
})

client.on(‘data‘, (data) => {
    console.log(‘data:‘, data.toString());
})

client.on(‘close‘, () => {
    console.log(‘Connection closed‘);
})

client.on(‘end‘, () => {
    console.log(‘已从服务器断开‘);
})

client.on(‘error‘, (err) => {
    console.log("err:", err);
})

 

QT QLocalServer服务端与node IPC通信

标签:define   this   qstring   text   ring   end   服务器   bug   and   

原文地址:https://www.cnblogs.com/caiyingyong/p/13266100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!