我正在尝试在 Linux 中使用命名管道在 NodeJS 和 C 程序之间进行通信。我的服务器程序是用 NodeJS 编写的:
'use strict';
const net = require('net');
const pipename = '/tmp/pipe1';
const fs = require('fs');
let server = net.createServer(function(socket){
        console.log('A new connection');
        socket.on('data',function(data){
                console.log(data.toString());
        });
        socket.on('end',function(){
                console.log('Closed connection');
        });
});
server.on('error',console.log);
fs.unlink(pipename,function(){
        server.listen(pipename);
})
//Test unix-socket server:
setInterval(function(){
        var stream = net.connect(pipename);
        stream.on('error',console.log);
        stream.write('hello');
        stream.end();
},2000);
Run Code Online (Sandbox Code Playgroud)
但是,当我想在 NodeJS 服务器已经启动后打开 C 内部的管道时,我收到错误:
const char* pipename = "/tmp/pipe1";
int hPipe = open(pipename, O_WRONLY); //Error: No such device or address
Run Code Online (Sandbox Code Playgroud)
当我尝试去做时echo 'Hello World!' > …