简体   繁体   中英

Server(Java)&Client(Android) remote connection delay

I developed remote mouse a week ago. I tested a lot but I can't fix a mysterious problem related with OS. ( Remote app, Difference of speed between Win7 and Win8 )

So I'm testing the remote connection between PC and Android with simple code I made. ( Please see the codes and result below. ) There is a little bit delay time ( about 200ms ) between them. I think this is why I felt my mouse's moving is slow. So I want to reduce the gap. Please give me any way to go. And I wonder is this happens only win 7. When I tried with win 8, there is little gap.

I would appreciate any help. Thanks for reading :)

Client Side :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Touch = (TextView) findViewById(R.id.Touch);
    BtnCnt = (Button) findViewById(R.id.BtnConnect);

    Touch.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me) {
            try {
                c = Calendar.getInstance();
                hour = c.get(Calendar.HOUR);
                minute = c.get(Calendar.MINUTE);
                second = c.get(Calendar.SECOND);
                msecond = c.get(Calendar.MILLISECOND);

                String CurTime = "(Android)" + hour + ":" + minute + ":"
                        + second + ":" + msecond;

                Touch.setText(CurTime);
                dos.writeUTF(CurTime);
                dos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        }
    });

    BtnCnt.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            Touch.setText("Connecting...");

            Thread T1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        socket = new Socket(IP, PORT);
                        dos = new DataOutputStrea(socket.getOutputStream());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            T1.start();
        }
    });
}

and

Server Side :

try {
    ss = new ServerSocket(PORT);
    System.out.println("Server starts...");
    s = ss.accept();

    if (s.isConnected()) {
        System.out.println("Client is connected.");
        dis = new DataInputStream(s.getInputStream());
        while (true) {
            c = Calendar.getInstance();
            c.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS");
            data = dis.readUTF();
            System.out.println("(Java)" + sdf.format(c.getTime())
                    + " - " + data);
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

and

Result :

(Java)12:24:04:693 - (Android)0:24:4:319
(Java)12:24:04:693 - (Android)0:24:4:342
(Java)12:24:04:693 - (Android)0:24:4:351
(Java)12:24:04:693 - (Android)0:24:4:369
(Java)12:24:04:694 - (Android)0:24:4:386
(Java)12:24:04:694 - (Android)0:24:4:403
(Java)12:24:04:694 - (Android)0:24:4:421

= Java received time - Android sended time

I don't understand why you are create a socket over each iteration of thread runs in this scope u are creating a socket per each runs call in thread that is numerable. i think after some time that thread works delay time increase.

 Thread T1 = new Thread(new Runnable() {
            public void run() {
                try {
                    socket = new Socket(IP, PORT);
                    dos = new DataOutputStrea(socket.getOutputStream());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

instead of this things i think you should have a single socket and send stream on this socket to server .

Update:

another approach is using REST to do That , i am using Volley its a great,sweet and easy REST framework .it creates by google

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM