2011-03-26 53 views

回答

2

對於我來說,像這樣的伎倆。

0

我一直在尋找到類似的問題。

以下是我找到的一些相關資源。 How to test internet speed (JavaSE)?

此博客文章推薦使用InetAddress.getByName(host).isReachable(timeOut),然後測量響應時間。

http://tech.gaeatimes.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/

這可能不是最好的解決辦法,但它很容易。所以像這樣的東西。

String host = "172.16.0.2"; 
int timeOut = 3000; 
long[] time = new long[5]; 
Boolean reachable; 

for(int i=0; i<5; i++) 
{ 
long BeforeTime = System.currentTimeMillis(); 
reachable = InetAddress.getByName(host).isReachable(timeOut); 
long AfterTime = System.currentTimeMillis(); 
Long TimeDifference = AfterTime - BeforeTime; 
time[i] = TimeDifference; 
} 

現在你有5個值,大約表明過了多長時間,看機器是否平安到達的數組;否則爲假。我們知道如果它差異的時間是3秒,那麼它會超時,您還可以添加一組布爾值來顯示成功與失敗率。

這並不完美,但給出了給定時間的延遲的一個粗略的想法。

這在下面的鏈接發現等待時間的定義相匹配,但它是測量的發送和返回時間,而不是從發送方的時間到接收器: http://searchcio-midmarket.techtarget.com/definition/latency

定義:在一個網絡中,等待時間,一個延遲的同義詞表達了一個數據包從一個指定點到另一個指定點需要多少時間。

做更多的研究表明isReachable可能不太好。 Android Debugging InetAddress.isReachable

這可能會更好。

HttpGet request = new HttpGet(Url.toString()); 

HttpParams httpParameters = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); 
HttpClient httpClient = new DefaultHttpClient(httpParameters); 
for(int i=0; i<5; i++) 
{ 
long BeforeTime = System.currentTimeMillis(); 
HttpResponse response = httpClient.execute(request); 
long AfterTime = System.currentTimeMillis(); 
Long TimeDifference = AfterTime - BeforeTime; 
time[i] = TimeDifference; 
} 

注:請記住,這個就不說了等待時間,你正在下載的文件,但給你在某一特定時間內經歷了網絡延遲的想法。

如果有幫助,請接受這個答案。通過閱讀過程中所提供的InputStream

Process process = Runtime.getRuntime().exec("ping -c 1 google.com"); 
process.waitFor(); 

您可以通過答題的正則表達式: