Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 java如何獲取NTP服務時間?
skwujinhua





发贴: 104
积分: 11
于 2008-10-14 09:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位老鳥﹐工作上有點問題﹐請教。

用java編程﹐通過NTP服務器的IP地址得到NTP服務器的時間﹐我該怎么做?
脆求﹗﹗﹗可不可以給我源代碼?
問題解決請吃飯。(深圳地區)



作者 Re:java如何獲取NTP服務時間? [Re:skwujinhua]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-10-14 17:24 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
I'm not familiar with the NTP server to be honest, but I do believe in that there is no difference between a NTP server and other standard servers.

Let me introduce the following wikipedia page for NTP protocol.
http://en.wikipedia.org/wiki/Network_Time_Protocol

The page stated as "NTP uses UDP port 123 as its transport layer. It is designed particularly to resist the effects of variable latency by using a jitter buffer.".

Therefore first of all, it is better to check the NTP server that you are working on has the transport port [UDP] 123 opened or not. Maybe do a telnet will identify this very quickly. If the targeted port 123 is not opened on the server (which I think is very impossible), then you need to find the manual or any document to find out which port running NTP protocol is opened on your server.

Secondly, the page also stated that "The operational details of NTP are illustrated in RFC 778, RFC 891, RFC 956, RFC 958, and RFC 1305. The current reference implementation is version 4 (NTPv4); however, as of 2005, only versions up to 3 (1992) have been documented in RFCs. The IETF NTP Working Group has formed to standardize the work of the NTP community since RFC 1305 et al."

Therefore you need to find out which version of NTP your server is operating on. This could be done by looking at the above RFC documents first. For example, do a very quick tcpdump or pdu (protocol data unit) capture will identify this.

Once you have all these questions cleared out, you can implement network code in Java very easily. Nearly every network book mentions this part.
Let me introduce this book for you:
http://books.google.ie/books?hl=en&id=NyxObrhTv5oC&dq=java+network+programming&printsec=frontcover&source=web&ots=1dbMAIYlNW&sig=FLdQwEeaK0METOjeuM78AJGhq1A&sa=X&oi=book_result&resnum=5&ct=result

Also see the below example as a UDP network application for a start:

try {

DatagramSocket client = new DatagramSocket( );

// send packets...

}

catch (SocketException ex) {

System.err.println(ex);

}

You would use this constructor in a client that initiates a conversation with a server. In this scenario, you don't care what port the socket is bound to, because the server will send its response to the port from which the datagram originated. Letting the system assign a port means that you don't have to worry about finding an unused port. If for some reason you need to know the local port, you can find out with the getLocalPort( ) method described later in this chapter.

The same socket can receive the datagrams that a server sends back to it. A SocketException is thrown if the socket can't be created. It's unusual for this constructor to throw an exception; it's hard to imagine situations in which the socket could not be opened, since the system gets to choose the local port.
Jiafan



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:java如何獲取NTP服務時間? [Re:skwujinhua]
skwujinhua





发贴: 104
积分: 11
于 2008-10-15 08:20 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝版主﹐研究中.有問題再請教。(對了﹐你的英語真行。)


作者 Re:java如何獲取NTP服務時間? [Re:skwujinhua]
skwujinhua





发贴: 104
积分: 11
于 2008-10-15 11:50 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
大哥﹐你知道不知道怎么分析數據包?
對數據包進行處理?



作者 Re:java如何獲取NTP服務時間? [Re:skwujinhua]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-10-15 18:07 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Link this thread to the following one:
http://www.cjsdn.net/post/view?bid=1&id=196300&tpg=1&ppg=1&sty=1



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923