multithreading - Java Multithread, Multi-Client Server- Client not executing run method -


trying better @ java, i'm attempting create mutlithreaded client & server messenger, in client sends message server, , server redirects & sends message user first client wanted send to. i've been researching lot concurrency , multithreading , such. current problem, i've concluded, bufferedreader/inputstream client isn't "ready" when start run thread, , returns nullpointerexception. few other questions:

  • should drawgui() method in swingutilities.invokelater() statement or not matter since i'm not interacting gui in method
  • wondering if hashmap usersip work key-value pair, pair clients ip. there's 1 of these in server class
  • was gonna ask question, forgot was. edit in later if remember

client code(cut down useful stuff)

public class client extends thread { public static socket socket; public static printstream os; public static bufferedreader is; public static stringbuffer toappend = new stringbuffer(""); public static stringbuffer tosend = new stringbuffer("");  //some variables use jmenubaritems   public static map<string, string> usersip = new hashmap<string, string>(); public static string[] friends;  //swing variables  public static void drawgui() {      //jmenubar set      //some jmenuitem stuff , adding action listeners      //adding jmenubaritems jmenubar      //main gui window set      //initializing jtextarea , setting properties     chatfield = new jtextfield();     chatfield.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent arg0) {             string s = chatfield.gettext();              if(!s.equals("")) {                 appendtochatbox(s);                 //textarea.append(username + "> " + s);                 chatfield.settext("");                 sendstring(s);                 system.out.println("in chat field: send: " + tosend.tostring());             }         }     });     //adding panels jframe             //setting stuff jframe }      //some of these methods below may unneeded, left them there in case public static void appendtochatbox(string s) {     synchronized(toappend) {         toappend.append(s);     } }  public static void appendtochatbox(string username, string s) {     synchronized(toappend) {         toappend.append(username + "> " + s);      } }  public static void sendstring(string s) {     synchronized(toappend) {         toappend.append(s);     } }  public static void cleanup() {     try {         if(socket != null) {             socket.close();             socket = null;          }     } catch(ioexception e) {         socket = null;      }      try {         if(is != null) {             is.close();             = null;          }      } catch(ioexception e) {         = null;      }      if(os != null) {         os.close();         os = null;     } }  public static void connect() {     try {         system.out.println("in connect method"); //debug          socket = new socket(host, port);         os = new printstream(socket.getoutputstream());         = new bufferedreader(new inputstreamreader(socket.getinputstream()));         appendtochatbox("successfully connected");         system.out.println("toappend in connect method: " + toappend.tostring()); //debug         system.out.println("successfully connected");      } catch(unknownhostexception e) {         system.err.println("don't know host: " + host);      } catch(ioexception e) {         system.err.println("couldn't i/o connection host " + host);     }      if(socket != null && os != null && != null) {         try {              while(!closed) {                 if(tosend.length() != 0) {                     system.out.println("in connect()> tosend.length isn't = 0"); //debug                     os.println(tosend);                     os.flush();                     tosend.setlength(0);                 }             }         } catch(exception e) {             system.err.println("error in sending message server " + e);             cleanup();         }     } }  public static void reconnect() {     cleanup();     connect();  }  public static void main(string args[]) {     swingutilities.invokelater(new thread(new runnable() {         @override         public void run() {             drawgui();          }     }));      new thread(new runnable() {         @override         public void run() {             system.out.println("attempting connect server..."); //debug             connect();         }     }).start();      new client().start(); }  @override public void run() {     string servermessage;      system.out.println("client.run() invoked"); //debug       try {         system.out.println("in try-catch statement in client.run()"); //debug          while(is.ready()) {             system.out.println("input stream isn't null & ready in client.run()"); //debug              servermessage = is.readline().trim();              if(servermessage != null && servermessage.length() != 0) {                 if(servermessage.equalsignorecase("client_disconnect")) {                     break;                  } else if(servermessage.equals(integer.tostring(username_request))) {                  } else {                     appendtochatbox("server" + servermessage);                     system.out.println("server> " + servermessage);                  }             }              if(tosend.length() > 0) { //debug                 system.out.println("tosend in client.run's while loop: " + tosend.tostring());              } else { //debug                 system.out.println("tosend empty");              }         }     } catch(exception e) {         e.printstacktrace();      }      if(toappend.length() != 0) {         appendtochatbox("from client.run() " + toappend.tostring());      } else { //debug         system.out.println("toappend empty");      } } 


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -