Home > Archive > Unix Programming > April 2007 > Getting -ve timestamp values.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Getting -ve timestamp values.
|
|
| jdthebigj@gmail.com 2007-04-08, 4:13 am |
| 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
//#include <sys/time.h>
#include <time.h>
#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
| |
| B. Augestad 2007-04-08, 8:03 am |
| 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/...nder/index.html
| |
| Geoff Clare 2007-04-12, 10:03 pm |
| "B. Augestad" <boa@metasystems.no> 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 <netnews@gclare.org.uk>
| |
|
|
|
|
|