使文件传输更高效Java [英] Making file transfer more efficient Java

查看:128
本文介绍了使文件传输更高效Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两台无线电脑连接到N无线路由器。这些PC中的每一台都以108-150Mbps的速度连接。

I have two wireless computers connected to an N wireless router. Each of these PCs are connected at between 108-150Mbps.

从理论上讲,我应该能够在绝对最佳条件下以13.5MB / s的速度传输到18.75MB / s。

Theoretically, I should be able to transfer at 13.5MB/s to 18.75MB/s, under the absolute best of conditions.

第一台计算机(正在发送)使用速度非常快的SSD,如果我没记错的话,大约100MB / s。 CPU使用率也低于20%。

The first computer (that is sending), uses a very fast SSD, which is around 100MB/s if I remember correctly. CPU usage also stays below 20%.

它在656367ms内发送了1960273535字节(1.8GB)。那是2.8MB / s(108兆比特中的22个)。
当我打开任务管理器时,我发现只有25-27%的网络连接被使用。

It sent 1960273535 bytes (1.8GB) in 656367ms. That's 2.8MB/s (22 out of 108 Megabits). When I open up task manager, I see that only 25-27% of the network connection is being used.

我正在寻找任何想法,建议或者可以使传输更快(通过网络)的改进。我在考虑从线程上的磁盘缓冲文件并从另一个线程发送缓冲的数据,但我不确定这是不是一个好主意。
这是SSCCE:

I am looking for any ideas, suggestions, or improvements that can make the transfer faster (over a network). I was thinking of buffering the file from the disk on a thread and sending the buffered data from another thread but I'm not sure if it's a good idea. Here is the SSCCE:

主持人:

import java.io.*;
import java.net.*;


public class Host {


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


        ServerSocket servsock = new ServerSocket(15064);
        Socket sock = servsock.accept();
            long time = System.currentTimeMillis();

        OutputStream out = sock.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream("C:\\complete.rar");

        byte [] buffer = new byte[64*1024]; 
        int bytesRead = 0;
        long totalSent = 0;

        while ( (bytesRead = fileInputStream.read(buffer)) != -1)
        {
            if (bytesRead > 0)
            {   
                out.write(buffer, 0, bytesRead);
                totalSent += bytesRead;
                System.out.println("sent " + totalSent);
            }   
        }

        sock.close();

        System.out.println("Sent " + totalSent + " bytes in "
                + (System.currentTimeMillis() - time) + "ms.");

    }
}

客户:

import java.io.*;
import java.net.*;

public class Client {

    public static void main(String[] args) throws Exception {
        Socket sock = new Socket("127.0.0.1", 15064);
        InputStream in = sock.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream("output.rar");

        byte [] buffer = new byte[64*1024]; 
        int bytesRead = 0;

        while ( (bytesRead = in.read(buffer)) != -1)
            fileOutputStream.write(buffer, 0, bytesRead);
        sock.close();
        fileOutputStream.close();
    }
}

编辑:我尝试映射网络驱动器并发送文件,而Windows更糟糕 - 2.35MB / s。根据这篇文章 http://tinyurl.com/634qaqg 映射网络驱动器比FTP快,我也没有时间继续玩游戏并设置FTP服务器。

I tried mapping a network drive and sending the file over that, and windows did even worse - 2.35MB/s. According to this article http://tinyurl.com/634qaqg mapping a network drive is faster than FTP, and I also don't have the time to stay playing around and setting up the FTP server.

编辑2:更改定时器后,结果是它通过WiFi以3MB / s的速度传输。我讨厌理论吞吐量。当我买东西时,我想知道它的真实表现。
事实证明,代码确实受到WiFi速度的限制。我仍然愿意接受建议。

After changing the timer, turns out it was transferring at 3MB/s over WiFi. I hate the "theoretical" throughput. When I buy something, I want to know it's REAL performance. It turns out the code is indeed limited by WiFi speeds. I am still open to suggestions though.

编辑3:在100Mbps LAN上运行程序后,它设法以11.8MB / s的速度传输文件。考虑到最大传输速率为12.5MB / s,这是非常好的。

Edit 3: After running the program on 100Mbps LAN, it managed to transfer the file at 11.8MB/s. That's pretty good considering that the maximum transfer rate is 12.5MB/s.

推荐答案

在2.8MB / s时,不大可能缓慢与你的代码有关。几乎可以肯定,由于无线网络无法达到理论吞吐量(可能是由于环境条件)。

At 2.8MB/s, it is unlikely that the slowness has anything to do with your code. It is almost certainly due to the wireless network not being able to achieve the theoretical throughput (possibly due to environmental conditions).

很容易测试是否是这种情况:只需在相同的两台计算机之间计算一个大的 ftp scp 文件传输,看看你看到的吞吐量是多少。

It's easy to test whether this is the case: simply time a large ftp or scp file transfer between the same two computers and see what kind of throughput you're seeing.

这篇关于使文件传输更高效Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆