Linux Nodejs与C++程序通信?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9843195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nodejs communicate with C++ program?
提问by Nami WANG
Say I've got a c++ program running on the same server with a Node.js web app, on a linux server.
假设我有一个 C++ 程序与 Node.js Web 应用程序在同一台服务器上运行,在 linux 服务器上。
The c++ program maintains a queue, and what I want to do with Node.js is, push some data into the queue.
c++程序维护了一个队列,我想用Node.js做的是,把一些数据推入队列。
What's the best way to do this?
做到这一点的最佳方法是什么?
Which is better? sockets or IPC?
哪个更好?插座还是工控机?
采纳答案by André Caron
If you're using Linux, I would suggest UNIX-domain sockets. They basically give you the high-performance of IPC using the BSD socket interface, making it easy to switch for TCP sockets later if you need to move the C++ (or node.js) application to a different computer.
如果您使用的是 Linux,我建议您使用UNIX-domain sockets。它们基本上为您提供了使用 BSD 套接字接口的 IPC 的高性能,如果您需要将 C++(或 node.js)应用程序移动到另一台计算机,则稍后可以轻松切换到 TCP 套接字。
They're already supported by node.jsand only the code that opens the socket will need to be changed. Many applications, including MySQL easily abstract this away in a configuration file.
node.js 已经支持它们,只需要更改打开套接字的代码。许多应用程序,包括 MySQL,很容易在配置文件中抽象出来。
回答by luke14free
I'd use sockets, they are clean and easy to use
我会使用插座,它们干净且易于使用
回答by Zac
If you want to use an IPC mechanism, you may consider writing a Node.js C++ module, and then use something from the Boost.Interprocesslibrary to communicate with your other app.
如果你想使用 IPC 机制,你可以考虑编写一个Node.js C++ 模块,然后使用Boost.Interprocess库中的东西与你的其他应用程序通信。
Boost.Interprocess has mechanisms already build for sharing containers from the standard library. Its also cross platform if you want to be open to that in the future.
Boost.Interprocess 已经构建了用于从标准库共享容器的机制。如果你想在未来开放,它也是跨平台的。