0
我已经在我的电脑上建立了一个服务器在Java中,客户端是我的Android手机。Android的TCP套接字连接慢
我试图模拟我的应用程序中的鼠标行为,但问题是即使在第一分钟一切运行顺利,最后在客户端和服务器之间有一个巨大的延迟。
Server代码
try {
System.out.println(InetAddress.getLocalHost());
serverSocket = new ServerSocket(4444); // Server socket
serverSocket.setReceiveBufferSize(10000);
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
try {
clientSocket = serverSocket.accept(); // accept the client connection
System.out.println("connection initiated");
DataInputStream din = new DataInputStream(clientSocket.getInputStream());
DataOutputStream dout= new DataOutputStream(clientSocket.getOutputStream());
/* do my things here */
din.close();
dout.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading "+ex);
}
客户端代码
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
//Inet4Address.getLocalHost().toString(); //InetAddress.getLocalHost().getHostAddress().toString();
System.out.println(dstAddress);
socket = new Socket(dstAddress, dstPort);
socket.setSendBufferSize(10000);
DataInputStream din = new DataInputStream(socket.getInputStream());
dout = new DataOutputStream(socket.getOutputStream());
dout.writeUTF(kappa);
response = "sent to server";
String msg = din.readUTF();
response += msg;
din.close();
dout.close();
socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = e.toString();
}
return null;
}
(卡帕是一个全球性的字符串,我每一次修改。)
可能是什么情况?
'/ *在这里做我的事* /'。至少展示你如何阅读和写作。 – greenapps
为什么要为每个字符串创建一个新的套接字/客户端?为什么不保持连接打开? – greenapps
'(kappa是我每次修改的全局字符串)'。你每次发送多少个字符?多久? – greenapps