Thanks for the quick replies.
I've got the right format now for the time, but it is not showing the correct time, unless that is what the server sends me.
more questions:
1) How could I check that what I have is the right sequence of bits? I don't know what type of machine is at the other end.
2) Am I still using pointers incorrectly?
3) If I put unsigned long int time2 = 0; lower down in the code, for example where the /**/ is the compiler (gcc) complains with a parse error. Why?
Seron
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <time.h>
#define SERVER_PORT 37
int main() {
time_t time = 0;
int sock;
struct sockaddr_in sin;
struct hostent *hp;
char *host = "roman";
char time_string[27];
char *tsp = time_string;
unsigned long int time2 = 0;
time_string[26] = '\0';
// for debugging
printf("time_string = %d\n", time_string);
printf("time = %d\n", time);
printf("host = %d\n", host);
hp = gethostbyname(host);
if(!hp) {
printf("unknown host: %s\n", host);
exit(-1);
}
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
sin.sin_port = htons(SERVER_PORT);
if((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(-1);
}
if(connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("connect");
exit(-1);
}
recv(sock, &time, 4, 0); // put some errorchecking here
tsp = ctime(&time);
/**/
// for debugging
printf("time = %d\n", time);
memmove( &time2, &time, sizeof(time2) );
printf("time = %u\n", time2 );
// prints the time
printf("%s\n", tsp);
tsp = ctime(&time2);
printf("%s\n", tsp);
}
output:
Code:
time_string = -4261488
time = 0
host = 68248
time = -1049019304
time = 3245947992
Sun Oct 4 14:44:56 1936
Sun Oct 4 14:44:56 1936