• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
please buy my thing and then I'll have more money:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic