android - How to accept multiple socket connection at Java server -


i'm trying implement client-server pair in android using standard java sockets. far, i've implemented 1 one client-server connection. now, i'm modifying server side code accept multiple client connection. i've taken here. i'm creating serversocket , wait client connection in infinite while loop. once client side socked accepted, run new thread handle client , again wait new connection. unfortunately, program keeps crashing unknown reason! logcat says- "error opening trace file: no such file or directory". file path correct (it working fine in older implementation). can suggest doing wrong? related missing manifest permission? here i've done far:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      intent launchfilemanager = new intent(getbasecontext(),             filechooseractivity.class);     startactivityforresult(launchfilemanager, request_code); //receives files     //filearray has been populated here      button btn=(button)findviewbyid(r.id.dispfilesid);     btn.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v) {             initializeserver();         }     }); }  private void initializeserver() {     boolean listening = true;     try {         serversocket = new serversocket(4444);     } catch (ioexception e) {         e.printstacktrace();         log.d("listen failed", "listening port 4444 failed");     }     while (listening) {         try {             socket = serversocket.accept();             thread clienttrd = new thread(new runnable() {                 @override                 public void run() {                      try {                         outputstream myos=socket.getoutputstream();                         myos.write(filepathnamearray.size()); //send file count                     } catch (ioexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                      if (!filearray.isempty()) {                         (int = 0; < filepathnamearray.size(); i++){                             copyfile(filearray.get(i), filearray.get(i).getname().tostring());                             //mtv.settext(filearray.get(i).getname().tostring());                         }                     }                 }             });             clienttrd.start();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     } } 

the intent filechooseractivity returns arraylist containing list of file uris. these uris wrapped around files , written on dataoutputstream of socket object. please help. insight appreciated! in advance!

finally solved problem. might others land on page in future. problem: using same thread accepting , handling client connection. so, server not become free listening other incoming connections. wrote separate connectionhandler class handling client connection:

btn.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v) {              thread trd = new thread(new runnable() {                 @override                 public void run() {                     try {                         serversocket = new serversocket(4444);                     } catch (ioexception e) {                         e.printstacktrace();                         log.d("listen failed",                                 "listening port 4444 failed");                     }                      while (listening) {                         try {                             socket = serversocket.accept();                             runnable connectionhandler = new connectionhandler(                                     socket, filearray, filepathnamearray);                             new thread(connectionhandler).start();                         } catch (ioexception e) {                             // todo auto-generated catch block                             e.printstacktrace();                         }                     }                 }             });             trd.start();         }     }); 

connectionhandler (this solved issue):

    public class connectionhandler implements runnable {      private socket socket=null;     private arraylist<file> filearray=null;     arraylist<string> filepathnamearray=null;      public connectionhandler(socket socket, arraylist<file> filearray, arraylist<string> filepathnamearray) {         super();         this.socket = socket;         this.filearray=filearray;         this.filepathnamearray=filepathnamearray;     }     @override     public void run() {         try {             outputstream myos=socket.getoutputstream();             myos.write(filepathnamearray.size()); //send file count         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          if (!filearray.isempty()) {             (int = 0; < filepathnamearray.size(); i++){                 copyfile(filearray.get(i), filearray.get(i).getname().tostring());                 //mtv.settext(filearray.get(i).getname().tostring());             }         }      }     private void copyfile(file file, string name) {         fileinputstream fis;         long filesize = file.length();         try {             fis = new fileinputstream(file);             bufferedinputstream bis = new bufferedinputstream(fis);             @suppresswarnings("resource")             datainputstream dis = new datainputstream(bis);             byte[] mybytearray = new byte[16384];             outputstream os;             if (socket != null) {                 os = socket.getoutputstream();                 dataoutputstream dos = new dataoutputstream(os);                 dos.writeutf(name); // filename sent client                 dos.writelong(filesize); // file size sent client                 long z = filesize;                 int n = 0;                 while ((z > 0)                         && (n = dis.read(mybytearray, 0,                                 (int) math.min(mybytearray.length, z))) != -1) {                     dos.write(mybytearray, 0, n);                     dos.flush();                     z -= n;                 }             }         } catch (filenotfoundexception e1) {             e1.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }     }  } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -