简体   繁体   English

Android TCP套接字连接速度慢

[英]Android tcp socket connection slow

I have set up a server on my computer in java, and the client is my android phone. 我已经用Java在计算机上设置了服务器,客户端是我的android手机。

I am trying to simulate the mouse behavior in my app but the problem is even though at the first minutes everything runs smoothly, in the end there is a huge delay between client and server. 我正在尝试模拟我的应用程序中的鼠标行为,但是问题是,即使在开始的几分钟内一切都运行顺利,最后,客户端和服务器之间存在巨大的延迟。

Server code 服务器代码

    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);
            }

Client code 客户代码

    @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;
    }

(kappa is a global string that i modify each time.) (kappa是我每次都会修改的全局字符串。)

What might be the case? 可能是什么情况?

问题解决了,我改成了udp连接,现在没有延迟了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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