Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use vectors within a sever/client multithreaded chat program

Status
Not open for further replies.

ChrisB02

Programmer
Mar 19, 2002
2
GB
i've written a chat program in java that uses a client and a server, i need to implement vectors to get the userlist working and to broadcast the message to all clients
can anybody help?
cheers
chris
 
I dont know i am trying to do exactly this and i was wondering should i use vectors or just store the IP and use that to send info. anyhow for what you are doing, i dont know you might have to make a vector class static in your server thread, each time you receive a connection and start a new thread to handle the client, you just simply add that thread to this vector and in doing so, make your adding synchronized. i think thats pretty much it, i dont think there is more to it, i may be wrong, but it hink just that should work, and when you do receive a message to be sent you just search through the vector and find your guy and use its sendMessage() function to send that message i guess...

please tell me also if i am getting this wrong.

thanx

Ankur
 
You should declare synchronized all the methods that deal with your vector (making it static won't work).

My first instinct for what you're doing would be to have an object that models the chatroom/channel, with synchronized methods for adding and removing users, and sending messages to everyone.
 
so what else did i say...have ur methods(add in particular) synchronized, cuz i dont think read would need to be in synch , cuz you dont neeed to synch things when you are reading, cuz you are not changing data? i may be wrong, but for sure u need to make it static..
 
Hi guys!
Very interesting discussion. I had similar "synchronized" issues hehe so I was wondering why one should make the vector static? I thought static was used only to enable a user to use certain 'utility' methods without instantiating.
Does static help with the synching too? Avendeval


 
i dont know, if it helps with synch, but the point is..how i was thinking of designing the server was, have a server thread which does all the accepting of new clients and then spawn a new thread for a client to be handled, now each client thread need to know of all the other threads, to be able to broadcast or pick the right username and send a message to it. so i thought make the vector static in the client thread, so there is only one storage space and not as many as the objects of client thread, so thats y, u have consistency throughout all the client threads.

i am sure there must be a better and different way of doing it, havent really thought about it too much.

but hope this helps..y i was thinking of making it static.

cheers,
Ankur
 
i see now. i', using a different approach that's why i didn't get it at first. thanks. Avendeval


 
Chris,

Well to try to answer the question for this topic, can you tell me if you are using just Java Sockets, RMI, or some other library to handle your communication?

Just for background for a client/server chat application, you need to be keep track of all the connected users(or sockets) so that you can broadcast all recieved messages out to each client. As for a user list, are you looking for a simple list that just displays the list of user names in the Client Side gui?

Because if you want to keep it simple, and not display a list of users to the client. Then only the server needs to keep track of them, and it should broadcast the user name with their messages.

Please let me know...

Rodney
 
sedai,
it would be good if you could tell us what other approach u might be using. might be enlightening...i will be looking forward to your approach..

Ankur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top