有人知道怎么做吗?使用 java 套接字
最佳答案
这可以通过装饰套接字的输入和输出流来实现。
所以它可能看起来像这样:
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
}
最后,如果你只是想知道流量,你可以使用嗅探器。
关于java - 测量使用 Java 套接字传输文件时使用的带宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8440787/