Ho modificato un codice trovato sul web per eseguire un ping verso un'indirizzo specificato in una textbox1. Il programma è scritto in c# .net e presenta un problema durante l'esecuzione. Il ping verso il sito viene eseguito per 10 volte, succede però che nel risultato (stampato in un textbo2) certe volte compare come tempo impiegato <1ms. Il test viene eseguito verso maya.ngi.it.
codice:
private void ping()
{
byte[] data = new byte[1024];
int recv, timestart, timestop;
//creo socket pe comunicazione
Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
IPHostEntry iphe = Dns.GetHostEntry(textBox1.Text);
IPEndPoint iep = new IPEndPoint(iphe.AddressList[0], 0);
EndPoint ep = (EndPoint)iep;
//creo pacchetto da inviare
ICMP packet = new ICMP();
packet.Type = 0x08;
packet.Code = 0x00;
packet.Checksum = 0;
Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 0, 2);
Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 2, 2);
data = Encoding.ASCII.GetBytes("aaaabbbb");
Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length);
packet.MessageSize = data.Length + 4;
int packetsize = packet.MessageSize + 4;
UInt16 chcksum = packet.getChecksum();
packet.Checksum = chcksum;
textBox2.Text = "Inizio ping verso: " + ep.ToString() + "\r\n";
host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
int badcount = 0;
string time = "";
//invio il comando per il ping per 10 volte
for (int i = 0; i < 10; i++)
{
host.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, 35);
host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
//misuro il tempo impiegato
timestart = Environment.TickCount;
//attendo una risposta
try
{
data = new byte[1024];
recv = host.ReceiveFrom(data, ref ep);
timestop = Environment.TickCount;
ICMP response = new ICMP(data, recv);
if ((timestop - timestart) < 1)
{
time = "<1";
}
else
{
time = (timestop - timestart).ToString();
}
//stampo il risultato
if (response.Type == 0 )
{
textBox2.Text = textBox2.Text + " response from " + ep.ToString() + ", " + time + "ms \r\n";
//label1.Text = label1.Text + ep.ToString() + " riched in " + i + " hops " + totaltime + "ms \n";
}
badcount = 0;
}
catch (SocketException)
{
textBox2.Text = textBox2.Text + "no response \r\n";
badcount++;
if (badcount == 5)
{
textBox2.Text = textBox2.Text + "no response host \r\n";
}
}
}
host.Close();
}
ecco qui due esempi con il programma e con ping.exe
Inizio ping verso: 88.149.128.3:0
response from 88.149.128.3:0, <1ms
response from 88.149.128.3:0, 31ms
response from 88.149.128.3:0, <1ms
response from 88.149.128.3:0, 32ms
response from 88.149.128.3:0, <1ms
response from 88.149.128.3:0, 15ms
response from 88.149.128.3:0, 16ms
response from 88.149.128.3:0, 15ms
response from 88.149.128.3:0, 16ms
response from 88.149.128.3:0, 16ms
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Daniele>ping maya.ngi.it
Pinging maya.ngi.it [88.149.128.3] with 32 bytes of data:
Reply from 88.149.128.3: bytes=32 time=14ms TTL=53
Reply from 88.149.128.3: bytes=32 time=13ms TTL=53
Reply from 88.149.128.3: bytes=32 time=14ms TTL=53
Reply from 88.149.128.3: bytes=32 time=12ms TTL=53
Ping statistics for 88.149.128.3:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 12ms, Maximum = 14ms, Average = 13ms
Dov'è che sbaglio?? GRazie