Я хочу сделать инструмент измерения скорости сети сервер-клиент.поэтому я создал серверную программу (откройте сокет и отправлю данные) и клиентскую программу (подключите сервер и получите данные).
Вкратце, клиент сохраняет данные, отправленные сервером для некоторыхраз (3 ~ 5сек), и вычислите полученные данные, деленные на время.
У меня был какой-то тест и между localhost, почти 1 ГБ / с, между другой машиной, почти 100 МБ / с, потому что мой маршрутизатор не поддерживаетСеть ГИГА.но когда я ограничил скорость сети до 0,1 Кбит / с, до 0,1 Кбит / с с помощью QOS (предоставление маршрутизатора) клиенту, скорость сети была почти 100 МБ / с.
// client
private static long cost = 0;
private static double total = 0;
private static long start = System.currentTimeMillis();
public static void main(String[] args) throws Exception {
Socket socket = new Socket("192.168.0.10", 50000);
// Socket socket = new Socket("localhost", 50000);
InputStream input = socket.getInputStream();
new Thread(() -> {
long interval = 3_000l;
try {
TimeUnit.MILLISECONDS.sleep(interval);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
while (true) {
cost = System.currentTimeMillis() - start;
String _total = String.format("%,.2f", total / 1000f / 1000f);
String _speed = String.format("%,.3f", total / cost / 1000f);
String _cost = String.format("%,.3f", cost / 1000f);
System.out.println(new Date() + " -- Read: " + _total + " Mbytes" +
", cost: " + _cost + " s" +
", speed: " + _speed + " MB/s");
total = 0;
start = System.currentTimeMillis();
try {
TimeUnit.MILLISECONDS.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
byte[] bytes = new byte[32 * 1024000]; // 32_000K
while (true) {
int read = input.read(bytes);
if (read < 0)
break;
total += read;
}
//server
private static ServerSocket server;
private static OutputStream output;
private static Socket socket;
public static void main(String[] args) throws InterruptedException, IOException {
while (true) {
try {
startServer();
} catch (IOException e) {
// TODO Auto-generated catch block
server.close();
output.close();
socket.close();
System.out.println("try to restart server after 2sec");
TimeUnit.MILLISECONDS.sleep(2000);
continue;
}
}
}
public static void startServer() throws IOException {
server = new ServerSocket(50000);
System.out.println("start start...");
socket = server.accept();
output = socket.getOutputStream();
byte[] bytes = new byte[32 * 1024000]; // 32000K
for (int i=0; i<bytes.length; i++) {
bytes[i] = 127;
}
while (true) {
output.write(bytes);
}
}
пожалуйста, скажите мне, что я пропустил.Спасибо.