一周前我开发了远程鼠标。我测试了很多,但我无法解决与操作系统相关的神秘问题。(远程应用,Win7和Win8的速度差异)
因此,我正在使用我编写的简单代码测试 PC 和 Android 之间的远程连接。(请看下面的代码和结果。)它们之间有一点延迟时间(大约200ms)。我想这就是为什么我觉得我的鼠标移动很慢。所以我想缩小差距。请给我任何办法。我想知道这是否只发生在win 7上。当我尝试使用win 8时,差距很小。
我将不胜感激任何帮助。谢谢阅读 :)
客户端 :
@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();
}
});
}
和
服务器端 :
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();
}
和
结果 :
(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 接收时间 - Android 发送时间