Link to home
Start Free TrialLog in
Avatar of DahaR
DahaR

asked on

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

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 !!!
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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.
Avatar of DahaR
DahaR

ASKER

ok !!! but will        out.println(message);       command     ,  send the message to all clients connected or will i have to use synchronization.... or sumthing  ???
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~!
Avatar of DahaR

ASKER

ok ......lemme check  !!!!  by the way ...have u done anything similar to this program !!! that i can get help from or see !!! thx!!!
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);
    }


ASKER CERTIFIED SOLUTION
Avatar of Webstorm
Webstorm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DahaR

ASKER

ok thanks man !!! i'll check it out and then tell ya !!!!!!!
Avatar of DahaR

ASKER

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 ...........
DahaR. do you know that you can split the points between experts who provide an idea and who provide a working solution?
>> only during the Vector method call.

Ah, right.
Avatar of DahaR

ASKER

sorry man , didnt know  that  :( .....will try to be careful next time .....n'ways thx for the info !!!
Hi mayankeagle,

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

Or i can post a question to give you points.
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.