aspose file tools*
The moose likes Threads and Synchronization and the fly likes Threading Chat in Command Prompt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threading Chat in Command Prompt" Watch "Threading Chat in Command Prompt" New topic
Author

Threading Chat in Command Prompt

Joel Larson
Greenhorn

Joined: Mar 29, 2003
Posts: 4
I'm just working with some basic socketing right now. So I made this chat client and server. Problem is you have to take turns typing messages back and forth. I am pretty new to java and I'm not too good with threads. What I want to know how to do is put the Recieving part and the Sending part in seperate threads so that it checks on both of them so that you don't have to take turns. Unless there is a better practice for something like this of course. After a few attempts I had to break down and ask other people. All I ask is a little guidance, I'm not asking anybody to write the script for me.
Client:


Server:

[ July 04, 2003: Message edited by: Joel Larson ]

Spin!
Joel Larson
Greenhorn

Joined: Mar 29, 2003
Posts: 4
Message here was editted in to original message.
[ July 04, 2003: Message edited by: Joel Larson ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think this is typical of socket servers staring a new thread for each new client:

My client also spins a thread to handle responses from the server.

This whole program is here. It's not a chat thing, but some of the socket client and server ideas may work for you. Oh, and it's not bug free, either! Something is not right in what it does after handling a command.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You may also want to check out the example at
http://java.sun.com/docs/books/tutorial/networking/sockets/index.html

In particular, see the last two listings, for KKMultiServer and KKMultiServerThread.
I think this is typical of socket servers staring a new thread for each new client:
This is the standard technique for handling multiple clients concurrently. As of JDK 1.4 it's also possible to use a Selector for improved efficciency. If you have a lot of sockets where most are not really active at any one time, then you can have a single thread listen to all of them using a Selector. And whenever something of interest happens on one of the sockets, use another thread (probably obtained from a thread pool) to service that socket. The idea is to avoid having a bunch of threads sitting around waiting for something to happen, taking up resources. Instead have a smaller number of threads, that are actually doing something. This sort of design may not be necessary for a small server (especially if it's your first one), but it's worth remembering that other options exist.


"I'm not back." - Bill Harding, Twister
Joel Larson
Greenhorn

Joined: Mar 29, 2003
Posts: 4
Wow, thanks alot. Those resources really help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threading Chat in Command Prompt
 
Similar Threads
making Thread of methods
client-serer...plz help
GBApplet problems
Help debugging and running My GUI TCP Chatroom!
Server sending data to Clients Randomly