File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shaik Muhammad
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic