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.
我们有一个客户端和一个服务器。我想测量它们之间网络的响应时间。当我向服务器发送请求时,它应立即响应我的请求,它应该像ping请求,以便在服务器上没有处理时间。
How can I do this in Java?
我怎么能用Java做到这一点?
4 个解决方案
#1
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.
我通过从客户端向服务器发送带有时间戳的数据包,然后让服务器返回相同的时间戳来完成此操作。当服务器收到时间戳时,它可以与当前时间戳进行比较以测量往返时间。
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 5及更高版本中生成ICMP ping。如果要测量网络速度,只需在之前和之后获取System.nanoTime()并进行比较。请注意,如果您的程序运行时没有足够的权限来执行ICMP ping,那么Java将使用TCP回送请求到端口7,这仍然适合您的需要。
#2
To ensure that the server response as fast as possible, you should either
为确保服务器尽快响应,您应该
- have a second connection to a different port - this is useful if you want to test the route to the server, i.e. the wires - or
- a common thread for all incoming packets, that will decide whether an incoming packet is a ping request and respond immediately - which is useful if you want to test the current connection (which takes other packets on that connection into account).
有另一个端口的第二个连接 - 如果你想测试到服务器的路由,即电线 - 或者,这是很有用的
所有传入数据包的公共线程,它将决定传入数据包是否为ping请求并立即响应 - 如果要测试当前连接(将该连接上的其他数据包考虑在内),这将非常有用。
Either way the listening part (server) should have its own Thread or the processing of the request could stale while other packets are being processed.
无论哪种方式,监听部分(服务器)都应该有自己的线程,或者在处理其他数据包时,请求的处理可能会失效。
Note that, whatever you do to measure the speed of the connection, you will not have a reliable value. You should have an acceptable value, though, if you take the average of several such pings.
请注意,无论您如何测量连接速度,都不会有可靠的值。但是,如果你取几个这样的ping的平均值,你应该有一个可接受的值。
#3
I think you need to distinguish between the time taken to the server (machine), and the time for your server-side process to handle this. For example, ping will simply measure the ICMP response time, which is a low-level network protocol return. If you take into account the TCP stack, and then the subsequent processing by the server, that time period will be greater.
我认为您需要区分服务器(机器)所花费的时间,以及服务器端进程处理此事件的时间。例如,ping将仅测量ICMP响应时间,这是一个低级别的网络协议返回。如果考虑TCP堆栈,然后由服务器进行后续处理,则该时间段将更长。
From your question, it sounds like ping (ICMP) is what you want. In JDK 5 and above, you can do:
从你的问题来看,听起来ping(ICMP)就是你想要的。在JDK 5及更高版本中,您可以:
String host = "192.168.0.1"
int timeOut = 5000;
boolean status = InetAddress.getByName(host).isReachable(timeOut)
and you would need to time that using System.nano()
or similar.
你需要使用System.nano()或类似的时间。
#4
using ping should be enough or wget that trace transfer rate. You can use justniffer for sniffing response times of "production" running servers.
使用ping应该足够或wget跟踪传输速率。您可以使用justniffer来嗅探“生产”运行服务器的响应时间。
#1
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.
我通过从客户端向服务器发送带有时间戳的数据包,然后让服务器返回相同的时间戳来完成此操作。当服务器收到时间戳时,它可以与当前时间戳进行比较以测量往返时间。
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 5及更高版本中生成ICMP ping。如果要测量网络速度,只需在之前和之后获取System.nanoTime()并进行比较。请注意,如果您的程序运行时没有足够的权限来执行ICMP ping,那么Java将使用TCP回送请求到端口7,这仍然适合您的需要。
#2
To ensure that the server response as fast as possible, you should either
为确保服务器尽快响应,您应该
- have a second connection to a different port - this is useful if you want to test the route to the server, i.e. the wires - or
- a common thread for all incoming packets, that will decide whether an incoming packet is a ping request and respond immediately - which is useful if you want to test the current connection (which takes other packets on that connection into account).
有另一个端口的第二个连接 - 如果你想测试到服务器的路由,即电线 - 或者,这是很有用的
所有传入数据包的公共线程,它将决定传入数据包是否为ping请求并立即响应 - 如果要测试当前连接(将该连接上的其他数据包考虑在内),这将非常有用。
Either way the listening part (server) should have its own Thread or the processing of the request could stale while other packets are being processed.
无论哪种方式,监听部分(服务器)都应该有自己的线程,或者在处理其他数据包时,请求的处理可能会失效。
Note that, whatever you do to measure the speed of the connection, you will not have a reliable value. You should have an acceptable value, though, if you take the average of several such pings.
请注意,无论您如何测量连接速度,都不会有可靠的值。但是,如果你取几个这样的ping的平均值,你应该有一个可接受的值。
#3
I think you need to distinguish between the time taken to the server (machine), and the time for your server-side process to handle this. For example, ping will simply measure the ICMP response time, which is a low-level network protocol return. If you take into account the TCP stack, and then the subsequent processing by the server, that time period will be greater.
我认为您需要区分服务器(机器)所花费的时间,以及服务器端进程处理此事件的时间。例如,ping将仅测量ICMP响应时间,这是一个低级别的网络协议返回。如果考虑TCP堆栈,然后由服务器进行后续处理,则该时间段将更长。
From your question, it sounds like ping (ICMP) is what you want. In JDK 5 and above, you can do:
从你的问题来看,听起来ping(ICMP)就是你想要的。在JDK 5及更高版本中,您可以:
String host = "192.168.0.1"
int timeOut = 5000;
boolean status = InetAddress.getByName(host).isReachable(timeOut)
and you would need to time that using System.nano()
or similar.
你需要使用System.nano()或类似的时间。
#4
using ping should be enough or wget that trace transfer rate. You can use justniffer for sniffing response times of "production" running servers.
使用ping应该足够或wget跟踪传输速率。您可以使用justniffer来嗅探“生产”运行服务器的响应时间。