java 프로세스 인/아웃 스트림 받아서 실시간으로 처리하고싶습니다.
글쓴이: jaykee33 / 작성시간: 목, 2017/02/09 - 10:14오후
자바로 서버를 만들려고하는데,
1차 목표로 일단 서버의 프로그램을 실행해서 스트림을 받아와서 사용해야되는데요,
입력이 없는 프로그램은 잘되는데, 입력이 존재하는 프로그램은 프로세스가 시작되고 입력 전 출력이 안되더라구요??
아무래도 입력,출력이 정해져있지 않아서 스레드로 돌리려는데 당최 이해가 안되서요.
package test1; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class test3 { public static void main(String[] args){ Process ps=null; Runtime rt=null; try{ rt = Runtime.getRuntime(); ps=rt.exec("C:\\temp\\test.exe"); Thread thread1 = new ReceiverThread(ps); Thread thread2 = new SenderThread(ps); thread1.start(); thread2.start(); }catch(Exception e){ System.out.println(e.getMessage()); } } } class SenderThread extends Thread{ Process ps; SenderThread(Process ps){ this.ps = ps; } public void run(){ try{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(ps.getOutputStream()); String msg=null; while(true){ msg=reader.readLine(); if(msg==null) continue; writer.println(msg); writer.flush(); } }catch(Exception e){ System.out.println(e.getMessage()); } } } class ReceiverThread extends Thread{ Process ps; ReceiverThread(Process ps){ this.ps = ps; } public void run(){ try{ BufferedReader reader = new BufferedReader(new InputStreamReader(ps.getInputStream())); PrintWriter writer = new PrintWriter(System.out); String msg=null; while(true){ msg=reader.readLine(); if(msg==null) continue; writer.print(msg); writer.flush(); } }catch(Exception e){ System.out.println(e.getMessage()); } } }
너무 답답해서 테스트코드 전문 붙입니다.
참고로 test.exe 의 코드는 간단하게 입출력 테스트입니다
#include <stdio.h> void main(void) { int a; printf("Hello J : "); scanf("%d",&a); printf("print : %d",a); }
실행은 되는데 출력이
5 Hello J : print : 5
이렇게 입력을 먼저 받아서 한방에 출력해버립니다.
제가 원하는건
Hello J : 5 print : 5
이런 결과인데 말이죠...
한마디로 출력>입력>출력 이런 순서의 프로그램인데
(사전출력X) 입력>출력 이렇게 되버린다는거죠
혹시 뭐가 문제인지 알려주실수 있으신 분 ㅜㅜ?
ProcessBuilder 이용해서 해도 같은증상입니다 ㅜ
참고로 개발환경 이클립스입니다
--- 자꾸 사람들이 C코드를 조언해줘서 테스트코드 파이썬으로 바꾸겠습니다. 테스트코드문제가 아닙니다.
def main(): print "start py!\n"; aaa=input(); print "hello world",aaa; if __name__ == '__main__': main()
이 프로그램의 결과도
3 start py!hello world 3
이렇게 입력을 먼저 받아버립니다.
File attachments:
첨부 | 파일 크기 |
---|---|
![]() | 83.71 KB |
![]() | 6.15 KB |
Forums:
댓글 달기