aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes TCP Threaded Server Client Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "TCP Threaded Server Client Question" Watch "TCP Threaded Server Client Question" New topic
Author

TCP Threaded Server Client Question

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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


"If the facts don't fit the theory, get new facts" --Albert Einstein
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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

Joined: Oct 25, 2008
Posts: 2700

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

Joined: Mar 13, 2009
Posts: 492

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

Joined: Oct 25, 2008
Posts: 2700

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.


[Thumbnail for Situation.png]

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

ok that cleared my confusion up thank you.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Glad I could help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TCP Threaded Server Client Question