测量使用Java套接字传输文件时使用的带宽

时间:2011-12-09 03:43:18

标签: java file rmi transfer

有谁知道怎么做?使用java套接字

2 个答案:

答案 0 :(得分:1)

这可以通过装饰套接字的输入和输出流来实现。

所以看起来像这样:

class SocketWrapper extends Socket {
    private CountingInputStream input;

    @Override
    public InputStream getInputStream() throws IOException {
        if (input == null) {
            input = new CountingInputStream(super.getInputStream());
        }

        return input;
    }

    public int getInputCounter() {
        return input.getCounter();
    }

    // other stuff like getOutputStream
}

class CountingInputStream extends InputStream {
    private final InputStream inputStream;

    public CountingInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    private int counter = 0;

    public int getCounter() {
        return counter;
    }

    @Override
    public int read() throws IOException {
        counter++;
        return inputStream.read();
    }

    // other methods
}

同时查看herehere

最后,如果您只是想知道流量,可以使用嗅探器。

答案 1 :(得分:0)

请参阅this accepted answer,因为它提供了示例代码以及如何衡量使用的速度。

相关问题