java - How to have multiple clients access my chat room (bucky's instant mssager) -


hi downloaded bucky's messager , instead of having multiple clients connected it, has them wait connect server. how make multiple clients connect server @ once? love if edit code me , tell me edit. here code:

client.java:

import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class client extends jframe{     private jtextfield usertext;    private jtextarea chatwindow;    private objectoutputstream output;    private objectinputstream input;    private string message = "";    private string serverip;    private socket connection;     //constructor    public client(string host){       super("client mofo!");       serverip = host;       usertext = new jtextfield();       usertext.seteditable(false);       usertext.addactionlistener(          new actionlistener(){             public void actionperformed(actionevent event){                sendmessage(event.getactioncommand());                usertext.settext("");             }          }       );       add(usertext, borderlayout.north);       chatwindow = new jtextarea();       add(new jscrollpane(chatwindow), borderlayout.center);       setsize(300,150);       setvisible(true);    }     //connect server    public void startrunning(){       try{          connecttoserver();          setupstreams();          whilechatting();       }catch(eofexception eofexception){          showmessage("\n client terminated connection");       }catch(ioexception ioexception){          ioexception.printstacktrace();       }finally{          closecrap();       }    }     //connect server    private void connecttoserver() throws ioexception{       showmessage("attempting connection... \n");       connection = new socket(inetaddress.getbyname(serverip), 6789);       showmessage("connected to: " + connection.getinetaddress().gethostname() );    }     //set streams send , receive messages    private void setupstreams() throws ioexception{       output = new objectoutputstream(connection.getoutputstream());       output.flush();       input = new objectinputstream(connection.getinputstream());       showmessage("\n dude streams go! \n");    }     //while chatting server    private void whilechatting() throws ioexception{       abletotype(true);       do{          try{             message = (string) input.readobject();             showmessage("\n" + message);          }catch(classnotfoundexception classnotfoundexception){             showmessage("\n dont know object type");          }       }while(!message.equals("server - end"));    }     //close streams , sockets    private void closecrap(){       showmessage("\n closing crap down...");       abletotype(false);       try{          output.close();          input.close();          connection.close();       }catch(ioexception ioexception){          ioexception.printstacktrace();       }    }     //send messages server    private void sendmessage(string message){       try{          output.writeobject("client - " + message);          output.flush();          showmessage("\nclient - " + message);       }catch(ioexception ioexception){          chatwindow.append("\n messed sending message hoss!");       }    }     //change/update chatwindow    private void showmessage(final string m){       swingutilities.invokelater(          new runnable(){             public void run(){                chatwindow.append(m);             }          }       );    }     //gives user permission type crap text box    private void abletotype(final boolean tof){       swingutilities.invokelater(          new runnable(){             public void run(){                usertext.seteditable(tof);             }          }       );          } } 

clienttest.java:

import javax.swing.jframe;  public class clienttest {    public static void main(string[] args) {       client charlie;       charlie = new client("127.0.0.1");       charlie.setdefaultcloseoperation(jframe.exit_on_close);       charlie.startrunning();    } } 

server.java:

import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class server extends jframe{     private jtextfield usertext;    private jtextarea chatwindow;    private objectoutputstream output;    private objectinputstream input;    private serversocket server;    private socket connection;     //constructor    public server(){       super("buckys instant messenger");       usertext = new jtextfield();       usertext.seteditable(false);       usertext.addactionlistener(          new actionlistener(){             public void actionperformed(actionevent event){                sendmessage(event.getactioncommand());                usertext.settext("");             }          }       );       add(usertext, borderlayout.north);       chatwindow = new jtextarea();       add(new jscrollpane(chatwindow));       setsize(300,150);       setvisible(true);    }     //set , run server    public void startrunning(){       try{          server = new serversocket(6789, 100);          while(true){             try{                waitforconnection();                setupstreams();                whilechatting();             }catch(eofexception eofexception){                showmessage("\n server ended connection! ");             }finally{                closecrap();             }          }       }catch(ioexception ioexception){          ioexception.printstacktrace();       }    }     //wait connection, display connection information    private void waitforconnection() throws ioexception{       showmessage(" waiting connect... \n");       connection = server.accept();       showmessage(" connected " + connection.getinetaddress().gethostname());    }     //get stream send , receive data    private void setupstreams() throws ioexception{       output = new objectoutputstream(connection.getoutputstream());       output.flush();       input = new objectinputstream(connection.getinputstream());       showmessage("\n streams setup! \n");    }     //during chat conversation    private void whilechatting() throws ioexception{       string message = " connected! ";       sendmessage(message);       abletotype(true);       do{          try{             message = (string) input.readobject();             showmessage("\n" + message);          }catch(classnotfoundexception classnotfoundexception){             showmessage("\n idk wtf user sent!");          }       }while(!message.equals("client - end"));    }     //close streams , sockets after done chatting    private void closecrap(){       showmessage("\n closing connections... \n");       abletotype(false);       try{          output.close();          input.close();          connection.close();       }catch(ioexception ioexception){          ioexception.printstacktrace();       }    }     //send message client    private void sendmessage(string message){       try{          output.writeobject("server - " + message);          output.flush();          showmessage("\nserver - " + message);       }catch(ioexception ioexception){          chatwindow.append("\n error: dude cant send message");       }    }     //updates chatwindow    private void showmessage(final string text){       swingutilities.invokelater(          new runnable(){             public void run(){                chatwindow.append(text);             }          }       );    }     //let user type stuff box    private void abletotype(final boolean tof){       swingutilities.invokelater(          new runnable(){             public void run(){                usertext.seteditable(tof);             }          }       );    }  } 

servertest.java:

import javax.swing.jframe;  public class servertest {    public static void main(string[] args) {       server sally = new server();       sally.setdefaultcloseoperation(jframe.exit_on_close);       sally.startrunning();    } } 

make servertest.java in form of thread , allow multiple clients


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -