This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What is the class to use for server in single server multiclient chat programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the class to use for server in single server multiclient chat programming" Watch "What is the class to use for server in single server multiclient chat programming" New topic
Author

What is the class to use for server in single server multiclient chat programming

Shaik Muhammad
Ranch Hand

Joined: Jul 16, 2008
Posts: 36
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?

Please help

[edit]Add code tags. CR[/edit]
[ July 24, 2008: Message edited by: Campbell Ritchie ]
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42954
    
  73
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).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41104
    
  29
Please Use Code Tags; I have added them so you can see how much easier the code is to read.

Did you really write "if (socket == null) . . . socket.close();"? Don't you mean if (socket != null)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the class to use for server in single server multiclient chat programming