Home > Blockchain >  HTTP requests received in wrong time sequence
HTTP requests received in wrong time sequence

Time:02-28

I am creating an Android app that sends http requests contains IMU data every 20ms using Handler and Runnable.

public void onClickLogData(View view){
        Log.d(TAG,"onClickLogData");
        final OkHttpClient client = new OkHttpClient();

        Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (Running) {
                    handler.postDelayed(this, 20);
                    String url = "http://192.168.86.43:5000/server";
                    Log.d(TAG, String.valueOf(time));
                    RequestBody body = new FormBody.Builder()
                            .add("Timestamp", String.valueOf(time))
                            .add("accx", String.valueOf(accx))
                            .add("accy", String.valueOf(accy))
                            .add("accz", String.valueOf(accz))
                            .add("gyrox", String.valueOf(gyrox))
                            .add("gyroy", String.valueOf(gyroy))
                            .add("gyroz", String.valueOf(gyroz))
                            .add("magx", String.valueOf(magx))
                            .add("magy", String.valueOf(magy))
                            .add("magz", String.valueOf(magz))
                            .build();

                    Request request = new Request.Builder()
                            .url(url)
                            .post(body)
                            .build();

                    final Call call = client.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            Log.i("onFailure", e.getMessage());
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response)
                                throws IOException {
                            assert response.body() != null;
                            String result = response.body().string();
                            Log.i("result", result);
                        }
                    });
                } else {
                    handler.removeCallbacks(this);
                }
                }
        };
        handler.postDelayed(runnable, 1000);
    }

And the data are received and stored on my laptop.

with open('imu.csv','w') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(['Timestamp','accx','accy','accz','gyrox','gyroy','gyroz','magx','magy','magz'])
        
app = Flask(__name__)
 
@app.route('/server', methods=['GET','POST'])
def server():
    r = request.form
    data = r.to_dict(flat=False)

    t = int(str(data['Timestamp'])[2:-2])
    print(t)
    accx = float(str(data['accx'])[2:-2])
    accy = float(str(data['accy'])[2:-2])
    accz = float(str(data['accz'])[2:-2])
    gyrox = float(str(data['gyrox'])[2:-2])
    gyroy = float(str(data['gyroy'])[2:-2])
    gyroz = float(str(data['gyroz'])[2:-2])
    magx = float(str(data['magx'])[2:-2])
    magy = float(str(data['magy'])[2:-2])
    magz = float(str(data['magz'])[2:-2])
    imu_data = [t,accx,accy,accz,gyrox,gyroy,gyroz,magx,magy,magz]

    with open('imu.csv','a ') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(imu_data)
    return("ok")
 
if __name__ == '__main__':
    app.run(host='0.0.0.0')

The requests are sent in chronological order on Android side as Log indicates, however on the receiving side many of the requests are received in wrong time sequence. enter image description here

It seems that this happens more frequently as time goes. What possibly could be the cause of this and where should I be looking at?

CodePudding user response:

All sorts of things. Requests are sent over a network. They can take different paths to get there each time. Requests can even get lost. Using TCP you'd automatically resend a lost request, but then it would be even more out of order. They can be delayed in the network in different bridges and routers. There is no promise over the internet that different requests will be received in order. That's only a promise over a single socket using TCP- and that is only possible with a lot of work (basically keeping track of every packet sent and received and waiting until you have them in order to send it to the app). If your architecture requires you to receive them in order, your architecture cannot possibly work over the internet.

If you do need an ordering on the server, either embed a request number that's monotonically increasing, or embed a timestamp in the request.

  • Related