• 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

Beginning Threads

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, just finished reading my first book on Java and I'm working on a Chat Client/Server project so I can dig a little bit into streams, multi-threading, saving objects, etc.

At the moment, I'm a little puzzled.

The way I think this through in my head (and you can tell me if this is completely wrong) is when the server accepts a client,
- I want to create a separate thread for any messages that might come through (text, when people are typing back and forth).
- The other thread is constantly waiting for any Objects that are sent to the Server so I can update the Player list.

This is my code for accepting another client:



I have two inner classes that implement Runnable and have their own run() methods that are being executed by two threads.
However, the program doesn't want to initialize both threads? If I have them both in there it won't make the second thread?
If I have just one thread created in this procedure than everything works fine?

Please tell me if I'm doing something completely incorrect here. If you need more code, let me know. Keep in mind, this is my FIRST thread project.


PS: After looking this over, is the solution to have another thread constantly just doing the accepting process of clients?
 
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you know the program doesn't want to initialize both? If you deduce this from code inside ClientHandlerText and ClientHandlerObj, we should probably see this code.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've added System.out.println for debugging between threads. The code above won't even finish after the first thread is initialized.

ClientHandlerText:




ClientHandlerObjects:

 
Stephan van Hulst
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you have two different threads reading from the same socket. That's a bad idea. Make one Runnable that's responsible for reading everything from one socket.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So,

- Scratch having two threads
- One thread constantly reading if the client sends an object or a string of text
- Implement code from there accordingly

Correct?

Something like:

 
Stephan van Hulst
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, because now you'll be alternating between text and objects, which both come from the same input stream.

You should first develop a protocol describing what input you expect at any given moment, and then attempt to write the thing you expect.

Describe the flow of messages between two clients.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, Yeah I didn't get a whole lot of background for handling multiple types of streams.

- Server Running...

- Client Running...
-> Connects to Server
-> Server sends currentPlayerList to client (So user can login)

- If User needs to create a new player
-> Send new Player to Server so Server can update playerList (...and then saves the playerlist to a file)

- When user logs in (Haven't created this yet)
-> Types message into JTextField
- Sends to Server which disperses it among all clients

This is all I have now, and I only have the GUI for the login window and create new player window
 
reply
    Bookmark Topic Watch Topic
  • New Topic