0

I'm building a chat program through the terminal, and I'm stuck when trying to figure out how I can print out a message from the server while simultaneously listening for user input.

The application is at all times waiting for input from the client to send to the server like so

enter message:

So when I have the server send a message to the client, it only prints out when the client enters a command and enters listen for server response mode.

I have a worker thread running of the main client thread that is continuously listening for server messages of a certain type and prints those to the terminal.

I've looked through some answers on here about interrupting the main thread which is listening for user input but I could not apply the solutions to my problem.

Alex
  • 325
  • 7
  • 16
  • This easiest way to do this is with a GUI such as Swing of JavaFX. The console support in Java is designed for this sort of interactivity. You can use third party libraries to do this but most people use a GUI. – Peter Lawrey Oct 09 '15 at 17:03
  • @PeterLawrey ah but a requirement of my program is to do this in the terminal – Alex Oct 09 '15 at 17:54
  • Can you show some code? You can always listen to the client in a new thread. – WalterM Oct 09 '15 at 18:02
  • Why do you _want_ to interrupt the main thread? The server thread handles messages from the server, the main() thread handles input from the console, what's the problem that you are trying to solve? Are you asking about how to _display_ the incoming messages without messing up the user's ability to edit an outgoing message? If that is the case, then you either want to go with a full GUI app (as @PeterLawrey suggested), or if your app must run in a terminal window for some reason, then you could use a library like Lanterna or Java Curses, or some such. – Solomon Slow Oct 09 '15 at 18:45
  • yea that's the only reason, I'm wanting to interrupt the main thread because it is running the blocking readLine() method. Are you telling me there's pretty much no way to do this without third party methods or a GUI? Because I would very happily use a java GUI for this if that's the only alternative. – Alex Oct 09 '15 at 18:54

1 Answers1

1

So it sounds like you are asking how to write a program that runs in a terminal emulator window, and which has one (or more than one) thread that updates the window, while another thread waits for the user to type commands.

If I'm wrong, then stop right here, and learn how to write a GUI app (e.g., with JavaFX or with Swing) instead. If you really need a console app, then read on.


The heart of the problem is that normally, a terminal emulator window is not controlled by your program.

When you type commands to a typical console application, you expect to be able to see what you type (that's called echo), and you expect to be able to edit what you're typing (e.g, to back up and fix mistakes).

Not only is it not your code that echoes characters and lets you edit the line, it's not even your process doing it: Something else--usually some component of the operating system--is handling the keypress events, doing the editing, updating the terminal window, and feeding your program input a whole line at a time.

To really take control, you have to open your program's console in a special mode (see, https://en.wikipedia.org/wiki/POSIX_terminal_interface)

Then your program can read the key-presses character-by-character, and it will be the sole source of characters going out to the terminal, and that plus knowledge of the escape sequences that position the cursor will give you the power to completely control what appears on the screen...

...but at a very low level.

Third party libraries exist that give you a higher-level of functionality. They can handle the line editing for you (assuming you want line-by-line input), and they can split the screen into different regions, and let your different threads write to them like they were little mini-consoles. They can do other things too...

The question, what's a good library to use? already has answers here:

What's a good Java, curses-like, library for terminal applications?

Community
  • 1
  • 1
Solomon Slow
  • 25,130
  • 5
  • 37
  • 57