C++ 如何在Node.js(pref.Express模块)服务器和C+之间进行本地通信+;使用IPC(Unix域套接字)的应用程序

C++ 如何在Node.js(pref.Express模块)服务器和C+之间进行本地通信+;使用IPC(Unix域套接字)的应用程序,c++,node.js,ipc,boost-asio,unix-socket,C++,Node.js,Ipc,Boost Asio,Unix Socket,我有一台机器同时运行一些C++应用程序,还有一台Node.js服务器 用例: 我希望能够触发我的C++应用程序,并使它通过一些数据(让我们说一个字符串)到一个套接字文件。然后,我的Node.js服务器将从套接字获取数据,并通过TCP端口(此处未包含代码)将其打印到某个网页上。反之亦然 到目前为止我所做的: 我能够使用以下代码将Node.js服务器中的一些字符串写入套接字文件: server.js var net = require('net'); var fs = require('fs');

我有一台机器同时运行一些C++应用程序,还有一台Node.js服务器

用例: 我希望能够触发我的C++应用程序,并使它通过一些数据(让我们说一个字符串)到一个套接字文件。然后,我的Node.js服务器将从套接字获取数据,并通过TCP端口(此处未包含代码)将其打印到某个网页上。反之亦然

到目前为止我所做的: 我能够使用以下代码将Node.js服务器中的一些字符串写入套接字文件:
server.js

var net = require('net');
var fs = require('fs');
var socketPath = '/tmp/sock';

fs.stat(socketPath, function(err) {
    if (!err) fs.unlinkSync(socketPath);
    var unixServer = net.createServer(function(localSerialConnection) {
        localSerialConnection.on('data', function(data) {
            // data is a buffer from the socket
                    console.log('Something happened!');
        });
        // write to socket with localSerialConnection.write()
                localSerialConnection.write('HELLO\n');
                localSerialConnection.write('I\'m\n');
                localSerialConnection.write('DOING something!\n');
                localSerialConnection.write('with the SOCKS\n');
    });
unixServer.listen(socketPath);
});
const net = require('net');
const express = require('express');
const app = express();
const c_port = 6666;
const si_port = 8888;

//------------- From here Browser stream is handled -------------//

app.get('/', (req, res)=>{
  res.send('Hello from Node!');
});

app.get('/index.html', (req, res) => {
  res.sendFile(__dirname + "/" + "index.html");
});

app.listen(si_port,(req, res)=>{
  console.log(`Listening on http://localhost:${si_port}`);
});


//------------- From here C++ stream is handled -------------//

var server = net.createServer(function(c) { //'connection' listener
  console.log('client connected');
    c.on('end', function() {
    console.log('client disconnected');
  });
  c.write('hello\r\n');

  c.on('data', function(data){
  var read = data.toString();
  console.log(read);
    // var message = c.read();
    // console.log(message);
  })
  // c.pipe(c);
  c.write('Hello back to C++'); // But only if you shut down the server
});

server.listen(c_port, function() { //'listening' listener
  console.log(`Listening for input from C++ application on port:${c_port}`);
});
使用
nc-U/tmp/sock
和以下输出读取内容

<>我运行C++代码:

cpp\u插座.cpp

#include <boost/asio.hpp>
#include <iostream>

int main() {
    using boost::asio::local::stream_protocol;
    boost::system::error_code ec;

    ::unlink("/tmp/sock"); // Remove previous binding.
    boost::asio::io_service service;
    stream_protocol::endpoint ep("/tmp/sock");
    stream_protocol::socket s(service);

    std::cout << "passed setup section" << std::endl;
    
    s.connect(ep);

    std::cout << "passed connection" << std::endl;

    std::string message = "Hello from C++!";
    
    std::cout << "before sending" << std::endl;
    boost::asio::write(s, boost::asio::buffer(message), boost::asio::transfer_all());
    /* s.write_some(boost::asio::buffer("hello world!"), ec); */
    std::cout << "after sending" << std::endl;
#include <iostream>
#include <boost/asio.hpp>

int main(int argc, char* argv[]) 
{

    if(argc != 4){
        std::cout<<"Wrong parameter\n"<<"Example usage ./client 127.0.0.1 1234 hello"<<std::endl;   
        return -1;
    }

    auto const address = boost::asio::ip::make_address(argv[1]);
    auto const port = std::atoi(argv[2]);
    std::string msg = argv[3];
    
    msg = msg + '\n';

    boost::asio::io_service io_service;
    
    //socket creation
    boost::asio::ip::tcp::socket socket(io_service);
    
    //connection
    boost::system::error_code ec;
    socket.connect( boost::asio::ip::tcp::endpoint( address, port ),ec);
    if(ec){std::cout<<ec.message()<<std::endl; return 1;}
    
    // request/message from client
    //const string msg = "Hello from Client!\n";
    boost::system::error_code error;
    boost::asio::write( socket, boost::asio::buffer(msg), error );

    if(error){
        std::cout << "send failed: " << error.message() << std::endl;
    }
    
    // getting response from server
    boost::asio::streambuf receive_buffer;
    boost::asio::read(socket, receive_buffer, boost::asio::transfer_all(), error);
    
    if( error && error != boost::asio::error::eof ){
        std::cout << "receive failed: " << error.message() << std::endl;
    }
    else{
        const char* data = boost::asio::buffer_cast<const char*>(receive_buffer.data());
        std::cout << data << std::endl;
  }
    return 0;
}
#包括
#包括
int main(){
使用boost::asio::local::stream_协议;
boost::system::error_code ec;
::取消链接(“/tmp/sock”);//删除以前的绑定。
boost::asio::io_服务;
流_协议::端点ep(“/tmp/sock”);
流_协议::套接字s(服务);

std::coutOh-oops。错误显而易见:

::unlink("/tmp/sock"); // Remove previous binding.
移除套接字。如果要连接到它,则这不好

删除该行使其正常工作:

passed setup section
passed connection: Success
before sending
after sending
在听众方面:

我想这是意料之中的,因为客户端尚未完成。

免责声明: 我让它与TCP套接字一起工作,但我想看看如何与unix套接字一起工作。多打开一个端口可能会导致潜在的安全威胁(如果我错了,请纠正我)。因此,如果您(sehe)或者有人知道如何做到这一点,请随意分享。因为我在互联网上的搜索中找不到这一点,这可能对其他人也有帮助

我现在做的是:

    < Lee >创建一个侦听两个端口的NoDEJS服务器,一个用于Web浏览器的端口,另一个用于C++应用程序
  • 将C++应用程序与一个端口
  • 连接
  • 使用telnet发送字符串
server.js

var net = require('net');
var fs = require('fs');
var socketPath = '/tmp/sock';

fs.stat(socketPath, function(err) {
    if (!err) fs.unlinkSync(socketPath);
    var unixServer = net.createServer(function(localSerialConnection) {
        localSerialConnection.on('data', function(data) {
            // data is a buffer from the socket
                    console.log('Something happened!');
        });
        // write to socket with localSerialConnection.write()
                localSerialConnection.write('HELLO\n');
                localSerialConnection.write('I\'m\n');
                localSerialConnection.write('DOING something!\n');
                localSerialConnection.write('with the SOCKS\n');
    });
unixServer.listen(socketPath);
});
const net = require('net');
const express = require('express');
const app = express();
const c_port = 6666;
const si_port = 8888;

//------------- From here Browser stream is handled -------------//

app.get('/', (req, res)=>{
  res.send('Hello from Node!');
});

app.get('/index.html', (req, res) => {
  res.sendFile(__dirname + "/" + "index.html");
});

app.listen(si_port,(req, res)=>{
  console.log(`Listening on http://localhost:${si_port}`);
});


//------------- From here C++ stream is handled -------------//

var server = net.createServer(function(c) { //'connection' listener
  console.log('client connected');
    c.on('end', function() {
    console.log('client disconnected');
  });
  c.write('hello\r\n');

  c.on('data', function(data){
  var read = data.toString();
  console.log(read);
    // var message = c.read();
    // console.log(message);
  })
  // c.pipe(c);
  c.write('Hello back to C++'); // But only if you shut down the server
});

server.listen(c_port, function() { //'listening' listener
  console.log(`Listening for input from C++ application on port:${c_port}`);
});
client.cpp

#include <boost/asio.hpp>
#include <iostream>

int main() {
    using boost::asio::local::stream_protocol;
    boost::system::error_code ec;

    ::unlink("/tmp/sock"); // Remove previous binding.
    boost::asio::io_service service;
    stream_protocol::endpoint ep("/tmp/sock");
    stream_protocol::socket s(service);

    std::cout << "passed setup section" << std::endl;
    
    s.connect(ep);

    std::cout << "passed connection" << std::endl;

    std::string message = "Hello from C++!";
    
    std::cout << "before sending" << std::endl;
    boost::asio::write(s, boost::asio::buffer(message), boost::asio::transfer_all());
    /* s.write_some(boost::asio::buffer("hello world!"), ec); */
    std::cout << "after sending" << std::endl;
#include <iostream>
#include <boost/asio.hpp>

int main(int argc, char* argv[]) 
{

    if(argc != 4){
        std::cout<<"Wrong parameter\n"<<"Example usage ./client 127.0.0.1 1234 hello"<<std::endl;   
        return -1;
    }

    auto const address = boost::asio::ip::make_address(argv[1]);
    auto const port = std::atoi(argv[2]);
    std::string msg = argv[3];
    
    msg = msg + '\n';

    boost::asio::io_service io_service;
    
    //socket creation
    boost::asio::ip::tcp::socket socket(io_service);
    
    //connection
    boost::system::error_code ec;
    socket.connect( boost::asio::ip::tcp::endpoint( address, port ),ec);
    if(ec){std::cout<<ec.message()<<std::endl; return 1;}
    
    // request/message from client
    //const string msg = "Hello from Client!\n";
    boost::system::error_code error;
    boost::asio::write( socket, boost::asio::buffer(msg), error );

    if(error){
        std::cout << "send failed: " << error.message() << std::endl;
    }
    
    // getting response from server
    boost::asio::streambuf receive_buffer;
    boost::asio::read(socket, receive_buffer, boost::asio::transfer_all(), error);
    
    if( error && error != boost::asio::error::eof ){
        std::cout << "receive failed: " << error.message() << std::endl;
    }
    else{
        const char* data = boost::asio::buffer_cast<const char*>(receive_buffer.data());
        std::cout << data << std::endl;
  }
    return 0;
}
#包括
#包括
int main(int argc,char*argv[])
{
如果(argc!=4){

STD:为什么不使用普通的socket和本地主机连接呢?还是把C++程序变成NoDE.JS本地模块?一个服务器应用程序能同时监听多个TCP端口吗?谢谢!course@SomeLoneDevMan是的,这只是制作一个数组并调用的问题。listen(端口号)好几次。没错。有道理。但它仍然困扰着我,无法通过本地套接字进行通信。我尝试了很多方法。谢谢!!!当我删除
::unlink(/tmp/sock)时唯一有效的方法就是是我的C++代码有规律地运行,但是对socket没有任何作用,我的节点服务器也会在屏幕截图上看到一个错误。或者进行持续的讨论。欢迎您打开新问题,或者编辑当前问题(前提是您不会对其进行太多更改,以免现有答案无效)