1

I have Java application. I try to realize multiplayer chat with TCP protocol, but I have an issue. I cannot output all messages I get on client tier (server tier works perfectly), because I give an input to user and create a thread to read messages from TCP client, so

<Scanner object>.nextLine();

Is blocking an output of my app. I can create by Swing, but I still haven't fully understood it.

I have two options:

  1. Output all new messages from queue after sending message by user.
  2. Somehow detach input and output.

I think you don't need to explain that in 1st option if user will go out for a long time, he will need to send a message before he will get all these new messages.

Only the second option remains. I need to detach input and output and I found that in some "interactive tasks" there are two consoles - first used for input, other - for output. But can I do it into a Java? And if I can, how?

1 Answer 1

0

If you use Swing, you can make the traditional two-control "chat" layout: ┌────────────┐ │output │ │output │ │output │ │... │ └────────────┘ ┌────────────┐ │input> _ │ └────────────┘

Send one thread to output to the top area, another thread to listen to changes in the bottom area. You can send bottom area's contents to the top area, too, if you want an unified log. You will need line-level buffering, though, like traditional terminals have; they hae it for a reason. Without it, parts of lines would intermix.

If you want Linux / macOS / other posix console, use some wrapper around curses to reproduce the layout like above, and don't read stdin directly.

(If you need Windows console, I have no idea what to use.)

4
  • I wrote into my question that I can create by Swing, but I still haven't fully understood it. I tried, but I cannot make app's structure. Dec 20, 2018 at 3:37
  • I suppose you should take a tutorial, like this, read more on concurrency in Swing, and generally do more googling. This topic is very popular, there are tons of example Java TCP + Swing chat example code.
    – 9000
    Dec 20, 2018 at 4:14
  • OK, but TCP client not the only reason to detach input & output. There are many tasks that need detaching, and I hope I won't make a Swing app for each one personally, and in this question I tried to know: how can I control at least 2 consoles at the same time. Dec 20, 2018 at 10:46
  • This depends on what are these consoles. For posix pseudoterminal interface (aka pty) it seems possible but neither usual nor particularly easy. A process can theoretically open more than one pty device. It would likely be easier to just run a local telnet server with different "consoles" on different ports / unix sockets, and let a telnet client handle the pty stuff. For Windows consoles, I have no idea. It would be great to understand which problem are you really solving.
    – 9000
    Dec 20, 2018 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.