【发布时间】:2011-12-09 03:43:18
【问题描述】:
有人知道怎么做吗?使用 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
}
最后,如果您只想知道流量,您可以使用嗅探器。
【讨论】: