请教java与c++通过管道交互的问题

duotiger 2012-10-17 11:30:19
c++里面fork,两个pipe,父进程把字符串写入pipe1[1],并读取pipe2[0];子进程重定向标准输入到pipe1[0]、标准输出到pipe2[1],然后启动java。
java里面就是读一下标准输入,然后再输入一些字符串。
问题是:现在从java回的标准输出不完整,不知道为什么。达人请帮忙看下。

c++代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void err_sys(string msg)
{
cout << msg << endl;
exit(-1);
}
int main()
{
int input[2];
int output[2];
int pid;
FILE *log;

if(pipe(input)<0)
err_sys("pipe input error");

if(pipe(output)<0)
err_sys("pipe output error");

if((pid = fork()) < 0)
err_sys("fork error");

if(pid==0)
{
cout << "child proc" << endl;
//child
close(input[1]);
close(output[0]);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

if(dup2(input[0], STDIN_FILENO) != STDIN_FILENO)
err_sys("dup2 error to stdin");

if(dup2(output[1], STDOUT_FILENO) != STDOUT_FILENO)
err_sys("dup2 error to stdout");

if(dup2(output[1], STDERR_FILENO) != STDERR_FILENO)
err_sys("dup2 error to stderr");

/***process the buf***/
system("java -cp /home/tiger/test/java ptest");
}
else
{
cout << "parent proc. child proc id=" << pid << endl;
//parent
close(input[0]);
close(output[1]);

char buf[100];

string temp="input from c father";
write(input[1], temp.c_str(), temp.length());
while(true){
sleep(2);
int n= read(output[0], buf, 100);
//buf[n]='\0';
printf("c read length=%d, content=%s\n", n, buf);
}
}
exit(0);
}


java代码:

import java.io.PrintWriter;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;


public class ptest {

public static void main(String args[]) throws Exception{
System.out.println("java start0");

BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter("output.log");
writer.println("Start");
writer.flush();
boolean readflag = false;
while(true){
System.out.println("java start4");
writer.println("enter while");
writer.flush();
try {
System.out.println("java start5");
for(String read; (read = consoleInput.readLine())!= null && read.length()>0;)
{

String str = "read in java: " + read;
writer.println(str);
writer.flush();
System.out.println(str);
//System.out.flush();
read = "";
readflag = true;
writer.println("readline end");
writer.flush();
}
writer.println("for end");
writer.flush();
if (readflag)
{
System.out.println("read end");
//System.out.flush();
writer.println("while end");
writer.flush();
break;
}
System.out.println("read continue");
//System.out.flush();
}
catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("java end");
//System.out.flush();
writer.println("java end");
writer.flush();
}


}


运行c++结果是:
# ./a.out
child proc
parent proc. child proc id=6295
c read length=36, content=java start0
java start4
java start5
?

java start5之后出现乱码,请教下是什么问题。多谢了。
...全文
170 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
duotiger 2012-10-22
  • 打赏
  • 举报
回复
为什么没回复啊……不能沉不能沉!
duotiger 2012-10-18
  • 打赏
  • 举报
回复
不能沉不能沉不能沉

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧