Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: continuous socket write
    text
    copied!<p>I'm new to Java and Android programming but there's this project that required me to do so.</p> <p>The app sends bytes to a server that receives all information send to it and performs the equivalent commands. The client and server is in an exclusive link so I would not worry about security issues.</p> <pre><code>public class NetworkTask extends AsyncTask&lt;Void, byte[], Boolean&gt; { OutputStream dataOut; //Network Output Stream @Override protected void onPreExecute() { Log.i("AsyncTask", "onPreExecute"); } @Override protected Boolean doInBackground(Void... params) { boolean result = false; while (sendData) { //While Boolean sendData is true try { gsocket = new Socket(roubotIP, roubotPort); byte[] data = EncodingUtils.getAsciiBytes(outData); Log.i("Data: ", outData); dataOut = new DataOutputStream(gsocket.getOutputStream()); dataOut.write(data); } catch (UnknownHostException e) { Log.i("Socket: ","Unkown host"); e.printStackTrace(); result = true; } catch (IOException e) { e.printStackTrace(); result = true; } catch (Exception e) { e.printStackTrace(); result = true; } } try { dataOut.close(); } catch (Exception e) { e.printStackTrace(); } return result; } } </code></pre> <p>With the code above I was able to establish connection to the server but the data is only sent/written to the socket every 1-2 seconds.</p> <p>Is there a way to perform this continuously? or with minimal delay (around 0.5 seconds or less?)</p> <p>Battery life is not an issue for me and I accept that a socket active continuously has its cons.</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload