java - Multi-client chat program, broadcasting chat to all clients? -


i'm trying make simple chat program can accommodate multiple clients. have multi-threaded server , can connect multiple clients it, server communicates single client (as should, each client on own thread) need getting server send messages connected clients each client. think i'd need share single object between threads? here's code:

server:

import java.net.serversocket; import java.net.socket;   public class threadedcommandserver {      public static void main(string[] args) throws exception {         system.out.println("starting server....");         int port = 8989;           serversocket ss = new serversocket(port);           while(true) {             system.out.println("waiting connection client...");             socket socket = ss.accept();             serverthread st = new serverthread(socket);             st.start();          }     }  } 

server thread:

import java.io.inputstream; import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.outputstream; import java.net.socket;  public class serverthread extends thread {      private socket socket = null;     private string s;      public serverthread(socket s) {         socket = s;      }      public void run() {         try {             inputstream = socket.getinputstream();             objectinputstream ois = new objectinputstream(is);             s = (string) ois.readobject();             system.out.println("received string: " + s);             outputstream os = socket.getoutputstream();             objectoutputstream oos = new objectoutputstream(os);             oos.writeobject(s);             system.out.println("sent object client...");         } catch (exception e) {             system.out.println(e.tostring());         }      }  } 

client:

import java.io.inputstream; import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.outputstream; import java.net.inetaddress; import java.net.socket;   public class controller {  private clientframe cf; private string message; private socket socket; private string response;  public controller(clientframe cf) {     this.cf = cf; }   public void sendchat(string s) {     message = s;     system.out.println("from controller: received: " + message);     try {     inetaddress = inetaddress.getbyname("localhost");     socket= new socket(i, 8989);     outputstream os = socket.getoutputstream();     objectoutputstream oos = new objectoutputstream(os);     system.out.println("from controller: sending message server...");     oos.writeobject(message);     inputstream = socket.getinputstream();     objectinputstream ois = new objectinputstream(is);     system.out.println("getting string server....");     response = (string) ois.readobject();     cf.updategui(response);     }     catch (exception e) {         e.printstacktrace();     } }  } 

gui:

   import java.awt.borderlayout;     import java.awt.event.actionevent;     import java.awt.event.actionlistener;     import javax.swing.jbutton;     import javax.swing.jframe;     import javax.swing.jpanel;     import javax.swing.jscrollpane;     import javax.swing.jtextarea;     import javax.swing.jtextfield;      public class clientframe extends jframe {      private jtextarea chatarea;     private jtextfield type;     private jbutton submit;     private jpanel upper;     private jpanel lower;     private borderlayout bl;     jscrollpane jsp;     controller c;      public clientframe() {         bl = new borderlayout();         upper = new jpanel();         lower = new jpanel();         chatarea = new jtextarea("chat goes here", 20, 30);         c = new controller(this);         chatarea.seteditable(false);         chatarea.setlinewrap(true);         type = new jtextfield("type here", 20);         jsp = new jscrollpane(chatarea);         new smartscroller(jsp);         lower.add(type);         submit = new jbutton("send");         submit.addactionlistener(new submit());         type.addactionlistener(new submit());         lower.add(submit);         upper.add(jsp);         this.setsize(400, 600);         this.setlayout(bl);         this.settitle("mattchatt");         this.add(upper, borderlayout.north);         this.add(lower, borderlayout.south);         this.setvisible(true);         this.setresizable(false);         this.setdefaultcloseoperation(exit_on_close);     }      public void updategui(string s) {         chatarea.append("\n" + s);     }      private class submit implements actionlistener {          @override         public void actionperformed(actionevent e) {              if (type.gettext().equals("")) {                 system.out.println("no text entered");             } else {                 system.out.println("submitted");                 c.sendchat(type.gettext());                 type.settext("");              }         }      }      } 

you have no way of accessing of serverthreads create. need group them in collection, such arraylist.

or better, hashmap, if ever want expand application allow private messaging, need access specific serverthread, , hashmap can easily.

anyways... when time send out message, should loop collection through , send message outputstreams associated each serverthread.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -