java - DataInputStream.available returns 0 when there should be data available -
i busy making inhouse navigation application google glass. using datainputstream / dataoutputstream sending orientation info glass android phone.
client/
protected void onhandleintent(intent intent) { seek { socket mclientsocket = new socket(serverip, port); log.d("odsa","client connected"); inputstream inputstream = mclientsocket.getinputstream(); datainputstream datainputstream = new datainputstream(inputstream); isservicerunning = true; long lasttime = system.currenttimemillis(); while (isservicerunning) { int bytesavailabe = datainputstream.available(); log.d("odsa","waiting bytesavailable: real bytes available: " + bytesavailabe); if(bytesavailabe > 4) { if (bytesavailabe > 8) { long bytestoskip = bytesavailabe - 8; log.d("odsa","bytesskipped: " + bytestoskip); inputstream.skip(bytestoskip); } log.d("odsa","trying read azimuth"); float azimuth = datainputstream.readfloat(); log.d("odsa","trying read pitch"); float pitch = datainputstream.readfloat(); string resulttxt = "received server - azimuth: " + azimuth + " | pitch: " + pitch; log.d("odsa", resulttxt); log.d("odsa","sende broadcast intent"); intent broadcastintent = new intent(); //deze wil ik eigenlijk niet hier maar krijg de reference niet te pakken in orientation service doe ik iets fout ? broadcastintent.setaction("com.odca.intent.action.message_processed"); broadcastintent.addcategory(intent.category_default); broadcastintent.putextra(param_out_msg, resulttxt); sendbroadcast(broadcastintent); log.d("odsa","done sending broadcast intent"); } else{ thread.sleep(1000); } } } grab (ioexception e) { e.printstacktrace(); //todo send error message activity } grab (interruptedexception e) { e.printstacktrace(); //todo send error message activity } }
server/
protected void onhandleintent(intent intent) { seek { //create connection orientation sensor context startactivitycontext = getbasecontext(); this.orientationsensormanager = new orientationsensormanager(startactivitycontext); //start orientation server serversocket mserversocket = new serversocket(); mserversocket.setreuseaddress(true); mserversocket.bind(new inetsocketaddress(port)); log.d("odsa","waiting connection on server"); socket mclientsocket = mserversocket.accept(); //todo send message activity (glass connected) log.d("odsa","server connected"); outputstream ostream = mclientsocket.getoutputstream(); dataoutputstream dataoutputstream = new dataoutputstream(ostream); isservicerunning = true; long lasttime = system.currenttimemillis(); while (isservicerunning) { long endtime = system.currenttimemillis(); long difference = endtime - lasttime; if(difference > 100) { float[] results = orientationsensormanager.getorientationdata(); dataoutputstream.writefloat(results[0]); dataoutputstream.writefloat(results[1]); lasttime = system.currenttimemillis(); //todo send orientation info activity log.d("odsa","data send client - azimuth: " + string.valueof(results[0] + " | pitch: " + results[1])); } } if (mclientsocket != null) mclientsocket.close(); if (mserversocket != null) mserversocket.close(); } grab (ioexception e) { e.printstacktrace(); }
the server sending info fine. , runs when testing (posting info @ 100 milliseconds).
the problem client code. reason datainputstream.available returns 0 when there should info available on stream. why happening?
i include logcat output improve understanding of problem. problem easy see in logcat (this running @ 1000 miliseconds interval). advice welcome.
10-23 13:48:48.452 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 8 10-23 13:48:48.452 2897-3214/com.odca d/odsa﹕ trying read azimuth 10-23 13:48:48.452 2897-3214/com.odca d/odsa﹕ trying read pitch 10-23 13:48:48.452 2897-3214/com.odca d/odsa﹕ received server - azimuth: 33.915466 | pitch: 82.41343 10-23 13:48:48.452 2897-3214/com.odca d/odsa﹕ sende broadcast intent 10-23 13:48:48.462 2897-3214/com.odca d/odsa﹕ done sending broadcast intent 10-23 13:48:48.462 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:49.472 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 24 10-23 13:48:49.472 2897-3214/com.odca d/odsa﹕ bytesskipped: 16 10-23 13:48:49.472 2897-3214/com.odca d/odsa﹕ trying read azimuth 10-23 13:48:49.472 2897-3214/com.odca d/odsa﹕ trying read pitch 10-23 13:48:49.482 2897-3214/com.odca d/odsa﹕ received server - azimuth: 34.72676 | pitch: 82.230484 10-23 13:48:49.482 2897-3214/com.odca d/odsa﹕ sende broadcast intent 10-23 13:48:49.502 2897-3214/com.odca d/odsa﹕ done sending broadcast intent 10-23 13:48:49.502 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:50.502 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:51.512 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:52.502 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:53.502 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:54.512 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:55.512 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:56.512 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:57.512 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:58.512 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 776 10-23 13:48:58.512 2897-3214/com.odca d/odsa﹕ bytesskipped: 768 10-23 13:48:58.512 2897-3214/com.odca d/odsa﹕ trying read azimuth 10-23 13:48:58.512 2897-3214/com.odca d/odsa﹕ trying read pitch 10-23 13:48:58.512 2897-3214/com.odca d/odsa﹕ received server - azimuth: 33.074875 | pitch: 82.22403 10-23 13:48:58.522 2897-3214/com.odca d/odsa﹕ sende broadcast intent 10-23 13:48:58.532 2897-3214/com.odca d/odsa﹕ done sending broadcast intent 10-23 13:48:58.532 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:48:59.542 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 64 10-23 13:48:59.542 2897-3214/com.odca d/odsa﹕ bytesskipped: 56 10-23 13:48:59.542 2897-3214/com.odca d/odsa﹕ trying read azimuth 10-23 13:48:59.542 2897-3214/com.odca d/odsa﹕ trying read pitch 10-23 13:48:59.542 2897-3214/com.odca d/odsa﹕ received server - azimuth: 32.31331 | pitch: 81.64859 10-23 13:48:59.552 2897-3214/com.odca d/odsa﹕ sende broadcast intent 10-23 13:48:59.572 2897-3214/com.odca d/odsa﹕ done sending broadcast intent 10-23 13:48:59.572 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 16 10-23 13:48:59.572 2897-3214/com.odca d/odsa﹕ bytesskipped: 8 10-23 13:48:59.572 2897-3214/com.odca d/odsa﹕ trying read azimuth 10-23 13:48:59.572 2897-3214/com.odca d/odsa﹕ trying read pitch 10-23 13:48:59.572 2897-3214/com.odca d/odsa﹕ received server - azimuth: 32.404648 | pitch: 82.11532 10-23 13:48:59.572 2897-3214/com.odca d/odsa﹕ sende broadcast intent 10-23 13:48:59.582 2897-3214/com.odca d/odsa﹕ done sending broadcast intent 10-23 13:48:59.582 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 0 10-23 13:49:00.592 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 72 10-23 13:49:00.592 2897-3214/com.odca d/odsa﹕ bytesskipped: 64 10-23 13:49:00.592 2897-3214/com.odca d/odsa﹕ trying read azimuth 10-23 13:49:00.592 2897-3214/com.odca d/odsa﹕ trying read pitch 10-23 13:49:00.592 2897-3214/com.odca d/odsa﹕ received server - azimuth: 32.3553 | pitch: 82.41276 10-23 13:49:00.592 2897-3214/com.odca d/odsa﹕ sende broadcast intent 10-23 13:49:00.612 2897-3214/com.odca d/odsa﹕ done sending broadcast intent 10-23 13:49:00.612 2897-3214/com.odca d/odsa﹕ waiting bytesavailable: real bytes available: 16
it isn't obliged homecoming non-zero ever, , many implementations don't. example, sslsocket has received part of ssl record may have no thought how long info going be, , years returned zero.
just rid of available()
phone call , skips, read everything, , block in read()
method, or in case readfloat()
method when there isn't input.
java android sockets
No comments:
Post a Comment