This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi, I am new to socket programming and working on a miniproject for chat server programming. Four clients are chatting through a server. The server is multithreaded. the client code is :
Using Serversocket class makes the server to bounce the message from a client to that same client, not to console of all the clients.What class should be used in server so that the server bounces back the message from one client to the console of all the four clients?
Using Serversocket class makes the server to bounce the message from a client to that same client, not to console of all the clients.
The function of a ServerSocket is to communicate with one client at a time, although it can accept multiple connections (as you apparently have implented already).
If you want it to send something to a client that originates from a different client, then you need to put the code to do that in place yourself - that's not something ServerSocket (or any other JRE class) does.
So if a client sends something that the server should send to all clients, store it in a data structure that all threads have access to. Or you could implement a listener scheme using something like the Observer/Observable classes (the clients threads would be the observers, the server thread the observed).