sockets - Java - Send between clients -
i have 2 clients, , server in between. trying send information text between clients.
it works first 2 seconds, receives information other client.
here server code handles message sending:
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 objectoutputstream output2; private objectinputstream input2; private serversocket server; private socket connection; private socket connection2; private serversocket server2; private string message; private string message2; //constructor public server(){ super("server"); usertext = new jtextfield(); usertext.seteditable(false); usertext.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ try { sendmessage(); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } 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); server2 = new serversocket(6788,100); while(true){ try{ waitforconnection(); waitforconnection2(); setupstreams(); whilechatting(); }catch(eofexception eofexception){ showmessage("\n server ended connection! "); }finally{ closeconnection(); } } }catch(ioexception ioexception){ ioexception.printstacktrace(); } } //wait connection, display connection information private void waitforconnection() throws ioexception{ showmessage(" waiting connect... \n"); connection = server.accept(); showmessage(" connected client 1 \n"); // } private void waitforconnection2() throws ioexception{ showmessage("waiting connection"); connection2 = server2.accept(); showmessage("now connected client 2 \n"); } //get stream send , receive data private void setupstreams() throws ioexception{ output = new objectoutputstream(connection.getoutputstream()); output.flush(); input = new objectinputstream(connection.getinputstream()); output2 = new objectoutputstream(connection2.getoutputstream()); input2 = new objectinputstream(connection2.getinputstream()); showmessage("\n streams setup! \n"); } // during chat conversation private void whilechatting() throws ioexception{ string message = " connected! "; string message2 = "jiiii"; try { sendmessage(); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } abletotype(true); // while(!message.equals("client - end")){ // try{ // message = (string) input2.readobject(); // message2 = (string) input.readobject(); // // //// showmessage("\n" + message); //// showmessage("\n" + message2); // }catch(classnotfoundexception classnotfoundexception){ // showmessage("\n not valid"); // } // } } //close streams , sockets after done chatting private void closeconnection(){ showmessage("\n closing connections... \n"); abletotype(false); try{ output2.close(); input2.close(); connection2.close(); output.close(); input.close(); connection.close(); }catch(ioexception ioexception){ ioexception.printstacktrace(); } } //send message client private void sendmessage() throws classnotfoundexception{ boolean msg1 = true; boolean msg2 = false; try{ while(msg1 == true){ message = (string)input.readobject(); output2.writeobject("phone" + message); output2.flush(); msg1 = false; msg2 = true; } while(msg2 == true){ message2 = (string)input2.readobject(); output.writeobject(message2); output.flush(); msg1 = true; } }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); } } ); } }
let's go through code of sendmessage method.
private void sendmessage() throws classnotfoundexception { boolean msg1 = true; boolean msg2 = false; try{ while(msg1 == true){ message = (string)input.readobject(); output2.writeobject("phone" + message); output2.flush(); msg1 = false; msg2 = true; } while(msg2 == true){ message2 = (string)input2.readobject(); output.writeobject(message2); output.flush(); msg1 = true; } } catch(ioexception ioexception){ chatwindow.append("\n error: dude cant send message"); } }
in second while-loop, don't put msg2 false, keeps running forever in while-loop. that's why stop receiving messages first client , receive messages second client.
but when correct this, program stop after 1 message each client. fix this, need put 2 while-loops inside while-loop, while(true) loop example, can alternate.
but introduces yet problem, not able stop program while running. possible solution introduce local variable true, changes false when 1 of clients enters specific word ("quit" example).
my example in code:
private void sendmessage() throws classnotfoundexception { boolean msg1 = true; boolean msg2 = false; boolean not-quit? = true; //boolean stop program try{ while(not-quit?) { while(msg1 == true){ message = (string)input.readobject(); if (message.equalsignorecase("quit") { not-quit? = false; } output2.writeobject("phone" + message); output2.flush(); msg1 = false; msg2 = true; } while(msg2 == true){ message2 = (string)input2.readobject(); if (message.equalsignorecase("quit") { not-quit? = false; } output.writeobject(message2); output.flush(); msg1 = true; msg2 = false; //so stops while-loop } } } catch(ioexception ioexception){ chatwindow.append("\n error: dude cant send message"); } }
this style of client-server programming using not good. suggestion can make implement server threads, each instance of communication client in separate thread, , can let threads communicate via global variables in server. cleaner way of working!
Comments
Post a Comment