Getting -ve timestamp values. - Unix
This is a discussion on Getting -ve timestamp values. - Unix ; I am not a very good unix programmer. I'm trying to get timestamps for
my packets. I tried using the SO_TIMESTAMP flag for obtaining the time
stamp. But it is returning me -ve timestamps. Can you tell me what the
...
-
Getting -ve timestamp values.
I am not a very good unix programmer. I'm trying to get timestamps for
my packets. I tried using the SO_TIMESTAMP flag for obtaining the time
stamp. But it is returning me -ve timestamps. Can you tell me what the
problem is in my code. I checked the system date and time and it is
absolutely correct.
_________________________________________
/*
** client.c -- a stream socket client demo
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#define PORT 3490 // the port client will be connecting to
#define MAXDATASIZE 100 // max number of bytes we can get at once
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[30];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address
information
struct msghdr msg;
struct cmsghdr *cmsg;
int on =1;
struct timeval tval;
struct timeval *tv;
time_t ts;
time_t tstamp;
char *time;
char *buffer;
struct tm *tm_time;
if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) { // get the host
info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short, network byte
order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of
the struct
if (connect(sockfd, (struct sockaddr *)&their_addr,
sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) != 0) {
perror("setsockopt");
return -1;
}
if ((numbytes=recv(sockfd,&msg, MAXDATASIZE-1, 0)) == -1) {
perror("recv");
exit(1);
}
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg !=NULL ; cmsg =
CMSG_NXTHDR(&msg,cmsg))
{
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type
== SO_TIMESTAMP) {
tv = (struct timeval *) CMSG_DATA(cmsg);
ts = tv->tv_sec*1000000;
ts +=tv->tv_usec;
}
}
printf("___________Output of gettimeofday()------------\n\n");
gettimeofday(tval,NULL);
tstamp = tval.tv_sec;
strftime(buffer,30,"%m-%d-%Y %T.",localtime(&tstamp));
printf("%s%ld\n",buffer,tval.tv_usec);
printf("___________Output of SO_TIMESTAMP------------\n\n");
time = ctime(&ts);
printf("Received: %s at time: %d = %s",&msg,ts,time);
close(sockfd);
return 0;
}
_________________________________OUTPUT___________ __________
~/Desktop/jd_progs> ./cClient localhost
___________Output of gettimeofday()------------
12-31-1969 18:00:01.-1208777040
___________Output of SO_TIMESTAMP------------
Received: Hello, world!
at time: -1208653732 = Sun Sep 13 17:51:08 1931
-
Re: Getting -ve timestamp values.
jdthebigj@gmail.com wrote:
> I am not a very good unix programmer. I'm trying to get timestamps for
> my packets. I tried using the SO_TIMESTAMP flag for obtaining the time
> stamp. But it is returning me -ve timestamps. Can you tell me what the
> problem is in my code. I checked the system date and time and it is
> absolutely correct.
[snip]
>
> time = ctime(&ts);
> printf("Received: %s at time: %d = %s",&msg,ts,time);
I have no idea what you mean by "-ve", but assume that you mean
"negative". If so, work on your spelling.
Anyway, time_t is not a signed type, it is unsigned. To print it, use
%lu and cast the value to (unsigned long).
printf("Received: %s at time: %lu = %s",&msg,(unsigned long)ts,time);
HTH
Bjørn
[snip]
--
Looking for an embeddable web server?
http://www.metasystems.no/products/h...der/index.html
-
Re: Getting -ve timestamp values.
"B. Augestad" wrote, on Sun, 08 Apr 2007:
> Anyway, time_t is not a signed type, it is unsigned.
It may happen to be unsigned on your system, but on most systems it
is signed. It is also allowed to be a floating-point type, but I've
never heard of any system where it is.
--
Geoff Clare