Android \ Java套接字太慢了?

时间:2016-02-27 07:57:20

标签: java android sockets

当我使用谷歌播放应用程序下载应用程序时,通常每秒约5MB。 但当我使用我的笔记本电脑作为发送100mb字节数组的服务器, 当我从我的智能手机下载(读取)这个阵列时,它大约是每秒2.5mb。

服务器代码,在我的计算机上运行。

class SpeedTestServer {

    public static void main(String[] args) throws IOException {

        int port = 1234;
        ServerSocket server = new ServerSocket(port);
        while(true) {
            Socket clientConn = server.accept();
            BufferedOutputStream out = new BufferedOutputStream(clientConn.getOutputStream());
            out.write(new byte[1024 * 1024 * 100]);
            out.flush();
            clientConn.close();
        }
    }
}

Cleint Code,在我的智能手机上运行。

public static void main(String[] args) throws IOException {

    int port = 1234;
    String host = "my computer internal ip";
    Socket client = new Socket(host, port);
    BufferedInputStream in = new BufferedInputStream(client.getInputStream());
    byte[] buffer = new byte[1024 * 1024 * 100];
    int sum = 0;

    Date start = new Date();
    System.out.println("Starting! " + start.toString());

    while((sum += in.read(buffer, sum, buffer.length - sum)) != -1) {
        if (sum == 1024 * 1024 * 100) {

            Date end = new Date();
            System.out.println("Finished! " + end.toString() + " " + sum);

            break;
        }
    }
}

0 个答案:

没有答案