Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java chat in which server send notification to all clients !!!!

Posted on 2004-05-02
14
Medium Priority
?
394 Views
Last Modified: 2010-03-31
hello to all .,

i am trying to make a console ( gui can be better ) client server chat program ( in java ofcourse !!! ) ...which is multithreaded  ( of course !!! )......
...which goes like this ....

client : LOGIN  shehryar         ( " shehyar" will be extracted using substring )
server : clientname , ip ,  port   ( sends this to all clients connected )
client : LOGOFF
server : clientname LOGOFF   ( sends this to all clients conneted )


i have done a hell lot but cant seem to do it in the order specified.........( i am sure the order is pretty comprehensible ) .......

Explaination : when a client gets connected , it types  LOGIN  clientname ....... "clientname" is extracted using subtring ....then server send    ->    clientname  hisIP  hisPORT    to all clients connected  , and when client A types LOGOFF , the server send notification to all clients that  client A has logged off ...

can plz any1 help in this and send me sum code stuff/part..........thanks !!!
0
Comment
Question by:DahaR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +1
14 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10974657
You should maintain a list of all client-Sockets or their PrintWriters in a static Vector or something like that, at the server-side. When a client connects, then add its PrintWriter to the Vector. When a client disconnects, then remove it. While adding/ removing, iterate through the Vector and send a message to all other clients.
0
 
LVL 1

Author Comment

by:DahaR
ID: 10977552
ok !!! but will        out.println(message);       command     ,  send the message to all clients connected or will i have to use synchronization.... or sumthing  ???
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Expert Comment

by:Mayank S
ID: 10978054
If you use a Vector, it is synchronized itself, so you don't need to worry about that. You can store all the PrintWriters in that Vector, loop through it and send the data using out.println ( message ). Be careful not to send it to the same client himself~!
0
 
LVL 1

Author Comment

by:DahaR
ID: 10981373
ok ......lemme check  !!!!  by the way ...have u done anything similar to this program !!! that i can get help from or see !!! thx!!!
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10981751
Hi all,

You can create a class for each client:

    public class ChatClient
    {
          Socket soc;
          String name,ip,port;  // information about the client
          PrintStream ps; // = new PrintStream( soc.getOutputStream() );
    }

>> If you use a Vector, it is synchronized itself
only during the Vector method call.

The client list may need some synchronization :


    public synchronized void addClient( ChatClient cc )
    {
         sendToAllClients("server: "+cc.name+", "+cc.ip+", "+cc.port);
         clients.add(cc);
    }

    public synchronized void removeClient( PrintStream ps )
    {
        clients.remove(cc);
        sendToAllClients("server: "+cc.name+" LOGOFF");
    }

    public synchronized void sendToAllClients( String msg )
    {
         int nb=clients.size();
         for (int i=0;i<nb;i++)
             ((ChatClient)clients.elementAt(i)).ps.println(msg);
    }


0
 
LVL 13

Accepted Solution

by:
Webstorm earned 375 total points
ID: 10981756
   public synchronized void removeClient( PrintStream ps )
Should be:
    public synchronized void removeClient( ChatClient cc )
0
 
LVL 1

Author Comment

by:DahaR
ID: 10981878
ok thanks man !!! i'll check it out and then tell ya !!!!!!!
0
 
LVL 1

Author Comment

by:DahaR
ID: 10982818
thanks a lot  WEBSTORM !!!!   yur idea really helped me !!!  

well i have osting another question regarding sending image from server to client using streams......so do check it out....remember my LOGIN plz !!!!!!! see ya ...........
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10983527
DahaR. do you know that you can split the points between experts who provide an idea and who provide a working solution?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10983528
>> only during the Vector method call.

Ah, right.
0
 
LVL 1

Author Comment

by:DahaR
ID: 10984558
sorry man , didnt know  that  :( .....will try to be careful next time .....n'ways thx for the info !!!
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10984814
Hi mayankeagle,

>> do you know that you can split the points ...
You can post a question to
http://www.experts-exchange.com/Community_Support/askQuestion.jsp

Or i can post a question to give you points.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10984964
No, Webstorm, not needed :-) thanks. I just wanted to let DahaR know that it is possible because I had a feeling he didn't know it. I don't have any issues with the PAQing of the question.
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question