最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS: Send EOF to stdin stream without closing stream - Stack Overflow

programmeradmin3浏览0评论

How can I signal EOF to a stream without closing the stream?

I've got a script that waits for input on stdin, then when I push ctrl-d, it spits output to stdout, then waits again for stdin until I press ctrl-d.

In my nodejs script, I want to spawn that script, write to the stdin stream, then somehow signal EOF without closing the stream. This doesn't work:

var http = require('http'),
    spawn = require('child_process').spawn;

var child = spawn('my_child_process');
child.stdout.on('data', function(data) {
    console.log(data.toString());
});

child.stdout.on('close', function() {
    console.log('closed');
})

http.createServer(function (req, res) {
    child.stdin.write('hello child\n');
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

But if I change child.stdin.write(...) to child.stdin.end(...), it works, but only once; the stream is closed after that. I read somewhere that EOF isn't actually a character, it's just anything that's NOT a character, usually -1, so I tried this, but this didn't work either:

var EOF = new Buffer(1); EOF[0] = -1;
child.stdin.write("hello child\n");
child.stdin.write(EOF);

How can I signal EOF to a stream without closing the stream?

I've got a script that waits for input on stdin, then when I push ctrl-d, it spits output to stdout, then waits again for stdin until I press ctrl-d.

In my nodejs script, I want to spawn that script, write to the stdin stream, then somehow signal EOF without closing the stream. This doesn't work:

var http = require('http'),
    spawn = require('child_process').spawn;

var child = spawn('my_child_process');
child.stdout.on('data', function(data) {
    console.log(data.toString());
});

child.stdout.on('close', function() {
    console.log('closed');
})

http.createServer(function (req, res) {
    child.stdin.write('hello child\n');
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

But if I change child.stdin.write(...) to child.stdin.end(...), it works, but only once; the stream is closed after that. I read somewhere that EOF isn't actually a character, it's just anything that's NOT a character, usually -1, so I tried this, but this didn't work either:

var EOF = new Buffer(1); EOF[0] = -1;
child.stdin.write("hello child\n");
child.stdin.write(EOF);
Share Improve this question asked Jun 1, 2012 at 0:05 Trevor DixonTrevor Dixon 24.4k13 gold badges75 silver badges113 bronze badges 6
  • I'm pretty sure this is not possible. See stackoverflow.com/questions/9633577/… – Nick White Commented Jun 1, 2012 at 0:23
  • Why can't you just close the input stream? I'm rather confused here. – jcolebrand Commented Jun 1, 2012 at 0:25
  • 1 Because I want to write again to stdin. The process waits for EOF, then chunks on the input, then reopens /dev/stdin to wait for more. – Trevor Dixon Commented Jun 1, 2012 at 0:26
  • 3 That seems horribly against the concept of unix, who wrote the other process? – jcolebrand Commented Jun 1, 2012 at 0:29
  • 1 I wrote it. It's a PhantomJS script. Looks like I may have to rework that. It's not unprecedented though; coders in Python, C, C++, and probably others have wondered the same thing. – Trevor Dixon Commented Jun 1, 2012 at 0:38
 |  Show 1 more comment

3 Answers 3

Reset to default 6

Have you tried child.stdin.write("\x04");? This is the ascii code for Ctrl+D.

You did it with res just two lines below...

  • stream.write(data) is used when you, want to continue writing
  • stream.end([data]) is used when you don't need to send more data (it will close the stream)
var http = require('http'),
    spawn = require('child_process').spawn;

var child = spawn('my_child_process');
child.stdout.on('data', function(data) {
    console.log(data.toString());
});

child.stdout.on('close', function() {
    console.log('closed');
})

http.createServer(function (req, res) {
    child.stdin.end('hello child\n');
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
var os = require("os");    
child.stdin.write("hello child\n");
child.stdin.write(os.EOL);

I use this in my project and it works

发布评论

评论列表(0)

  1. 暂无评论