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);
精彩评论