java - Making Instant Messaging Program, Running Incorrectly? -
i running server ensure works. when run servertest.java, "closing connections in textarea", expected have "waiting connect..." within textfield. tell me, problem is?
servertest.java
import javax.swing.jframe; public class servertest { public static void main(string[] args){ server spirit = new server(); spirit.setdefaultcloseoperation(jframe.exit_on_close); spirit.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; //server constructor public server(){ super("sim"); 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); } public void startrunning(){ try{ server = new serversocket (6798, 100); // parameters port , backlog while(true){ try{ waitforconnection();//waits connection setupstreams(); //sets connection whilechatting();//once connected, sends message between each other }catch(eofexception eofexception){ showmessage("\n server ended connection!"); //once streams been closed server }finally{ closecrap(); } } }catch(ioexception ioexception){ ioexception.printstacktrace(); } } //connection waiting private void waitforconnection() throws ioexception{ showmessage("waiting connect... \n"); connection = server.accept(); //listens connection made socket , accepts showmessage("connected to" + connection+getinetaddress().gethostname() + " \n"); //dispays ip of connected } //exchange data private void setupstreams() throws ioexception{ output = new objectoutputstream(connection.getoutputstream()); output.flush(); //flushes stream input = new objectinputstream(connection.getinputstream()); showmessage("\n streams accepted \n"); } //initiates chat private void whilechatting() throws ioexception { string message = "you connected"; sendmessage(message); abletotype(true); //allows user type do{ try{ message = (string) input.readobject(); showmessage("\n"+ message); //sent messages }catch(classnotfoundexception classnotfoundexception){ //if odd sent i.e string not sent showmessage("\n unknown message sent! \n"); } }while(!message.equals("client - end")); } //closes program private void closecrap(){ showmessage("\n closing connections... \n"); abletotype(false); try{ output.close(); //closes connection client input.close(); //closes connection client connection.close(); //closes socket }catch(ioexception ioexception){ ioexception.printstacktrace(); } } //send message client private void sendmessage(string message){ try{ output.writeobject("admin/server - " + message); output.flush(); showmessage("\nadmin/server - " + message); }catch(ioexception ioexception){ chatwindow.append("\n message not sent \n"); } } //update chatwindow private void showmessage(final string text){ swingutilities.invokelater( //creates thread update gui new runnable(){ public void run(){ chatwindow.append(text); //adds end of chattwindow } } ); } //to type private void abletotype(final boolean tof){ swingutilities.invokelater( //creates thread update gui new runnable(){ public void run(){ usertext.seteditable(tof); //updates gui, allow type } } ); } }
i have been following thenewboston tutorials.
my error waitforconnection() constructor
private void waitforconnection() throws ioexception{ showmessage("waiting connect... \n"); connection = server.accept(); //listens connection made socket , accepts showmessage("connected to" + connection+getinetaddress().gethostname() + " \n"); //dispays ip of connected }
on line 4 should "." not "+"
+ connection.getinetaddress().gethostname() +
it typing error.
Comments
Post a Comment