下面的代碼是我用來ping 8.8.8.8,但有些時候,當我按開始它只是包丟失,這很重要,這很奇怪,因爲我確信我不'沒有那麼多的數據包丟失,然後我暫停並重新開始,它工作得很好,對我來說沒有意義,我不知所措。獲取隨機不變的數據包丟失
我加了Thread.Sleep();因爲我猜想這個程序在啓動時可能不會加載,但這也很奇怪!它是這樣一個小而簡單的程序,它不應該有這樣的問題
我也有一個問題,我應該把這個時間放在什麼時候,現在應用程序每秒發送一個數據包,並每隔900毫秒丟棄一次,我應該把它放在900以上還是會降低性能?
public partial class Form1 : Form
{
public int PingTime;
public int pingor = 0;
public int pingur = 0;
public int maxping;
public int minping;
public Int64 avgping;
public string avrage;
public int ping_no;
public string fail;
public string msg1 = " Packet Lost ";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
{
timer1.Enabled = false;
Thread.Sleep(1000);
}
else if (timer1.Enabled == false)
{
timer1.Enabled = true;
Thread.Sleep(1000);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
bool pingable = false;
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "1";
byte[] buffer = Encoding.ASCII.GetBytes(data);
Ping pinger = new Ping();
try
{
//37.58.117.146
//8.8.8.8
PingReply reply = pinger.Send("8.8.8.8", 900, buffer, options);
pingable = reply.Status == IPStatus.Success;
string pingtime = reply.RoundtripTime.ToString();
int newpingtime = Convert.ToInt32(pingtime);
if (reply.Status == IPStatus.Success)
{
label1.Text = reply.RoundtripTime.ToString();
}
else
{
label1.Text = msg1;
}
//_________________________Max Ping
if (maxping == 0)
{
maxping = Convert.ToInt32(newpingtime);
}
else if (newpingtime >= Convert.ToInt32(maxping))
{
maxping = Convert.ToInt32(newpingtime);
}
lblmax.Text = maxping.ToString();
//_________________________Min Ping
if (reply.Status == IPStatus.Success && minping == 0 && newpingtime == 0)
{
lblmin.Text = minping.ToString();
}
else if (reply.Status == IPStatus.Success && lblmin.Text == " - - -" && newpingtime >= 0)
{
minping = newpingtime;
lblmin.Text = minping.ToString();
}
else if (reply.Status == IPStatus.Success && newpingtime < minping)
{
minping = newpingtime;
lblmin.Text = minping.ToString();
}
//_________________________Ping AVG
if (reply.Status == IPStatus.Success)
{
avgping = avgping + newpingtime;
ping_no = ping_no + 1;
lblavg.Text = Convert.ToString(avgping/ping_no);
}
}
catch (PingException error)
{
MessageBox.Show(error.Message);
}
if (pingable == true)
{
pingor++;
}
if (pingable == false)
{
pingur++;
}
lblrecived.Text = Convert.ToString(pingor);
lbllost.Text = Convert.ToString(pingur);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Thread.Sleep(900);
}
在此先感謝,只是讓你知道,我並沒有丟包超過2%,這不應該表現出收到40個丟包和0!它也正常工作80%的時間!
再一次當我暫停並快速啓動時,或第一次啓動它時,會發生這種情況。
Ping是不是權威的答案,世界 - 平具有較低的網絡優先級,所以是的,中國平安可以報告數據包丟失時沒有任何..但是,它是其他問題的指示。 – BugFinder
但使用CMD,Powershell和Pingplotter我沒有看到這樣的問題,我需要修復它,以使其成爲一個可用的應用程序。 –
你確定你沒有收到例外 - 你正默默地壓制它們。 –