やあ、これは別の質問に関連している:How can I limit bandwidth in Java?
このソリューションは簡単で、私のために正常に動作します。
//サーバコード
Stream in;
long timestamp = System.currentTimeInMillis();
int counter = 0;
int INTERVAL = 1000; // one second
int LIMIT = 1000; // bytes per INTERVAL
...
/**
* Read one byte with rate limiting
*/
@Override
public int read() {
if (counter > LIMIT) {
long now = System.currentTimeInMillis();
if (timestamp + INTERVAL >= now) {
Thread.sleep(timestamp + INTERVAL - now);
}
timestamp = now;
counter = 0;
}
int res = in.read();
if (res >= 0) {
counter++;
}
return res;
}
//クライアントコード
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
クライアントコードのソース:https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html
私もそのスレッドを発見しました。限られたストリームとソケットを実装するのに役立ちました。しかし、今私はどのようにURLConnectionでこれらのソケットを使用するか知る必要があります。 – CitizenVayne
OK、このURLConnectionのクライアントを作成しますか? – jeorfevre
はい、私はクライアント側です。あなたが提供したコードを使用して、タイムフレームごとに限られたデータを持つURLConnectionを作成することができました。それでも基本的なソケットを何らかの形で直接アドレスできるなら、それは良いことです。私はソケット上で送受信するために割り当てられたバッファサイズを変更しようとしました。 – CitizenVayne