another question about time()

Abby (J.P.) the.ueabraham at laposte.net
Wed Apr 26 13:09:05 PDT 2006


Hello, I'm trying to make a ping utility, I made a working ICMP request 
(I tested it with Ethereal, I get a reply from the server). But I wonder 
how much precise are the std.c.time.time() or the std.date.time(), 
because I always find a ping of 0ms (I know that it must be 1000 ticks 
per seconds, but I'm not sure, this really is).
Here is my code :


char[] host = "www.microsoft.com"
InternetHost targetHost;
targetHost.getHostByName(host);

InternetAddress targetAddress = new 
InternetAddress(targetHost.addrList[0], InternetAddress.PORT_ANY);

Socket pingingSocket = new Socket(AddressFamily.INET, SocketType.RAW, 
ProtocolType.ICMP);

//pingingSocket.connect(targetAddress);
//long start_time = getUTCtime();
int start_time;
std.c.time.time(&start_time);

pingingSocket.sendTo(PING_REQUEST, targetAddress);

char[] response;
pingingSocket.receive(response);

//long end_time = getUTCtime();
int end_time;
std.c.time.time(&end_time);

// char [] str_ping = std.string.toString((end_time - start_time));
char [] str_ping = std.string.toString((end_time - start_time)/2);
pingingSocket.close();
printf("ping : " ~ str_ping);

As you can see, I tried both std.c.time and std.date functions, but 
str_ping is always 0. Did I done something wrong ? Should I put the 
start_time before the resolving getHostByName() ?

--
Abby



More information about the Digitalmars-d-learn mailing list