如何通过 DataOutputStream 发送算术运算的有效结果

How to send a valid result of an arithmetic operation through DataOutputStream

我有一个子进程 class 计算两个整数的总和,然后将其放入 DataOutputStream:

public class SubProcess {
  public static void main(String[] args) throws IOException {
     DataInputStream in = new DataInputStream(System.in);
     DataOutputStream out = new DataOutputStream(System.out);
     int a = in.readInt();
     out.writeInt(a);
     int b = in.readInt();
     out.writeInt(b);
     int result = a+b;
     out.writeInt(result);
     out.flush();
     in.read();
     out.close();
     in.close();
    }
  } 

当把 a 和 b 的两个值分别写成 12 和 47 时,结果是 "ei"。

另一方面,主进程不会像 DataInputStream 那样通过 ReadInt() 行读取结果,它会抛出异常:

        Exception in thread "main" java.io.EOFException
            at java.io.DataInputStream.readInt(Unknown Source)
            at testthread.MainProcess.main(MainProcess.java:21)

看来主进程并没有真正与子进程通信。从类 和运行 两个主进程中删除包避免异常。