sockets - Java Client Server - Multiple Event Handling for the Client -


i'm trying setup client server application using socket programming. client connects server, i'm unable multiple event handling work. client applet has 2 text boxes , buttons associated each 1 of of them. when click button one, trying "hello" displayed in text box. when click on button two, trying "hello there" displayed in second text box. however, 1 value (the value first click) shows in both of text boxes. event handling mechanism incorrect? implementing serializable interface , client server communication deals objects. can please tell me problem in code is? haven't posted objectcommunication.java code, implements serializable interface , has getter , setter (takes string input parameter) method.

many thanks!

the following server code:

import java.io.*; import java.net.*;  public class server_app {     public static void main(string[] args) {          try {             serversocket holder = new serversocket(4500);              (;;) {                 socket incoming = holder.accept();                 new serverthread(incoming).start();              }         } catch (exception e) {             system.out.println(e);         }     } }  class serverthread extends thread  {      public serverthread(socket i) {         incoming = i;     }      public void run() {         try {               objectcommunication hold = new objectcommunication();              objectinputstream input = new objectinputstream(incoming.getinputstream());              objectoutputstream output = new objectoutputstream(incoming.getoutputstream());               hold = (objectcommunication) input.readobject();              if ((hold.getmessage()).equals("event 1")) {                  system.out.println("message read: " + hold.getmessage());                  hold.setmessage("hello!");              } else if ((hold.getmessage()).equals("event 2")) {                 system.out.println("message read:" + hold.getmessage());                  hold.setmessage("hello there!");             }              output.writeobject(hold);              input.close();              output.close();              incoming.close();          } catch (exception e) {             system.out.println(e);         }     }      objectcommunication hold = null;     private socket incoming;  } 

the following client code:

import java.applet.applet; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*;  public class client_app extends applet {     textfield textval;     textfield anothertextval;     socket socket;     objectcommunication hold = new objectcommunication();     objectcommunication temp = new objectcommunication();     objectoutputstream outputstream;     objectinputstream inputstream;      public void init() {          socketconnection();          creategui();          validate();     }      public void socketconnection() {          try {             socket = new socket("127.0.0.1", 4500);         } catch (exception e) {             system.out.println("unknown host");         }          try {              outputstream = new objectoutputstream(socket.getoutputstream());             inputstream = new objectinputstream(socket.getinputstream());          } catch (ioexception ex) {             system.out.println("error: " + ex);             return;         }       }      public void creategui() {          button button = new button("hello button");          add(button);           button.addactionlistener(new actionlistener() {             public void actionperformed(actionevent evt) {                 button_actionperformed(evt);              }         });           textval = new textfield(6);         add(textval);          button anotherbutton = new button("hello there button");          add(anotherbutton);          anotherbutton.addactionlistener(new actionlistener() {              public void actionperformed(actionevent evt) {                 anotherbutton_actionperformed(evt);             }          });           anothertextval = new textfield(6);         add(anothertextval);       }      public void button_actionperformed(actionevent e) {            string actioncommand = e.getactioncommand();          if (e.getsource() instanceof button)             if (actioncommand.equals("hello button")) {                   try {                      temp.setmessage("event 1");                      //outputstream.writeobject(temp);                       new sendtoserver().start();                      new listentoserver().start();                   } catch (exception ex) {                     system.out.println("communication didn't work!");                 }                  textval.settext(hold.getmessage());             }     }      public void anotherbutton_actionperformed(actionevent evt) {         string action_command = evt.getactioncommand();          if (evt.getsource() instanceof button)             if (action_command.equals("hello there button")) {                   try {                      temp.setmessage("event 2");                      new sendtoserver().start();                      new listentoserver().start();                  } catch (exception ex) {                     system.out.println("communication didn't work!");                 }                  anothertextval.settext(hold.getmessage());             }       }      class listentoserver extends thread {         public void run() {             while (true) {                 try {                     hold = (objectcommunication) inputstream.readobject();                 } catch (ioexception e) {} catch (classnotfoundexception e2) {}             }         }     }      class sendtoserver extends thread {          public void run() {             while (true) {                 try {                     outputstream.writeobject(temp);                 } catch (ioexception e) {}             }         }     }    } 

to honest - i'm little bit lazy read through code , seek there bug :) nevertheless i'll post here snippet socket-based multiple client-server application..

import java.net.*; import java.io.*;  class serveconnection extends thread {         private socket socket = null;         private bufferedreader in = null;         private printwriter out = null;          public serveconnection(socket s) throws ioexception {                  // init connection client                 socket = s;                 try {                         in = new bufferedreader(new inputstreamreader(                                         this.socket.getinputstream()));                         out = new printwriter(this.socket.getoutputstream(), true);                 } catch (ioexception e) {                         system.err.println("couldn't i/o.");                         system.exit(1);                 }                                 start();         }          public void run() {                  system.out.println("client accepted from: " + socket.getinetaddress()                                 + ":" + socket.getport());             // commands client, until communicating or until no error            // occurs                 string inputline, outputline;                  try {                         while ((inputline = in.readline()) != null) {                                   system.out.println("request: " + inputline);                                 outputline = inputline;                                     out.println("i've recived "+outputline);                                                  } catch (ioexception e) {                         e.printstacktrace();                 }                  system.out.println("server ending");                 out.close();                 try {                         in.close();                         socket.close();                 } catch (ioexception e) {                         e.printstacktrace();                 }         } }  class server {         public static void svr_main(int port) throws ioexception {                 serversocket serversocket = null;                 try {                         serversocket = new serversocket(port);                 } catch (ioexception e) {                         system.err.println("could not listen on port: " + port);                         system.exit(1);                 }                  system.out.println("server ready");                  try {                         while (true) {                                 socket socket = serversocket.accept();                                 try {                                         new serveconnection(socket);                                 } catch (ioexception e) {                                         system.err.println("io exception");                                 }                         }                 } {                         serversocket.close();                 }         } }  class client {               static socket echosocket = null;         static printwriter out = null;         static bufferedreader in = null;              public static void cli_main(int port, string servername) throws ioexception {                 try {                         echosocket = new socket(servername, port);                         out = new printwriter(echosocket.getoutputstream(), true);                         in = new bufferedreader(new inputstreamreader(                                         echosocket.getinputstream()));                 } catch (unknownhostexception e) {                         system.err.println("don't know host: " + servername);                         system.exit(1);                 } catch (ioexception e) {                         system.err.println("couldn't i/o " + servername);                         system.exit(1);                 }                  system.out.println("client ready!");                 while (true) {                          inputline = (in.readline().tostring());                         if (inputline == null) {                                 system.out.println("client closing!");                                 break;                         }                          // input , tokenize                         string[] tokens = inputline.split(" ");                   }                  out.close();                 in.close();                 echosocket.close();                 system.out.println("client closing");         } }  public class myclientserversnippet{         public static void main(string[] args) throws ioexception {                 if (args.length == 0) {                         system.err.println("client: java snippet.myclientserversnippet<hostname> <port>");                         system.err.println("server: java snippet.myclientserversnippet<port>");                          system.exit(1);                 }                 else if (args.length > 1) {                                            system.out.println("starting client...\n");                         client client = new client();                         client.cli_main(3049, "127.0.0.1");                 } else {                         system.out.println("starting server...\n");                         server server = new server();                         server.svr_main(3049);                 }         } } 

hope helps :] if ununderstandable, don't hesitate ask more details :)


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -