请教java与c++通过管道交互的问题
c++里面fork,两个pipe,父进程把字符串写入pipe1[1],并读取pipe2[0];子进程重定向标准输入到pipe1[0]、标准输出到pipe2[1],然后启动java。
java里面就是读一下标准输入,然后再输入一些字符串。
问题是:现在从java回的标准输出不完整,不知道为什么。达人请帮忙看下。
c++代码:
java代码:
运行c++结果是:
# ./a.out
child proc
parent proc. child proc id=6295
c read length=36, content=java start0
java start4
java start5
?
java start5之后出现乱码,请教下是什么问题。多谢了。
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之后出现乱码,请教下是什么问题。多谢了。
...全文
相关推荐
android pipe
android 下匿名管道实现
从C++转到Java需注意的地方
从C++转到Java需注意的地方 默认的Java环境中包括一些从Java基础类实现而来的一些包。这些允许你很快的写出一些有用的程序,这些包如下: java.awt:当今许多应用程序都非常依赖GUI,java提供了一个Abstract Window Toolkid,这可以让你在不考虑运行平台的前提下处理GUI对象。 java.applet:applet的主要目的是提供浏览有关的内容。它本身是awt组件的字类并且支持其他一些特性,比如声音、渲染等。 java.io:java.io提供了对流、文件、管道的读写操作。 java.lang:提供了java的基础类Objcet,Integar,Float……; java.net:提供对网络编程的支持。包括处理socket,URL,Internet寻址等。
jsp+servlet+c++图像拼接系统
jsp+servlet+c++图像拼接系统
PipeImpl.rar_Java编程_Unix_Linux_
Implements java.nio.channels.Pipe for Embedded Linux.
Android平台音视频硬解码教程项目_基于AndroidStudio363与NDKr17c开发环境_集成MediaCodec解码音视频数据_AudioTrack播放音频原始数.zip
Android平台音视频硬解码教程项目_基于AndroidStudio363与NDKr17c开发环境_集成MediaCodec解码音视频数据_AudioTrack播放音频原始数.zip


