aspose file tools*
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 Java 8 in Action this week in the Java 8 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
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
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).


Ping & DNS - updated with new look and Ping home screen widget
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
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
 
Similar Threads
Threads and global variable prolems
problem with login to the server
Chat At Server Part Both write and read at the same time?
Producer/Consumer Design impacts exception handling
Please find the problem in this code.