Communicating with a C program without altering stdin/stdout/stderr

22 views
Skip to first unread message

Mike Aubury

unread,
Dec 19, 2017, 3:07:00 AM12/19/17
to nodejs
Hi,
I want to be able to communicate with a C program while maintaining stdio streams as-is - sort of like the way NodeJS can send messages to child nodejs programs.

I can see in the documentation - i can use something like : 


   spawn('prg', [], { stdio: ['pipe', 'pipe', 'pipe', 'pipe' /* extra one here */] });


But - what do i need to do on the C side to link back to that new pipe ? 
Do I need 2 pipes (one for read, one for write)

Am I better trying to do something with IPC ?

Any ideas, and/or code samples would be greatly appreciated !


Dave Benson

unread,
Dec 19, 2017, 7:47:39 PM12/19/17
to nod...@googlegroups.com
you can access the new file-descriptor from C simply as 3, the index of extra 'pipe'.  for example:
   read(3, buf, sizeof(buf))
   write(3, buf, sizeof(buf))
   fcntl(3, ...)

i usually give my programs command-line arguments for the file descriptor, as it feels a bit ugly to hard-code the relationship.  But, there's plenty of precedence for hard-coding as well.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/12b181e7-ad06-4c98-88e6-de7e0959409f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mikkel Wilson

unread,
Dec 19, 2017, 7:47:44 PM12/19/17
to nodejs
This would be a good case for using Native Addons for Node (nan). https://nodejs.org/api/n-api.html . You may be tempted to use the new C++ addons for node (https://nodejs.org/api/addons.html), but it's not API complete and spits annoying warnings (even with flags) so I'd suggest avoiding it.

The raw docs on nodejs.org are a bit hard to get started with, but I found this article helpful. I think it's still relevant. https://community.risingstack.com/using-buffers-node-js-c-plus-plus/

Also, the libsodium library has a bunch of macros that teach you how to do data conversion to/from node/C: https://github.com/paixaop/node-sodium/blob/master/src/include/node_sodium.h

Best,
Mikkel
Reply all
Reply to author
Forward
0 new messages