2
有誰知道如何做到這一點?使用java插座用java socket轉換文件時使用的帶寬
有誰知道如何做到這一點?使用java插座用java socket轉換文件時使用的帶寬
這可以通過裝飾套接字的輸入和輸出流來實現。
因此,它可能是這樣的:
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
}
最後,如果您只想知道流量,可以使用嗅探器。
請參閱this accepted answer,因爲它提供了示例代碼以及如何測量所使用的速度。
你如何定義「帶寬浪費」? –
已使用*對不起我的不良 –
已更新標題... – akf