c/c++ - safest way to send time_t over socket

前端 未结 4 1762
暖寄归人
暖寄归人 2021-02-20 11:47

I\'ve set up a C++ server/client environment, and am trying to send a time_t value from the server to the client (an useful thing in any server). But I\'m coming accross a heada

4条回答
  •  余生分开走
    2021-02-20 12:13

    You could send a textual representation produced by strftime in combination with gmtime. The representation would be a bit larger than a binary representation, but not huge. E.g., the format string "%Y%j%H%M%S" produces a 13-byte representation of a moment in time (excluding a NUL character).

    EDIT: forget about my previous advice to use ctime; that uses localtime and would therefore only work if client and server are in the same timezone. And apparently, asctime is unsafe, so use strftime.

提交回复
热议问题