测量UDP速度

时间:2018-04-16 08:04:59

标签: android udp datagram

如何使用UDP准确测量路由器的数据速率?

我的参考是Wi-Fi SweetSpots它测量本地路由器的数据传输速率(不是网速)

现在这是我的代码,我大约700mbs到800mbs(这是不正确的)

public class RouterTransferRateService extends DaggerService {
    public static final String ACTION_NAME = "rounter.transter.data.service";

    @Inject
    WifiManager wifiManager;

    @Inject
    WirelessInfoProvider wirelessInfoProvider;

    private final IBinder binder = new RouterTransferRateDataBinder();

    private TimerTask taskReceive;
    private Timer timerReceive;
    private boolean isSending = false;
    private boolean isReceiving = false;

    private final int BUF_SIZE = 65507;
    private final int serverPort = 50001;
    private final long MB = 1024L * 1024L;
    private long oldBytes = 0;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    private void initializeTimerTask() {

        taskReceive = new TimerTask() {
            @Override
            public void run() {
                long currentBytes = TrafficStats.getUidRxBytes(myUid());
                long bytesPerSec = currentBytes - oldBytes;
                oldBytes = currentBytes;


                Log.e("Data Rate?", (bytesPerSec / MB) + "Mbps");
            }
        };

        new Thread(() -> {
            try {
                DatagramSocket senderSocket = new DatagramSocket();
                senderSocket.setSendBufferSize(BUF_SIZE);
                byte[] buf = new byte[BUF_SIZE];

                InetAddress address = wirelessInfoProvider.getInetAddress();
                new Random().nextBytes(buf);

                while (isSending) {
                    try {
                        DatagramPacket packet = new DatagramPacket(buf, buf.length, address, serverPort);
                        senderSocket.send(packet);
                    } catch (Exception e) {

                    }
                }
                senderSocket.disconnect();
                senderSocket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                DatagramSocket receiverSocket = new DatagramSocket(serverPort);
                receiverSocket.setReceiveBufferSize(BUF_SIZE);
                byte[] buffer = new byte[BUF_SIZE];
                DatagramPacket packet = new DatagramPacket(buffer, BUF_SIZE);

                while (isReceiving) {
                    receiverSocket.receive(packet);
                }
                receiverSocket.disconnect();
                receiverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    private void startTimer() {
        isSending = true;
        isReceiving = true;

        initializeTimerTask();
        timerReceive = new Timer();
        timerReceive.schedule(taskReceive, 0, 1000);
    }

    private void stopTimer() {
        isSending = false;
        isReceiving = false;

        if (timerReceive != null) {
            timerReceive.cancel();
            timerReceive = null;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(getClass().getName(), "onCreate");
        startTimer();
    }

    @Override
    public void onDestroy() {
        Log.e(getClass().getName(), "onDestroy");
        stopTimer();
        super.onDestroy();
    }

    @Override
    public ComponentName startService(Intent service) {
        return super.startService(service);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public class RouterTransferRateDataBinder extends Binder {
        public RouterTransferRateService getService() {
            return RouterTransferRateService.this;
        }
    }
}

0 个答案:

没有答案