Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send data to a process?

I'm trying to write a dart server application that will communicate to an application which accepts input and gives output, like the unix tool bc.

I can read the output of bc, but I cannot send a command to bc. Here is my code:

#import('dart:io');

void main() {
  var p = Process.start('bc', ["-i"]);
  var stdoutStream = new StringInputStream(p.stdout);

  stdoutStream.onLine = () => print(stdoutStream.readLine());
  p.stdin.writeString("quit\n");

  p.onExit = (exitCode) {
    print('exit code: $exitCode');
    p.close();
  };
}

When I run it, I get the following error:

Unhandled exception:
SocketIOException: writeList failed - invalid socket handle
 0. Function: '[email protected]' url: 'dart:io' line:4808 col:48
 1. Function: '_SocketOutputStream@14117cc4._write@14117cc4' url: 'dart:io' line:4993 col:70
 2. Function: '[email protected]' url: 'dart:io' line:4969 col:29
 3. Function: '[email protected]' url: 'dart:io' line:5197 col:3
 4. Function: '::main' url: 'file:///var/www/html/example.dart' line:8 col:22

If I comment out the line where I try to write "quit\n", then it runs and I can see the output of bc.

So how do I get my program to send commands to an application on my server like bc?

like image 389
user1591612 Avatar asked Dec 14 '25 16:12

user1591612


1 Answers

The problem is that you're writing to stdin before the process has properly started. Try:

#import('dart:io');

void main() {
  var p = Process.start('bc', ["-i"]);
  var stdoutStream = new StringInputStream(p.stdout);

  stdoutStream.onLine = () => print(stdoutStream.readLine());
  p.onStart = () => p.stdin.writeString("1+1\nquit\n");

  p.onExit = (exitCode) {
    print('exit code: $exitCode');
    p.close();
  };
}
like image 145
Shannon -jj Behrens Avatar answered Dec 16 '25 21:12

Shannon -jj Behrens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!