mikko3024
mikko3024

Reputation: 11

Android: continuous socket write

I'm new to Java and Android programming but there's this project that required me to do so.

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.

public class NetworkTask extends AsyncTask<Void, byte[], Boolean> {
    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;
    }
}

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.

Is there a way to perform this continuously? or with minimal delay (around 0.5 seconds or less?)

Battery life is not an issue for me and I accept that a socket active continuously has its cons.

Thanks.

Upvotes: 1

Views: 1543

Answers (1)

fycth
fycth

Reputation: 3489

  1. you create multiple sockets in 'while' block
  2. on second link you create multiple output streams in 'while' block too

Try do it this way:

public class NetworkTask extends AsyncTask<Void, byte[], Boolean> {
    OutputStream dataOut; //Network Output Stream

    @Override
    protected void onPreExecute() {
        Log.i("AsyncTask", "onPreExecute");
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            gsocket = new Socket(roubotIP, roubotPort);
            dataOut = new DataOutputStream(gsocket.getOutputStream());
        } catch (UnknownHostException e) {
            Log.i("Socket: ","Unkown host");
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        while (sendData) { //While Boolean sendData is true
            try {
                byte[] data = EncodingUtils.getAsciiBytes(outData);
                Log.i("Data: ", outData);
                dataOut.write(data);
                dataOut.flush();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        try {
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}

Upvotes: 3

Related Questions