1

I am writing simple socket chat using console to output messages. The problem is that when one user types a message, and at the same time getting one, his input interrupted:

I: writing my input here, but
Other_user: hi! here is a message for you.
I: it splits to different lines, which is 1) very inconvenient 2) cannot see which simbols i deleted if press backspace

So, what i am asking is, how can I avoid this
(something like: if message is received, check input for symbols; if there are, remember them, delete last stroke in console, print message, and then recreate that stroke).
EDIT: attached picturemessage interleaving

Worm359
  • 87
  • 12
  • it is just console as u want it to be, so I wouldn't expect miracles :) The input would be interrupted as there is nothing that makes your output wait when there is sth typed in and not sent yet. In normal chat it would look the same, but as you notice you have two separate boxes, one for input and second just flashing your and friends messages. – kolboc Oct 17 '16 at 20:33
  • Have you found a solution? – Ufkoku Jan 01 '18 at 18:12
  • @Ufkoku noup. as far as i remember, I came to a conclusion, that operations with standart ouput dont provide such an oportunity – Worm359 Feb 12 '18 at 19:40

1 Answers1

0

hard to tell without specific code, but an option is to use two threads, one to handle the socket input, one for output. attach these to System.in and System.out respectively. it seems like you might be using only one thread to do both.

ryonts
  • 126
  • 6
  • Can't see how it helps. Both System.in and System.out work with console, so it's natural that writing in console interleave with printing new letters, like this: – Worm359 Oct 17 '16 at 21:35
  • added a pic to the post – Worm359 Oct 17 '16 at 21:37
  • i understand; since a single resource is being used, you must use some form of synchronization to allow either the writer or user to use the console, but not both, at any given time. this is far beyond a quick post. is there any chance you can redesign the approach? bottom line, what you are asking for is non-trivial – ryonts Oct 18 '16 at 17:05