java - Not catching Exception in server (unless I add a breakpoint) (Solved) -


so have try/catch catch catching exception. purpose remove client list of connected clients if client disconnects without notifying server. reason though not catching. weird part when add breakpoint @ catch (i'm using eclipse btw) breaks , when continue, catches , continue catch until restart server. @ point client connects server , dies. server doesn't catch , stops responding if try , close server window. way stop if doesn't catch terminate it.

so clarify code works, issue solely catch not catching results in server not realizing client has left server. adding breakpoint fixes reason problem.

here of code server:

35 public static arraylist<socket> list_sockets = new arraylist<socket>(); 36 public static arraylist<integer> list_client_states = new arraylist<integer>(); 37 public static arraylist<datapackage> list_data = new arraylist<datapackage>(); 38 39 private static runnable accept = new runnable() { 40  @override 41  public void run() { 42      new thread(send).start(); 43      new thread(receive).start(); 44       45      while (true) { 46          try { 47              socket socket = server.accept(); 48               49              objectinputstream ois = new objectinputstream(socket.getinputstream()); 50               51              string username = (string) ois.readobject(); 52               53              objectoutputstream oos = new objectoutputstream(socket.getoutputstream()); 54              oos.writeobject("welcome..."); 55               56              list_clients_model.addelement(username + " - " + socket.getinetaddress().gethostaddress() + " - " + socket.getinetaddress().gethostname()); 57              list_client_states.add(0); 58               59              list_data.add(new datapackage()); 60              list_sockets.add(socket); 61          } 62          catch (exception e) {}   63      }    64  } 65 }; 66 67 private static runnable send = new runnable() { 68  @override 69  public void run() { 70      objectoutputstream oos; 71       72      while (true) { 73          (int = 0; < list_sockets.size(); i++){ 74              try { 75                  oos = new objectoutputstream(list_sockets.get(i).getoutputstream()); 76                  int client_state = list_client_states.get(i); 77                  oos.writeobject(client_state); 78                   79                  oos = new objectoutputstream(list_sockets.get(i).getoutputstream()); 80                  oos.writeobject(list_data); 81                   82                  if (client_state == 1) { // kicked server 83                      disconnectclient(i); 84                      i--; 85                  } 86                  else if (client_state == 2) { // server disconnected 87                      disconnectclient(i); 88                      i--; 89                  } 90              } 91              catch (exception e) {} 92          } 93      } 94  } 95 }; 96 97 private static runnable receive = new runnable() { 98  @override 99  public void run() { 100     objectinputstream ois; 101      102     while (true) { 103        //system.out.println(list_sockets.size()); 104        (int = 0; < list_sockets.size(); i++) { 105             try { 106                 ois = new objectinputstream(list_sockets.get(i).getinputstream()); 107                 int receive_state = (integer) ois.readobject(); 108                  109                 ois = new objectinputstream(list_sockets.get(i).getinputstream()); 110                 datapackage dp = (datapackage) ois.readobject(); 111                  112                 list_data.set(i, dp); 113                  114                 if (receive_state == 1) { // client disconnected user 115                     disconnectclient(i); 116                     i--; 117                 } 118             } 119             catch (exception e) { // client disconnected (client didn't notify server disconnecting) 120                 disconnectclient(i); 121                 i--; 122                 e.printstacktrace(); 123                  124             } 125         } 126     } 127 } 128 }; 

and server disconnect method:

public static void disconnectclient(int index) {     try {         list_clients_model.removeelementat(index);         list_client_states.remove(index);         list_data.remove(index);         list_sockets.remove(index);     }     catch (exception e) {} } 

here exception getting when add breakpoint (it doesn't happen if don't add it):

java.net.socketexception: connection reset @ java.net.socketinputstream.read(unknown source) @ java.net.socketinputstream.read(unknown source) @ java.io.objectinputstream$peekinputstream.read(unknown source) @ java.io.objectinputstream$peekinputstream.readfully(unknown source) @ java.io.objectinputstream$blockdatainputstream.readshort(unknown source) @ java.io.objectinputstream.readstreamheader(unknown source) @ java.io.objectinputstream.<init>(unknown source) @ servermain$3.run(servermain.java:105) @ java.lang.thread.run(unknown source) 

so update, work if uncomment line 103. mentioned in comments i'm following tutorial on i'm following reference: http://www.youtube.com/watch?v=vff0jrcfch0

edit: thank azzurroverde, problem in fact lists wern't synchronized. changing lines 35-37 following fixed issue , works fine:

35 public static list<socket> list_sockets = collections.synchronizedlist(new arraylist<socket>()); 36 public static list<integer> list_client_states = collections.synchronizedlist(new arraylist<integer>()); 37 public static list<datapackage> list_data = collections.synchronizedlist(new arraylist<datapackage>()); 

let's summarize answer then, after long discussion :) problem arraylist not synchronized , different threads accessing @ same time leaving in unstable state

public static arraylist<socket> list_sockets = new arraylist<socket>(); 

the solution synchronize array list: http://docs.oracle.com/javase/7/docs/api/java/util/arraylist.html

list list = collections.synchronizedlist(new arraylist(...)); 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -