콘솔 (즉, 표준 입력 및 출력 스트림)은 시작시 Java 프로세스에 운영 체제에 의해 제공됩니다. 하나 이상의 콘솔을 제공 할 수있는 운영 체제가 없다는 것을 알고 있습니다.
따라서 클라이언트와 서버를 별도의 Java 프로세스로 시작하고 TCP를 통해 통신하도록 권장합니다. 목표에 따라 Telnet 또는 IRC와 같은 기존 통신 프로토콜을 구현할 수 있습니다.이 프로토콜을 사용하면 이러한 프로토콜에 기존 클라이언트 응용 프로그램을 사용할 수 있지만 이러한 프로토콜은 모든 기능을 제공하지만 사용자보다 더 많은 명령을 구현할 수 있습니다 아마도 필요하거나 자신의 간단한 프로토콜을 정의해야합니다.이 경우 클라이언트도 구현해야합니다. 후자에 대해 갈
한 가지 방법은 같은 것을 할 수 있습니다 :
public class Client {
public static void main(String[] args) throws Exception {
Socket s = new Socket(args[0], Integer.parseInt(args[1]));
new Repeater(System.in, s.getOutputStream()).start();
new Repeater(s.getInputStream(), System.out).start();
}
}
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(Integer.parseInt(args[0]));
for (;;) {
Socket s = ss.accept();
// simply echo for now. You can do more interesting things here ...
new Repeater(s.getInputStream(), s.getOutputStream()).start();
}
}
}
class Repeater extends Thread {
final InputStream in;
final OutputStream out;
public Repeater(InputStream in, OutputStream out) {
this.in = in;
this.out = out;
}
@Override public void run() {
try {
byte[] buf = new byte[4096];
int r;
while ((r = in.read(buf)) != -1) {
out.write(buf, 0, r);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
을 당신은 할 수 있습니다 :
java Server 55555
와 다른 콘솔
java Client localhost 55555
모든 라인 당신의
해당 콘솔에 입력하면 서버에서 다시 사용자에게 에코됩니다.
프로젝트가 성공적으로 완료되었습니다.
이 [예제] (http://stackoverflow.com/questions/3245805)가 apropos인지 확인할 수 있습니다. – trashgod
나는 스윙 솔루션이 효과가 있다는 것을 알고 있지만, 지금은이 터미널 창에 상당히 부분적이다. 정말 그렇게 간단한 것 같아요. – Quaternion