• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 80613
467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic