开发者

Finding Time Connected in Seconds

开发者 https://www.devze.com 2023-03-25 11:20 出处:网络
When someone connects to my TCP Server, I log the time they connected using System.DateTime dt = System.DateTime.Now;

When someone connects to my TCP Server, I log the time they connected using

System.DateTime dt = System.DateTime.Now;
开发者_开发问答

Then when I need to find how long they have been connected, I do...

    foreach (KeyValuePair<string, System.DateTime> pair in playerList)
    {
         System.DateTime curTime = System.DateTime.Now;
         float connectedTime = (float)(curTime - pair.Value).TotalSeconds;
         writer.Write(connectedTime);
    }

When I debug the value of connectedTime looks correct, but the protocol I am using to report it is seeing it as gibberish.

The C++ code I use which works fine is...

time_t currentTime = time(NULL);
time_t connectedTime = currentTime - g_Users.Element(i).connectTime;
g_UserInfo.WriteFloat(float(tempTime));

Am I doing something silly?


You don't have to cast to float, as you are using the BinarryWirter the Write method can take any integral type: double, float, int.., So:

writer.Write((DateTime.Now.Subtract(pair.Value)).TotalSeconds);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号