www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - another question about time()

reply "Abby (J.P.)" <the.ueabraham laposte.net> writes:
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
Apr 26 2006
next sibling parent reply Tydr Schnubbis <fake address.dude> writes:
Abby (J.P.) wrote:
 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

I can't ping www.microsoft.com at all, did you try another address, like google.com?
Apr 26 2006
parent reply "Abby (J.P.)" <the.ueabraham laposte.net> writes:
Tydr Schnubbis wrote:
 Abby (J.P.) wrote:
 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

I can't ping www.microsoft.com at all, did you try another address, like google.com?

yes, I tried few addresses (I thought microsoft.com would work also) IP addresses on my LAN (with getHostByAddr) produce the same probleme. But you're right, I should check the char[] response to see if the server has anwsered me ! I'm going to use std.perf as Dereck told me (I hope it will work !) Thanks for your anwsers -- Abby
Apr 26 2006
parent "Abby (J.P.)" <the.ueabraham laposte.net> writes:
Abby (J.P.) wrote:
 Tydr Schnubbis wrote:
 Abby (J.P.) wrote:
 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

I can't ping www.microsoft.com at all, did you try another address, like google.com?

yes, I tried few addresses (I thought microsoft.com would work also) IP addresses on my LAN (with getHostByAddr) produce the same probleme. But you're right, I should check the char[] response to see if the server has anwsered me ! I'm going to use std.perf as Dereck told me (I hope it will work !) Thanks for your anwsers -- Abby

comes from pingingSocket.receive(response) The string is always null, and that might also be the cause of my bad timer. But I can see the resquest AND the reply fom the server in ethereal. Is there something special to do, when working with ICMP packets ? I didn't use connect(Address) so my socket is not connected, maybe I should specify that it has to listen for incoming packets ?
Apr 27 2006
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 26 Apr 2006 22:09:05 +0200, Abby (J.P.) wrote:

 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() ?

You might be better to use another undocumented module 'std.perf'. For example ... import std.perf; . . . ProcessTimesCounter counter = new ProcessTimesCounter(); counter.start(); . . . the stuff you want to time . . . counter.stop(); writefln("Duration: %s microseconds", counter.microseconds); -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 27/04/2006 10:50:33 AM
Apr 26 2006
parent Bruno Medeiros <brunodomedeirosATgmail SPAM.com> writes:
Derek Parnell wrote:
 On Wed, 26 Apr 2006 22:09:05 +0200, Abby (J.P.) wrote:
 
 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() ?

You might be better to use another undocumented module 'std.perf'. For example ... import std.perf; . . . ProcessTimesCounter counter = new ProcessTimesCounter(); counter.start(); . . . the stuff you want to time . . . counter.stop(); writefln("Duration: %s microseconds", counter.microseconds);

Wouldn't clock() suffice ? -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Apr 30 2006