• Post Reply Bookmark Topic Watch Topic
  • New Topic

homework : multi user console chat program  RSS feed

 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fellow programmers!

I `m struggling with homework task to create multiuser console chat program.
During whole day of poking around , i finally stand on such concept:

1. When running , Server part must stay into while(true) loop and create threads on each ServerSocket.accept() call, providing socket object inside thread.
I.e.


2. Client side always stays within while(true) loop and waiting for user input with ScannerInstance.nextLine() method.
As soon as user prints something and hits enter, data being captured from scanner and thrown to socket output stream to the server.

My question is : if all parties (X clients and server) are actually in waiting mode (server is waiting for connections and each client is waiting for user input), who will refresh the screen for each client to draw the messages other parties sent?
At this time , each party see updates only when he hits enter and while loop does next iteration reading data from buffer and displaying on console.

Thank you.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides what you have for the client already, you'll need something like this: Another thread which periodically sends a message to the server asking for new messages for the client. The server would respond to such messages by returning a list of messages for the client which it has not already sent to the client.
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.
OK , i figured out that i need to run threads on each client as well. One thread does updates from server and other thread collects input and publish to server.

Second part of the task is to convert this chat into single threaded application.
How this can be done?
I`m out of ideas at this moment.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Gurianov wrote:I `m struggling with homework task to create multiuser console chat program...

Well I'm certainly no expert in this field, but that loop of yours really bothers me because, as far as I can see, it will just keep churning out Threads until hell freezes over.

while(true) - ie, "do forever" - is rarely the right way to do things unless something inside the loop is guaranteed to eventually cause it to break - and I don't see anything that fits that bill here.

Programming involves breaking down problems, so my question to you is this:
Do you know how to to create a single user console chat program? - Or indeed, a console chat program of any kind?

If not, then I strongly advise you to work on a single user (and, if possible, synchronous) version first. Once you've got that working, then will be the time to tackle multiple users.

HIH

Winston
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That while loop is a common idiom of network servers, which are supposed to run forever after all, so that looks OK to me.

As to your question, I'm confused - if all clients are "waiting", not doing anything, there will be nothing to show. Only once a client sends a message will there be anything to display. Am I misunderstanding your question?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:That while loop is a common idiom of network servers, which are supposed to run forever after all, so that looks OK to me.

Fair enough - but I still wonder what would stop that code from simply creating and running Threads by the billion, unless there's some "smarts" in either the Runner constructor or servSocket.accept() that I don't understand.

Winston
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most likely the fact that this is a small project that will never be used by more than a handful of people :-)
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Second part of the task is to convert this chat into single threaded application.
How this can be done?


Essentially you have to make that one Thread multi-task - here is a sketch:

while( running ){

any user keystroke to process? - process it

any complete chat message to send? send it

any state change command? change state

is it time to ask for others chat messages from the server? - request to server

.. well, you get the picture - the one Thread is constantly looking for work

}

Back in the days of CP/M and dial-up connectivity I made an 8 line chat / email / game system work this way.

Bill

 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:That while loop is a common idiom of network servers, which are supposed to run forever after all, so that looks OK to me.

As to your question, I'm confused - if all clients are "waiting", not doing anything, there will be nothing to show. Only once a client sends a message will there be anything to display. Am I misunderstanding your question?


Clients are waiting - i meant clients in endless loop sits on Scanner.readLine(). So user console always in mode "waiting for text" and Enter triggers acquiring string of text and go down the code.
So , if client enters no text and does not hit enter, thread is always in waiting mode.

Essentially you have to make that one Thread multi-task - here is a sketch


I`m confused with the fact that if one thread will do tasks one by one, i will hit the behavior : untill user does not hit enter , - user does not see the new messages.
Here is my sketch :

while (running) {
1 - wait for the text user enters, after enter pressed, get data.
2 - check new messages and display on user screen
3 - display what user has entered above .
}

I`m afraid this loop will hang on 1 step and will not display otehr messages (2) untill user press enter.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, your loop needs to be like this:

while (running) {
see if the user has entered anything
if so, send it to the server
see if the server has any messages for the user
if so, display them to the user
}

This of course means you can't use methods which block while waiting for user input.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while (running) {
1 - wait for the text user enters, after enter pressed, get data.


Like Paul says, you want "check to see if user has done anything" not "wait for something"

Exactly how you do this depends on the client GUI toolkit.

In the old AWT tookit you would make a KeyListener to grab keystrokes and put the events in a queue for the main Thread to process when it gets a chance.

Event oriented computing takes some getting used to.

Bill
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:That while loop is a common idiom of network servers, which are supposed to run forever after all, so that looks OK to me.

Then can you explain to me what it is that stops it (as posted originally) from creating so many Threads that it simply clogs the system or runs out of memory space?

If it doesn't, then clearly there's something inside that loop that prevents it from doing so; and the only thing I can see is either servSocket.accept() or new Runner(...), and the reason why isn't obvious to me in either case.

Sorry if this is a slight digression, but I'm interested to know.

Winston
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing in there to that effect, and nobody said that there was. This isn't code that's about to be used to run the Skype backend, it's a small project for a handful of users where that isn't an issue.
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To update thread with answer .

Single threaded chat application should be done with NIO and channel selectors.
I`ve clarified this task with my menthor.
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know nice tutorial about NIO channels and selectors?
I already red Jenkov.com and some other examples arond the net and still do not get how they work.
Behavior of methods looks like real mess and i cannot get why , for instance we are doing select at 27 line?

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!