Java networking | Streams are working only 1 time -


this question exact duplicate of:

im working on little chat program , have huge problem , can not solve it. dont know mistake be, me code right. really need help. have 2 threads in server, 1 thread accepting clients , other streams. , thread streams not working right. sends 1 time message client , multiple clients not working. , there strange problem. can send 1 message back, if put joptionpane-message called "sockets empty" in else statement, without doesnt work. here stream thread code:

private static runnable streamthread = new runnable() {     public void run()     {         while(true)         {             if(!(socketlist.isempty()))             {                 for(int = 0; < socketlist.size(); i++)                 {                                try                      {                         string key = socketlist.get(i);                          if(sockethashmap.containskey(key))                         {                             socket connection = sockethashmap.get(key);                              objectinputstream ois = new objectinputstream(connection.getinputstream());                              string response = (string) ois.readobject();                              objectoutputstream oos = new objectoutputstream(connection.getoutputstream());                              oos.writeobject(key + ": " + response);                             oos.flush();                         }                                                if(connection.isclosed())                         {                             joptionpane.showmessagedialog(null, "client closed connection", "info", joptionpane.information_message);                         }                     }                      catch (ioexception | classnotfoundexception e) {                         joptionpane.showmessagedialog(null, "error: " + e.getmessage(), "alarm", joptionpane.error_message);                         try {                             connection.close();                             connection.shutdowninput();                             connection.shutdownoutput();                              listclientsmodel.remove(i);                         } catch (ioexception e1) {                         }                      }                                    }             }             else             {                 joptionpane.showmessagedialog(null, "sockets leer", "info", joptionpane.information_message);             }         }      } }; 

and if want see complete servercode : http://pastebin.com/sxggrnjv

the documentation getoutputstream says:

closing returned outputstream will close associated socket.

and problem. create objectoutputstream, don't keep reference object around. object gets garbage collected, will call close() close socket's output stream.

same problem inputstream.

you could, example, create class contains socket , input , output stream , store in map instead of socket. way can re-use streams.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -