multithreading - Coding a Terminal for ANdroid which communicates a Remote Server -


i'm reformulating question information, due on "on hold" state. i'll remove other question.

i trying build app console (like cmd), can send orders (commands) , receive response server.

so should see in screen order , server's response.

what should purpose?

i'm trying service. service can't work network.

asynctask finish when send first command. i'll have reconnect every time decide send order (unefficient?)

intentservice bounded activity's life. , finishes same way asynctask does.

how can thread where, first time, connects server. then, every time send thread message, contacts server , retrieves textedit info server?


edited [on hold] state. adding more info


here have:

  • an activity textview (used terminal prompt), edittext (allows user put command wants send) , button send it.
  • that activity bounded service way:

    serviceconnection mconnection = new serviceconnection() {      public void onservicedisconnected(componentname name) {         mbounded = false;         mserver = null;     }      public void onserviceconnected(componentname name, ibinder service) {         mbounded = true;         statusservice.localbinder mlocalbinder = (statusservice.localbinder)service;         mserver = mlocalbinder.getserverinstance();           /* once bounded, update data */         updatesensorsdata();       } };  /* ---------------- life cycle methods -------------------------*/  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_console);      intent mintent = new intent(this, statusservice.class);     bindservice(mintent, mconnection, bind_auto_create);      /* scrollable textview console prompt*/     textview tvconsole = (textview) findviewbyid(r.id.tvconsole);     tvconsole.setmovementmethod(new scrollingmovementmethod()); } 
  • the service should interact server , retrieve info textview, implemented:

    public string sendmessage(string message) {      /* here should connect server, dont know how */     string response;      connect(server, port);     sendreceivecmdcontrol(message);       return response; }  protected string sendreceivecmdcontrol(string msg) throws interruptedexception {     string res;       send(controlout, msg+"\n");     // wait response     try {         waitfor(controlin, msg);     } catch (eofexception e) {         log.d("error", "remote connection closed.");         return "error";      } catch (ioexception e) {         log.d("error", "io problem.");         return "error";     } // end try     // read rest     try {         res = controlin.readline();     } catch (ioexception e) {         log.d("error", "io problem.");         return "error";     } // end try       return(res); } // end sendreceivecmdcontrol() 

where controlout printwriter, controlin datainputstream, both initialized in method connect follows:

   /* connection i/o */    public static socket controlsocket = null;    public static printwriter controlout = null;    public static datainputstream controlin = null;    public static socket imagesocket = null;    public static printwriter imageout = null;    public static datainputstream imagein = null;     protected void connect(string server, int port, boolean controlenabled, boolean imageenabled) {          if (imageenabled) {             try {                 imagesocket = new socket(server, port);                 imageout = new printwriter(imagesocket.getoutputstream(), true);                 imagein = new datainputstream(new bufferedinputstream(imagesocket.getinputstream()));                 log.d("info", "connected image socket correctly");             } catch (unknownhostexception e) {                 disconnect();                 log.d("error", "exception: " + e.getmessage());                 return;             } catch (ioexception e) {                 disconnect();                 log.d("error", "exception: " + e.getmessage());                 return;             }          }          if (controlenabled) {             try {                 controlsocket = new socket(server, port+1);                 controlout = new printwriter(controlsocket.getoutputstream(), true);                 controlin = new datainputstream(new bufferedinputstream(controlsocket.getinputstream()));                 log.d("info", "connected control socket correctly");             } catch (unknownhostexception e) {                 disconnect();                 log.d("error", "exception: " + e.getmessage());                 return;             } catch (ioexception e) {                 disconnect();                 log.d("error", "exception: " + e.getmessage());                 return;             }           }      } 

so want is:

  1. connect server method connect 1st time run service.
  2. every time want send order remote server, should mserver.sendmessage(command); in activity.
  3. so service interacts remote server, , when gets response, service retrieves response activity ui update.

if there proper way activity-service-thread/whatever... let me know please help. , patience.


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 -