android - OutputStream not sending until socket is closed -


hi i'm developing android app controlling computer using sockets, problem don't data server until close outputstream closing socket , won't able send or receive data anymore. question how can send data server client without closing outputstream ?

this code working closing outputstream (ps1.close()) : socket closed

private class receivecmd extends thread{         scanner sc = null;         @override         public void run() {             runtime r;             try {                 sc = new scanner(socket.getinputstream());                 while(true)                 {                     if(sc.hasnext())                     {                         system.out.println("got some");                         string sr = sc.nextline();                         if(sr.contains("shutdown "))                         {                             if(sr.contains("/h"))                             {                                 sr = sr.replace("/t", "");                             }                             r = runtime.getruntime();                             r.exec(sr+"/f");                         }                         else if (sr.contains("taskman"))                         {                             system.out.println("task");                             r = runtime.getruntime();                             process p = r.exec("tasklist.exe");                             scanner sc1 = new scanner(p.getinputstream());                             printstream ps1 = new printstream(socket.getoutputstream());                             while(sc1.hasnext())                             {                                 string s = sc1.nextline();                                 ps1.println(s);                                 system.out.println(s);                              }                             ps1.close();                          }                         else if(sr.contains(".exe"))                         {                             r = runtime.getruntime();                             r.exec(sr);                             system.out.println(sr);                         }                       }                 }             } catch (ioexception ex) {                 logger.getlogger(androidserver.class.getname()).log(level.severe, null, ex);             }             super.run(); //to change body of generated methods, choose tools | templates.         } 

this code not working when delete ps1.close() : can still receive data

private class receivecmd extends thread{         scanner sc = null;         @override         public void run() {             runtime r;             try {                 sc = new scanner(socket.getinputstream());                 while(true)                 {                     if(sc.hasnext())                     {                         system.out.println("got some");                         string sr = sc.nextline();                         if(sr.contains("shutdown "))                         {                             if(sr.contains("/h"))                             {                                 sr = sr.replace("/t", "");                             }                             r = runtime.getruntime();                             r.exec(sr+"/f");                         }                         else if (sr.contains("taskman"))                         {                             system.out.println("task");                             r = runtime.getruntime();                             process p = r.exec("tasklist.exe");                             scanner sc1 = new scanner(p.getinputstream());                             printstream ps1 = new printstream(socket.getoutputstream());                             while(sc1.hasnext())                             {                                 string s = sc1.nextline();                                 ps1.println(s);                                 system.out.println(s);                              }                           }                         else if(sr.contains(".exe"))                         {                             r = runtime.getruntime();                             r.exec(sr);                             system.out.println(sr);                         }                       }                 }             } catch (ioexception ex) {                 logger.getlogger(androidserver.class.getname()).log(level.severe, null, ex);             }             super.run(); //to change body of generated methods, choose tools | templates.         } 

use flush()

flushes object writing out buffered data underlying output.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -