This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TCP Threaded Server Client Question

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

I am working on a small chat program for one of my classes, where users can connect to a server and create groups to chat in. When the user connects a new thread is created for the user-server to interact in, inside this thread I add a user to a list of users that have already connected. I am running into the issue an where inside my thread I am adding a user to the list of users but for some reason when I check to the number of users that have connected on my client program code, is says the number of users is 0.

ChatServer:


ThreadHandler:


ChatClient:


Sorry for the long post, any help would be awesome.

Thanks,
Hunter
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the thing is that ChatClient runs in its own JVM and thus has its own version of ChatServer. So the number of users that you're seeing is not of the server.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does ChatClient run in its own JVM? Also I've never encountered this problem before, any ideas on what I can change to remedy this?


Hunter
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time you type in your console: java className, a new JVM is started. I wouldn't classify this as a problem. You can run your programs in a single JVM but that isn't really the idea behind communicating through sockets. You could ask the server what the number of users is at that time and print that.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The server has a method getClients() which returns a List of all the clients connected to the server. The size of that list should be the number of clients that connected to the server, so when I call that method and test it's size isn't that the same as asking the server for the number of users?

Hunter
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 Is that you currently have and 2 is what you want. You'll need to communicate through the sockets to ask what the number of users is.
Situation.png
[Thumbnail for Situation.png]
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok that cleared my confusion up thank you.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic