如何使用Java测量网络的响应时间? [英] How to measure response-time of network using Java?

查看:447
本文介绍了如何使用Java测量网络的响应时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个客户端和一个服务器。我想测量它们之间网络的响应时间。当我向服务器发送请求时,它应立即响应我的请求,它应该像ping请求,这样服务器上就没有处理时间。

We have a client and a server. I want to measure the response-time of the network between them. When I send a request to server it should immediate respond to my request, it should be like a ping request so that there will be no processing time at the server.

如何我可以用Java做这个吗?

How can I do this in Java?

推荐答案

我是通过从客户端发送带有时间戳的数据包来完成此操作的,然后是让服务器返回相同的时间戳。当服务器收到时间戳时,它可以与当前时间戳进行比较以测量往返时间。

I have done this by sending a packet with a timestamp from client to server, and then having the server return the same timestamp back. When the server receives the timestamp, it can compare against the current timestamp to measure round trip time.

显然有一种方法可以生成 ICMP ping 在Java 5及更高版本中。如果您想测量网络速度,只需在之前和之后获取 System.nanoTime()并进行比较。请注意,如果您的程序运行时没有足够的权限来执行ICMP ping,那么Java将使用TCP回送请求到端口7,这仍然适合您的需求。

Apparently there is a way to generate an ICMP ping in Java 5 and later. If you want to measure network speeds, just grab System.nanoTime() before and after and compare. Note that if your program is running with insufficient privs to do ICMP ping, then Java will resort to using a TCP echo request to port 7, which will still suit your needs.

这篇关于如何使用Java测量网络的响应时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆