Issue with Java Server class and socket connections -


i trying set server class, , i'm running issue in no error being thrown.

import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class server extends jframe implements runnable{  private static final long serialversionuid = 1l; private jtextfield usertext; private jtextarea chatwindow; private objectoutputstream output; private objectinputstream input; private serversocket server; private socket connection;  //constructor public server(){     super("server");     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); //sets window size     setdefaultcloseoperation(jframe.exit_on_close);     setvisible(true);    }  public void run(){     try{         server = new serversocket(6789, 100); //6789 dummy port testing, can changed. 100 maximum people waiting connect.         while(true){             try{                 //trying connect , have conversation                 waitforconnection();                 setupstreams();                 whilechatting();             }catch(eofexception eofexception){                 showmessage("\n server ended connection! ");             } finally{                 closeconnection(); //changed name more appropriate             }         }     } 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("the user has sent unknown object!");         }     }while(!message.equals("client - end")); }  public void closeconnection(){     showmessage("\n closing connections... \n");     abletotype(false);     try{         output.close(); //closes output path client         input.close(); //closes input path server, client.         connection.close(); //closes connection between can client     }catch(ioexception ioexception){         ioexception.printstacktrace();     } }  //send mesage client private void sendmessage(string message){     try{         output.writeobject("server - " + message);         output.flush();         showmessage("\nserver -" + message);     }catch(ioexception ioexception){         chatwindow.append("\n error: cannot send message, please retry");     } }  //update chatwindow private void showmessage(final string text){     swingutilities.invokelater(         new runnable(){             public void run(){                 chatwindow.append(text);             }         }     ); }  private void abletotype(final boolean tof){     swingutilities.invokelater(         new runnable(){             public void run(){                 usertext.seteditable(tof);             }         }     );  }  }    import java.awt.dimension; import java.awt.event.actionevent; import java.awt.event.actionlistener;   import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.joptionpane;  public class menu extends jframe implements actionlistener{  private static final long serialversionuid = 1l; private jbutton server; private jbutton client; private static string host;  public menu(){      this.getcontentpane().setpreferredsize(new dimension(300, 300));      this.setdefaultcloseoperation(jframe.exit_on_close);     this.setlayout(null);     this.pack();      this.setlocationrelativeto(null);      server = new jbutton();     server.settext("server");     server.setbounds(0, 0, 300, 150);     server.addactionlistener(this);     client = new jbutton();     client.settext("client");     client.setbounds(0, 150, 300, 150);     client.addactionlistener(this);      this.add(server);     this.add(client);      this.setvisible(true); }   public void actionperformed(actionevent e) {     if(e.getsource() == server){         server s = new server();         s.run();     }     if(e.getsource() == client){         host = joptionpane.showinputdialog("enter server i.p.");         client c = new client(host);         c.run();     }  }  public static void main(string[] args){     new menu(); }  } 

the jframe created, can exited termination button in eclipse, not default_exit_on_close operation, , see through (not opaque should be). client class acts same way, leading me believe issue the:

 server s = new server();         s.run();  

since if have main method call that, works fine.

your constructor can never exit.

this waitforconnection()/setupstreams() logic not appropriate. need accept loop accepts sockets, constructs runnable handle connection, , starts thread. loop should in separate run() method , executed in separate thread. not in constructor.

nb socket returned accept() must local variable in loop, otherwise liable run concurrency problems.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -