{"id":73,"date":"2018-03-01T15:29:26","date_gmt":"2018-03-01T06:29:26","guid":{"rendered":"http:\/\/mell0w-5phere.net\/jaded5phere\/2018\/03\/01\/2018-03-01-152926\/"},"modified":"2018-12-21T17:50:07","modified_gmt":"2018-12-21T08:50:07","slug":"java-ntp","status":"publish","type":"post","link":"https:\/\/mell0w-5phere.net\/jaded5phere\/2018\/03\/01\/java-ntp\/","title":{"rendered":"\u3010Java\u3011NTP\u30b5\u30fc\u30d0\u30fc\u304b\u3089\u6642\u523b\u3092\u53d6\u5f97\u3059\u308b"},"content":{"rendered":"\n<p>\u3000\u7aef\u672b\u6642\u523b\u306e\u30ba\u30ec\u304c\u9177\u304b\u3063\u305f\u306e\u3067NTP\u30b5\u30fc\u30d0\u30fc\u304b\u3089\u6642\u523b\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u3057\u305f\u3002<\/p>\n<hr>\n<h3>\u30fb\u3068\u308a\u3042\u3048\u305a\u52d5\u3051\u3070\u3044\u3044\u4eba\u7528<\/h3>\n<p>\u3000\u5148\u306b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u7f6e\u3044\u3066\u304a\u304f\u3002<a href=\"https:\/\/commons.apache.org\/proper\/commons-net\/download_net.cgi\">\u5916\u90e8\u30e9\u30a4\u30d6\u30e9\u30ea(org.apache.commons.net.ntp)<\/a>\u3092\u4f7f\u3046\u3002<br \/>\n\u3000\u6700\u521d\u306f\u305d\u306e\u3078\u3093\u3067\u62fe\u3063\u305f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30da\u3059\u308c\u3070\u52d5\u304f\u3060\u308d\u3046\u3068\u601d\u3063\u3066\u3044\u305f\u304c\u3001\u3069\u3046\u3082\u3046\u307e\u304f\u3044\u304b\u306a\u304b\u3063\u305f\u306e\u3067Javadoc<\/a>\u3092\u983c\u308a\u306b\u3082\u304c\u3082\u304c\u3057\u305f\u7d50\u679c\u3001\u30b3\u30d4\u30da\u5143\u306e\u30bd\u30fc\u30b9\u306b6\u884c\u76ee\u306e\u3068\u3053\u308d\u304c\u4e0d\u8db3\u3057\u3066\u3044\u305f\u3053\u3068\u304c\u5224\u660e\u3057\u305f\u3002<br \/>\n\u3000\u306a\u304a\u3001\u53d6\u5f97\u3057\u3066\u3044\u308b\u306e\u306f\u30b5\u30fc\u30d0\u30fc\u3068\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u6642\u8a08\u306e\u30ba\u30ec(offset)\u3067\u3042\u308b\u3002<br \/>\n\u3000serverName\u306b\u306fNTP\u30b5\u30fc\u30d0\u30fc\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u5165\u308c\u308b\u30020.jp.pool.ntp.org\u3068\u304bntp.nict.jp\u3068\u304b\u304c\u3044\u3044\u3060\u308d\u3046\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-java\">NTPUDPClient client = new NTPUDPClient();\r\ntry {\r\n  InetAddress address = InetAddress.getByName(serverName);\r\n  client.open();\r\n  TimeInfo ti = client.getTime(address);\r\n  ti.computeDetails();\r\n  long offset = ti.getOffset();\r\n  System.out.println(&quot;Offset:&quot; + offset);\r\n} catch (Exception e) {\r\n  e.printStackTrace();\r\n}\r\n<\/code><\/pre>\n<p><\/p>\n<h3>\u30fb\u305d\u3082\u305d\u3082NTP\u3068\u306f<\/h3>\n<p>\u3000NTP\u306f Network Time Protocol\u306e\u7565\u3002\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3092\u4f7f\u3063\u3066\u3001\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u540c\u58eb\u306e\u6642\u523b\u3092\u540c\u671f\u3055\u305b\u308b\u30d7\u30ed\u30c8\u30b3\u30eb\u306e\u3053\u3068\u3002<br \/>\n\u3000\u7c21\u5358\u306a\u539f\u7406\u3092\u8aac\u660e\u3059\u308b\u3068\u3001<\/p>\n<p style=\"align-left:15px;\">\u30001. \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u6642\u523b \\(t_1\\) \u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3059\u308b\u3002<br \/>\n\u30002. \u30b5\u30fc\u30d0\u30fc\u306f\u6642\u523b \\(t_2\\) \u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u53d7\u4fe1\u3059\u308b\u3002<br \/>\n\u30003. \u30b5\u30fc\u30d0\u30fc\u304c\u6642\u523b \\(t_3\\) \u306b\u5fdc\u7b54\u3059\u308b\u3002<br \/>\n\u30004. \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306f\u6642\u523b \\(t_4\\) \u306b\u5fdc\u7b54\u3092\u53d7\u3051\u53d6\u308b\u3002<\/p>\n<p>\u3000\u305f\u3060\u3057\u3053\u3053\u3067 \\(t_1\\) ~ \\(t_4 \\)\u306f\u5404\u3005\u306e\u6a5f\u5668\u306e\u6642\u523b\u3067\u3042\u308b\u3002\u56f3\u306b\u3059\u308b\u3068\u3053\u3046\u3060\u3002<br \/>\n<img loading=\"lazy\" src=\"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-content\/uploads\/2017\/07\/NTP.png\" alt=\"\" width=\"800\" height=\"600\" class=\"alignnone size-full wp-image-109\" srcset=\"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-content\/uploads\/2017\/07\/NTP.png 800w, https:\/\/mell0w-5phere.net\/jaded5phere\/wp-content\/uploads\/2017\/07\/NTP-300x225.png 300w, https:\/\/mell0w-5phere.net\/jaded5phere\/wp-content\/uploads\/2017\/07\/NTP-768x576.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><br \/>\n\u3000\u3053\u3053\u304b\u3089\u3001\u30b5\u30fc\u30d0\u30fc\u3068\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u6642\u523b\u306e\u5dee\u3092\u6c42\u3081\u308b\u306b\u306f\u3001\u307e\u305a\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u5f80\u5fa9\u306b\u304b\u304b\u3063\u305f\u6642\u9593\uff08Roundtrip Time\uff09\u3092\u6c42\u3081\u308b\u3002\u3053\u308c\u306f\u7c21\u5358\u3067\u3001\u3053\u306e\u6642\u9593\u3092\\(d\\)\u3068\u304a\u304f\u3068<br \/>\n$$d=(t_4-t_1)-(t_3-t_2)\\;\\;\\;\\;\\cdots(1)$$\u3068\u306a\u308b\u306e\u306f\u304a\u308f\u304b\u308a\u3060\u308d\u3046\u3002<br \/>\n\u3000\u3053\u3053\u3067\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u6642\u523b\u304c \\(\\Delta t\\) \u3060\u3051\u9045\u308c\u3066\u3044\u308b\u3068\u3059\u308b\u3068\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u6642\u523b \\(t_1\\) \u306f\u30b5\u30fc\u30d0\u30fc\u3067\u306f \\(t_1+\\Delta t\\) \u306b\u306a\u308b\u3002\u307e\u305f\u3001\u5f80\u8def\u3068\u5fa9\u8def\u3067\u304b\u304b\u308b\u6642\u9593\u306f\u7b49\u3057\u3044\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u9001\u4fe1\u3057\u305f\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u5230\u9054\u3059\u308b\u306e\u306b\\(\\displaystyle \\frac d2\\) \u3060\u3051\u7d4c\u904e\u3059\u308b\u304b\u3089\u3001\u30b5\u30fc\u30d0\u30fc\u304c\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u53d7\u3051\u53d6\u308b\u6642\u523b \\(t_2\\) \u306f<br \/>\n$$t_2=t_1+\\frac d2 +\\Delta t$$\u3068\u306a\u308b\u3002\u3053\u306e\u5f0f\u306b(1)\u3092\u4ee3\u5165\u3059\u308b\u3068\u3001$$\\Delta t=\\frac{(t_2-t_1)+(t_3-t_4)}{2}$$\u3068\u306a\u308b\u3002\u3053\u308c\u304c\u30b5\u30fc\u30d0\u30fc\u3068\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u6642\u523b\u306e\u30ba\u30ec\u306b\u306a\u308b\u3002\u305f\u3060\u3001\u3053\u308c\u306f\u5f80\u8def\u3068\u5fa9\u8def\u306e\u6240\u8981\u6642\u9593\u304c\u7b49\u3057\u3044\u3068\u4eee\u5b9a\u3057\u305f\u7d50\u679c\u3060\u304b\u3089\u3001\u8aa4\u5dee\u306f\u6700\u5927\u3067\\(\\displaystyle\\pm\\frac d2\\)\u3060\u3051\u767a\u751f\u3057\u3046\u308b\u3002<br \/>\u4ee5\u4e0a\u3092\u8e0f\u307e\u3048\u3066\u3088\u308a\u539f\u7406\u7684\u306a\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/a>\u3092\u66f8\u304f\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<pre class=\"line-numbers\" ><code class=\"language-java\">NTPUDPClient client = new NTPUDPClient();\r\ntry {\r\n  InetAddress address = InetAddress.getByName(serverName);\r\n  client.open();\r\n  TimeInfo ti = client.getTime(address);\r\n  NtpV3Packet packet = ti.getMessage();\r\n  long t1 = packet.getOriginateTimeStamp().getTime();\r\n  long t2 = packet.getReceiveTimeStamp().getTime();\r\n  long t3 = packet.getTransmitTimeStamp().getTime();\r\n  long t4 = ti.getReturnTime();\r\n  long offset = (t2 - t1 + t3 - t4)\/2;\r\n  System.out.println(&quot;Offset:&quot; + offset);\r\n} catch (Exception e) {\r\n  e.printStackTrace();\r\n}\r\n<\/code><\/pre>\n<p><\/p>\n<hr>\n<p>\u3000\u4ee5\u4e0a\u3002\u5916\u90e8\u30e9\u30a4\u30d6\u30e9\u30ea\u3063\u3066\u3079\u3093\u308a\u3067\u3059\u306d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u7aef\u672b\u6642\u523b\u306e\u30ba\u30ec\u304c\u9177\u304b\u3063\u305f\u306e\u3067NTP\u30b5\u30fc\u30d0\u30fc\u304b\u3089\u6642\u523b\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u3057\u305f\u3002 \u30fb\u3068\u308a\u3042\u3048\u305a\u52d5\u3051\u3070\u3044\u3044\u4eba\u7528 \u3000\u5148\u306b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u7f6e\u3044\u3066\u304a\u304f\u3002\u5916\u90e8\u30e9\u30a4\u30d6\u30e9\u30ea(org.apache.commons.net.ntp)\u3092\u4f7f\u3046\u3002 \u3000\u6700<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[15,26],"_links":{"self":[{"href":"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-json\/wp\/v2\/posts\/73"}],"collection":[{"href":"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":6,"href":"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":736,"href":"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-json\/wp\/v2\/posts\/73\/revisions\/736"}],"wp:attachment":[{"href":"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mell0w-5phere.net\/jaded5phere\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}