java - Display online users from server to all connected clients -


i'm building simple socket server-client application consisting of: server project (which creates new client handler thread when socket accepted) , client project (which establishing connection server , displaying gui message thread open).

so far i'm able connect using multiple clients , chat no problems. i've set command called !getusers should display connected users requesting client. when so, i'm unable continue , chat, client stuck, connected users list though.

as request here entire client code:

import java.io.*; import java.net.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import static java.lang.system.out; import java.util.logging.level; import java.util.logging.logger;  public class chatclient extends jframe implements actionlistener { string uname; printwriter pw; bufferedreader br; jtextarea tamessages, tauserlist; jtextfield tfinput; jbutton btnsend, btnexit; socket client;  public chatclient(string uname, string servername) throws exception {     super("connected as: " + uname);  // set title frame     this.uname = uname;     client = new socket(servername, 18524);     br = new bufferedreader(new inputstreamreader(client.getinputstream()));     pw = new printwriter(client.getoutputstream(), true);     pw.println(uname);  // send name server     //bring chat interface     buildinterface();     new messagesthread().start();  // create thread listens messages }  public void buildinterface() {     btnsend = new jbutton("send");     btnexit = new jbutton("exit");     //chat area     tamessages = new jtextarea();     tamessages.setrows(10);     tamessages.setcolumns(50);     tamessages.seteditable(false);     //online users list     tauserlist = new jtextarea();     tauserlist.setrows(10);     tauserlist.setcolumns(10);     tauserlist.seteditable(false);     //top panel (chat area , online users list     jscrollpane chatpanel = new jscrollpane(tamessages, jscrollpane.vertical_scrollbar_as_needed,             jscrollpane.horizontal_scrollbar_never);     jscrollpane onlineuserspanel = new jscrollpane(tauserlist, jscrollpane.vertical_scrollbar_as_needed,             jscrollpane.horizontal_scrollbar_never);     jpanel tp = new jpanel(new flowlayout());     tp.add(chatpanel);     tp.add(onlineuserspanel);     add(tp, "center");     //user input field     tfinput = new jtextfield(50);     //buttom panel (input field, send , exit)     jpanel bp = new jpanel(new flowlayout());     bp.add(tfinput);     bp.add(btnsend);     bp.add(btnexit);     add(bp, "south");     btnsend.addactionlistener(this);     tfinput.addactionlistener(this);//allow user press enter key in order send message     btnexit.addactionlistener(this);     setsize(500, 300);     setvisible(true);     pack(); }  @override public void actionperformed(actionevent evt) {     if (evt.getsource() == btnexit) {         pw.println("!end");  // send end server server know termination         system.exit(0);     } else if(tfinput.gettext().contains("!getusers")){             pw.println("!getusers");     }else{         // send message server         pw.println(tfinput.gettext());     } }  public static void main(string args[]) {      // take username user     string name = joptionpane.showinputdialog(null, "enter name: ", "username",             joptionpane.plain_message);     string servername = "localhost";     try {         new chatclient(name, servername);     } catch (exception ex) {         out.println("unable connect server.\nerror: " + ex.getmessage());     }  } // end of main  // inner class messages thread class messagesthread extends thread {      @override     public void run() {         string line;         try {             while (true) {                 line = br.readline();                 tamessages.append(line + "\n");                 tamessages.setcaretposition(tamessages.getdocument().getlength());//auto scroll last message             } // end of while         } catch (exception ex) {         }     }     } } //  end of client 

which accepted , handled server, following entire server code:

public class chatserver {      vector<string> users = new vector<string>();     vector<handleclient> clients = new vector<handleclient>();      public void process() throws exception {         serversocket server = new serversocket(18524);         out.println("server started...");         while (true) {             socket client = server.accept();             //add incoming client connected clients vector.             handleclient c = new handleclient(client);             clients.add(c);         }  // end of while     }      public static void main(string... args) throws exception {         new chatserver().process();     } // end of main      public void broadcast(string user, string message) {         // send message connected users         (handleclient c : clients) {             c.sendmessage(user, message);         }     }      /*      * inner class, responsible of handling incoming clients.      * each connected client set it's own thread.      */     class handleclient extends thread {          string name = "";//client name/username         bufferedreader input;//get input client         printwriter output;//send output client          public handleclient(socket client) throws exception {             // input , output streams             input = new bufferedreader(new inputstreamreader(client.getinputstream()));             output = new printwriter(client.getoutputstream(), true);             // read name             name = input.readline();             users.add(name); // add users vector             broadcast(name, " has connected!");             start();         }          public void sendmessage(string uname, string msg) {             output.println(uname + ": " + msg);         }          public void getonlineusers() {             (handleclient c : clients) {                 (int = 0; < users.size(); i++) {                     broadcast("", users.get(i));                 }             }         }          public string getusername() {             return name;         }          public void run() {             string line;             try {                 while (true) {                     line = input.readline();                     if (line.equals("!end")) {                         //notify user disconnection                         broadcast(name, " has disconnected!");                         clients.remove(this);                         users.remove(name);                         break;                     } else if(line.equals("!getusers")){                         getonlineusers();                         break;                     }                     broadcast(name, line); // method  of outer class - send messages                 } // end of while             } // try             catch (exception ex) {                 system.out.println(ex.getmessage());             }         } // end of run()     } // end of inner class } // end of server 

should defince new printwriter object handling onlineusers request? i'm sure i'm missing here, yet figure out exactly.

ah, have solved puzzle.

public void run() {         string line;         try {             while (true) {                 line = input.readline();                 if (line.equals("!end")) {                      // blah                 } else if(line.equals("!getusers")){                     getonlineusers();                     break;    << breaks read loop                 }                 broadcast(name, line); // method  of outer class - send messages             } // end of while         } // try         catch (exception ex) {             system.out.println(ex.getmessage());         }     } // end of run() 

the break statement in run() loop terminates reading loop, server no longer "listening" client. believe if remove break, should good.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -