开发者

Java console / read and write concurrently

开发者 https://www.devze.com 2023-02-16 22:44 出处:网络
I am practicing Java concurrency and using a chat client/server for learning. Currently I need a pair of terminal windows (a receiver and a sender) per client. So really there is a group of sender cl

I am practicing Java concurrency and using a chat client/server for learning.

Currently I need a pair of terminal windows (a receiver and a sender) per client. So really there is a group of sender clients and a group of receiver clients, I just consider them in pairs.

Is there a simple way I can enter data and receive console/terminal data concurrently?

If it makes a difference开发者_如何学Go I don't need to echo input, just the output from the server. Also the terminal is bash, so maybe there is a simple terminal solution?


In principle there should be no problem of writing and reading the same terminal at the same time - just use different threads for input and output. Your output may look a bit mangled, when the input is between it, though.

To avoid this, and have sort of a windowed terminal, you need to speak more detailed to your terminal, which is different on different systems, and may need JNI (or some JNI-wrapping library).


The console (i.e. the standard input and output streams) are provided by the operating system to the java process at startup time. I know of no operating system that can provide more than one console.

I therefore recommend that you start the clients and servers as separate Java processes, and have them communicate over TCP. Depending on your objectives, you could implement an existing communication protocol such as Telnet or IRC, which would allow you to use existing client applications for these protocols with all the bells and whistles these provide, but possiblly burden you with implementing rather more commands than you probably need, or define your own simple protocol, in which case you have to implement the clients as well.

One way to go about the latter is to do something like:

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();
        }
    }
}

You can then do:

java Server 55555

and in another console

java Client localhost 55555

Every line you then type into that console will then be echoed back to you from the server.

Good luck with your project!

0

精彩评论

暂无评论...
验证码 换一张
取 消