Hello there!

I've built a simple server and a simple client in C (Linux).
Among other tasks, I need the server to send the timestamp (using time.h) to the client.
Then I need the client to change the system time based on the timestamp received from the server.

What would be the best way?
I already get the timestamp in the client, but as a string after using asctime, this way:

time_t now;
struct tm* myTime;

now = time(NULL); 
myTime = localtime(&now);

strcpy(msgTime, asctime(myTime));
int msgSize = strlen(msgTime);

send(sockCli, msgTime, msgSize, 0);

But this is just a string...
Is there a way of receiving the number of seconds in the client and thus making it a lot easier to do time operations? How?

Thank you for any hints!

Recommended Answers

All 5 Replies

i dont understand your question.

you want to send the number of seconds?

well.... the number of seconds of "what"?

You got the time in string and you want to manipulate it?

commented: waste of space -1

You can use atoi() for converting "ASCII" to "integer". :)

Instead of sending the string returned by asctime() why now just convert the integer returned by time() to a string and send that? Then on client side just convert back to int and use it to change the computer's clock. If client needs the string returned by asctime() then it can get it itself from the string you send that was returned by time().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.