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
boundedservice
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 infotextview
, 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:
- connect server method
connect
1st time run service. - every time want send order remote server, should
mserver.sendmessage(command);
in activity. - so
service
interacts remoteserver
, , when gets response,service
retrieves responseactivity
ui update.
if there proper way activity-service-thread/whatever...
let me know please help. , patience.
Comments
Post a Comment