Java Instant Messenger - Contact List -


i'm building first instant messaging app. using various tutorials (the new boston; head first java) have implemented working client-server can send receive/messages between one-another.

i wish add advanced features,such contact list allows me add friends, see when friends online/offline. i'm trying avoid 3rd party apis (e.g. smack), wish learn basics. unfortunately, online tutorials i've read don't go beyond setting basic two-party client-server model.

my question this: how implement basic contact list links below server.java , client.java?

many help.

server.java:

import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*;  import javax.swing.*;  import java.util.date;//timestamp functionality   public class server extends jframe{ //inherits jframe      //1. instance variables      private jtextfield usertext; //where messages typed     private jtextarea chatwindow; //where messages displayed     private string fulltimestamp = new java.text.simpledateformat("dd/mm/yyyy hh:mm:ss").format(new date());     //fulltimestamp - mm = months; mm = minutes; hh = 24-hour clock      //setting streams     private objectoutputstream output; //messages being sent user     private objectinputstream input; //messages being received user;      private serversocket server;      private socket connection; //socket = sets connection between 1 computer , another.      //2. constructor (gui)     public server(){     super("mick's instant messenger [server]"); //window title     usertext = new jtextfield();     usertext.seteditable(false); //you cannot type anything, unless connected else     usertext.addactionlistener(          new actionlistener(){         public void actionperformed(actionevent event){             sendmessage(event.getactioncommand());             usertext.settext(""); //resets editable text field after send message         }         }     );     add(usertext, borderlayout.south);//places user text input (jtextarea) field @ bottom     chatwindow = new jtextarea(15,30); //displays conversation     add(new jscrollpane(chatwindow));     chatwindow.setlinewrap(true); //wraps lines when outgrow panel width     chatwindow.setwrapstyleword(true); //ensures above line wrap occurs @ word end     setsize(400,320);     setvisible(true); //set visible on screen     }      //3.setup , run server     public void startrunning(){         try{             server = new serversocket(6789, 100);             //client connects @ port # 6789             //100 = queuelength - backlog of clients can wait @ port #6789 connect server              //while(true) ... means while loop going run forever             while(true){                 try{                     waitforconnection();                     setupstreams();                     whilechatting(); //allows messages pass , forth through streams                 }catch(eofexception eofexception){                      //connect , have conversation                     //display timestamp disconnection                     showmessage("\n\n" + fulltimestamp);                     showmessage("\nconnection terminated server! "); //displays end of stream/connection                   }finally{                     closecrap();                 }             }         }catch(ioexception ioexception){             ioexception.printstacktrace(); //displays info there's error!     }     }          //4. wait connection method, display connection info         private void waitforconnection() throws ioexception{             showmessage("\n server : waiting user(s) connect... \n "); //tells user server waiting connection             connection = server.accept();             //socket "connection" accept connections. creates socket each new connection.             showmessage("connected " +connection.getinetaddress().gethostname()); //displays ip address , hostname          }          //5. setup streams method.. streams send , receive data         private void setupstreams() throws ioexception{             output = new objectoutputstream(connection.getoutputstream());             //creates pathway allow connection whichever computer 'connnection' socket created.             output.flush(); //clears data gets left on in buffer when try connect else. flushes on other person.             input = new objectinputstream(connection.getinputstream());             //no flush here, because cannot flush else's stream             showmessage("\n streams setup! \n");          }          //6. during conversation method          private void whilechatting() throws ioexception{              //display timestamp connection             showmessage("\n" + fulltimestamp);              string message = "you connected! \n ";             sendmessage(message);             abletotype(true); //will allow user type text box after connection             string timestamp = new java.text.simpledateformat("hh:mm:ss").format(new date());//timestamp              do{                 try{                     message = (string) input.readobject(); //read incoming message string , store in 'message' variable.                     showmessage("\n" + message);//displays each message receive on new line                 }catch(classnotfoundexception classnotfoundexception){                     showmessage("/n don't know object user has sent!");                 }              //***broken timestamp?***                 }while(!message.equalsignorecase("client " + "[" + timestamp  + "]" + ": " + "end")); //allows conversation until client enters "end"          }          //7. close crap method - close streams , sockets after finished chatting         private void closecrap(){             showmessage("\n\n closing connections... \n");             abletotype(false);             try{                 output.close(); //close stream other users                 input.close(); //close incoming streams                 connection.close(); //close socket              }catch(ioexception ioexception){                 ioexception.printstacktrace();             } }         //8. send message method - send message client         private void sendmessage(string message){             try{                  //writeobject method built java.                  string timestamp = new java.text.simpledateformat("hh:mm:ss").format(new date());//timestamp                 output.writeobject("server" + " [" + timestamp + "]" + ": " + message);                 showmessage("\nserver" + " [" + timestamp + "]" + ": " + message); //shows ouput message in our conversation window                 output.flush();             }catch(ioexception ioexception){                 chatwindow.append("error: unable send message!");             }         }          //9. updates chatwindow - instead of creating entirely new gui each time         private void showmessage(final string text){         swingutilities.invokelater(                 new runnable(){                     public void run(){                         chatwindow.seteditable(false); //disallows text editing in chatwindow                         chatwindow.append(text); //appends text, passed in above                     }                 }                 );   }         //10. lets user type         private void abletotype(final boolean tof){             swingutilities.invokelater(                     new runnable(){                         public void run(){                             usertext.seteditable(tof); //passes in 'true'                         }                     }                     );            } } 

client.java:

import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import java.util.date; //timestamp functionality  import javax.swing.*;  public class client extends jframe { //inherits jframe      //1. creating instance variables      private jtextfield usertext; //where user inputs text     private jtextarea chatwindow; //where messages displayed     private string fulltimestamp = new java.text.simpledateformat("dd/mm/yyyy hh:mm:ss").format(new date());     //fulltimestamp - mm = months; mm = minutes; hh = 24-hour cloc     private objectoutputstream output; //output client server     private objectinputstream input; //messages received server     private string message ="";     private string serverip;     private socket connection;      //2. constructor (gui)     public client(string host){ //host=ip address of server         super("mick's instant messenger [client]");         serverip = host; //placed here allow access private string serverip         usertext = new jtextfield();         usertext.seteditable(false);         usertext.addactionlistener(             new actionlistener(){                 public void actionperformed(actionevent event){                     sendmessage(event.getactioncommand()); //for work, must build senddata method                     usertext.settext(""); //resets usertext blank, after message has been sent allow new message(s)                 }             }                  );         add(usertext, borderlayout.south);         chatwindow = new jtextarea();         add(new jscrollpane(chatwindow), borderlayout.center); //allows scroll , down when text outgrows chatwindow         chatwindow.setlinewrap(true); //wraps lines when outgrow panel width         chatwindow.setwrapstyleword(true); //ensures above line wrap occurs @ word end         setsize(400,320);         setvisible(true);     }      //3. startrunning method     public void startrunning(){         try{             connecttoserver(); //unlike server, no need wait connections. connects 1 specific server.             setupstreams();             whilechatting();         }catch(eofexception eofexception){               //display timestamp disconnection             showmessage("\n\n" + fulltimestamp);             showmessage("\nconnection terminated client! ");           }catch(ioexception ioexception){             ioexception.printstacktrace();         }finally{                 closecrap();             }      }      //4. connect server     private void connecttoserver() throws ioexception{         showmessage(" \n attempting connection server... \n");         connection = new socket(inetaddress.getbyname(serverip), 6789);//server ip can added later         showmessage(" connected to: " +connection.getinetaddress().gethostname() ); //displays ip address of server     }      //5. setup streams send , receive messages     private void setupstreams() throws ioexception{         output = new objectoutputstream(connection.getoutputstream());         output.flush();         input = new objectinputstream(connection.getinputstream());         showmessage("\n streams setup! \n");      }      //6. while chatting method     private void whilechatting() throws ioexception{           //display timestamp connection         showmessage("\n" + fulltimestamp);          abletotype(true);         string timestamp = new java.text.simpledateformat("hh:mm:ss").format(new date());//timestamp         do{             try{                 message = (string) input.readobject(); //read input, treat string, store in message variable                 showmessage("\n" + message);             }catch(classnotfoundexception classnotfoundexception){                 showmessage("\n don't know object type");             }          //***broken timestamp?***             }while(!message.equalsignorecase("server " + "[" + timestamp  + "]" + ": " + "end")); //conversation happens until server inputs 'end'      }      //7. close streams , sockets     private void closecrap(){         showmessage("\n\nclosing streams , sockets...");         abletotype(false);//disable typing feature when closing streams , sockets         try{             output.close();             input.close();             connection.close();         }catch(ioexception ioexception){             ioexception.printstacktrace(); //show error messages or exceptions         }     }      //8. send messages server     private void sendmessage(string message){         try{         string timestamp = new java.text.simpledateformat("hh:mm:ss").format(new date());//timestamp         output.writeobject("client" + " [" + timestamp + "]" + ": " + message);         output.flush();         showmessage("\nclient" + " [" + timestamp + "]" + ": " + message);     }catch(ioexception ioexception){         chatwindow.append("\n error: message not sent!");     }      }      //9.change/update chatwindow     private void showmessage(final string m){         swingutilities.invokelater(                 new runnable(){                     public void run(){                         chatwindow.seteditable(false); //disallows text editing in chatwindow                         chatwindow.append(m); //appends text, passed in above                     }                 }                  );     }      //10. lets user type             private void abletotype(final boolean tof){                 swingutilities.invokelater(                         new runnable(){                             public void run(){                                 usertext.seteditable(tof); //passes in 'true' }         }                          );             } } 

i assume want persistent contact list , not 1 one session only. therefore 1 solution create accounts based on username , associate id:

example friend logs onto im name fred, if new name give id , save somewhere (e.g. text file or simple database)

if add fred contact need separate text file or database table hold contact details each user. doing linking fred's id own.

then, when next go im server should bring list of contacts wherever saved


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -