1

I am working on a project in which I have to write a chat client. For this project, we cannot use a UI and it must be written in Java. I haven't used Java in quite some time so I find myself stuck trying to find the "Java" way to do something.

What I am trying to do is allow incoming data to be processed and printed to the console but at the same time allowing for input. I know it is not a simple task (or sometimes even possible) to have simultaneous non-blocking input/output, but luckily this isn't exactly what I am looking for either. The best way to describe what I want is the ability to let output to be processed and displayed and printed until user input begins.

I know this can be done in C/C++ with a switch statement, but as far as I have found there is no equivalent in Java. I have come across the nio package, and have begun reading through it, but to no avail yet so far.

What are some suggestions on how best to perform this task using Java's toolset? Also, apologies if this isn't clear, I am having a difficult time putting my desires into words.

2 Answers 2

1

So in the end I ended up going with the information found in this post in Graham King's blog. In addition to this I used a single thread to queue incoming data, and then print them once the input was finished.

Incoming data is printed to the screen until user input begins, then it pauses, and all data received during the time that user input is happening is then displayed when user input ends.

Sign up to request clarification or add additional context in comments.

Comments

0

The usual java way of doing it would be to use multiple threads -- two dedicated for each connection (one input, one output), plus any other threads you need for handling stuff not directly related to connections.

Alternately, you CAN use non-blocking I/O via the java.nio package, but its kind of klunky.

1 Comment

This is why I do not suggest NIO, but rather a library - say, XNIO, although there are others - which nicely handles NIO (or "plain threaded IO") in a consistent and easy manner.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.