Skip to content

Instantly share code, notes, and snippets.

@geoffreyp
Created March 20, 2017 20:05
Show Gist options
  • Save geoffreyp/9a623d9f3aae0c27ec803475fe39c5e0 to your computer and use it in GitHub Desktop.
Save geoffreyp/9a623d9f3aae0c27ec803475fe39c5e0 to your computer and use it in GitHub Desktop.
Communication entre NodeJS et un programme écrit en C
const exec = require('child_process').exec;
// On éxécute le programme C
const process = exec('./a.out');
// Envoie d'un message au programme C
process.stdin.write('Message envoyé au programme C depuis app.js');
// écoute le programme C et affiche les données reçu
process.stdout.on('data', function (data) {
console.log("NodeJS receive : "+data.toString().trim());
});
#include <stdio.h>
int main() {
int n;
char buf[255];
// On écoute la sortie standart
n = read(0,buf,sizeof(buf));
// Quand on a reçu quelque chose on le renvoie sur la sortie standart
write(1,"Program C receive : ",20);
write(1,buf,n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment