因此,我在linux上用C编写了一个时间服务器客户机应用程序,它应该向客户机发送当前的unix时间戳。
它可以很好地工作,但是我被告知time_t可能并不总是大小和字节顺序相同的。如何确保发送客户始终理解的时间?
到现在为止,我只知道
time_t now = htonl(time(0));
然后发过来。
我在google和stackoverflow上搜索,但似乎其他人只发送由ctime()或strftime()生成的时间字符串。
提前感谢!
发布于 2014-06-26 16:40:48
由于发送者和接收方的解释方式不同,一般发送的二进制数据容易出错。
特别是对于time_t
,甚至不清楚将涉及多少位,可能是32位或64位,甚至是更复杂的东西,因为time_t
甚至可能被实现为struct
。
在使用htonl()
的特殊情况下,假定为32位,因为htonl()
的值为32位。
因此,故障安全解决方案确实是发送一个系统时间的文本表示。
从编程上看,这可能如下所示:
char st[64] = "";
{
struct * tm = gmtime(time(NULL));
if (NULL == tm)
{
fprintf(stderr, "gmtime() failed\n");
}
{
if(0 == strftime(st, sizeof(st), "%s", tm)) /* Prints the text representaiotn of the seconds since Epoch into st. */
{
fprintf(stderr, "strftime() failed\n");
}
}
}
要逆转此操作,可以使用strptime()
char st[64] = "123456789123";
time_t t;
memset(&t, 0, sizeof(t));
{
struct tm = {0};
char p = strptime(t, "%s", &tm);
if (NULL == p || p != (t + strlen(t)))
{
fprintf(stderr, "strptime() failed\n");
}
else
{
t = mktime(&tm);
}
}
使用strptime()
和strftime()
的好处是,您可以轻松地更改传输中日期/时间的格式,只需按调用这两个函数时指定的格式进行修改即可。
将"%s"
更改为"%Y-%m-%d %H:%M:%S"
将像"2014-05-20 13:14:15"
一样传输时间。
但是,如果您真的想以二进制格式发送自Epoch以来的秒数,并且保持故障安全和可移植性,那么您需要处理三件事:
解决这一问题的一个可能办法是:
#include <time.h>
#include <inttypes.h> /* For uint64_t, as 64bit should do to represent the seconds since Epoch for the next few years. */
...
time_t t_epochbegin;
memset(&t_epochbegin, 0, sizeof(t_epochbegin);
uint64_t t_host = (uint64_t) difftime(time(NULL), t_epochbegin); /* Get the seconds since Epoch without relying on time_t being an integer. */
uint64_t t_network = htonll(t_host); /* Convert to network byte order. */
关于如何实现非标准htonll()
,请参阅对此问题的各种答案:Big Endian and Little Endian support for byte ordering
上面示例中的所有代码都假设代码运行的系统提供了一个计时器,尽管对time()
的调用不会失败。
https://stackoverflow.com/questions/24435367
复制相似问题