• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threading Chat in Command Prompt

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Joel Larson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Message here was editted in to original message.
[ July 04, 2003: Message edited by: Joel Larson ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joel Larson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, thanks alot. Those resources really help.
 
I do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic