我正在尝试构建一个示例应用程序,它将展示与符合 RFC 868 的时间服务器同步时间的概念证明。
到目前为止,使用 Java Socket API,我能够连接和查询服务器并从服务器获取响应,但它不是人类可读的格式。
我得到的响应是:�)6
我认为响应是二进制格式的(但不确定)。 RFC 868表示以 32 位二进制数发送时间
。
我的问题是:
- 如何解析此响应?
- 除了我的这种方法之外,我想知道是否还有其他推荐的方法可以实现这一目标。
提前致谢。
最佳答案
1) How do I parse this response?
查看 source code of TimeTCPClient来自 Apache Commons Net 库:
public long getTime() throws IOException {
DataInputStream input;
input = new DataInputStream(_input_);
return (input.readInt() & 0xffffffffL);
}
public Date getDate() throws IOException {
return new Date((getTime() - SECONDS_1900_TO_1970)*1000L);
}
2) Apart from this approach of mine, I'd like to know if there is any other recommended approach which I should take to achieve this.
使用 Apache Commons Net Library,查看 API of TimeTCPClient .
Apache Commons Net home page ,希望这有帮助。
关于java - 解析二进制响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10041700/