所以这里有一个测试流行的 RuneScape 游戏的每个服务器的应用程序。我在 139 台服务器上运行 ping 并向数组添加延迟值。在遍历列表条目时,我可以计算每个服务器的平均、最小和最大延迟。
现在,在我的代码中,当注意到其中一台无法访问的服务器时,最低延迟显示为 0。如果我的 ping 响应已达到超时并将 0 发布到数组列表,我不确定如何处理。
当服务器关闭时,我能做些什么来避免 0 作为我的最低 ping?
using System;
using System.Net.NetworkInformation;
using System.Collections.Generic;
namespace RuneScape_Ping_Tool
{
class Program
{
static void Main(string[] args)
{
Console.Write(Environment.NewLine + "Start the test? (y/n): ");
if (Console.Read() == char.Parse("y"))
{
Console.WriteLine();
Ping();
}
}
static void Ping()
{
List<int> lag = new List<int>();
for (int server = 1; server <= 139; server++)
{
string url = "world" + server.ToString() + ".runescape.com";
Console.WriteLine("Checking world " + server + "...");
Ping ping = new Ping();
PingReply reply = ping.Send(url);
lag.Add(int.Parse(reply.RoundtripTime.ToString()));
}
for (int i = 1; i <= 139; i++)
{
Console.WriteLine("World " + i + ": " + lag[i - 1]);
}
int average = 0;
int highest = 1;
int lowest = 1000;
int highestWorld = 0;
int lowestWorld = 0;
for (int i = 1; i <= 139; i++)
{
average = average + lag[i - 1];
}
for (int i = 1; i <= 139; i++)
{
if (highest < lag[i - 1])
{
highest = lag[i - 1];
highestWorld = i;
}
}
for (int i = 1; i <= 139; i++)
{
if (lowest > lag[i - 1])
{
lowest = lag[i - 1];
lowestWorld = i;
}
}
Console.WriteLine();
Console.WriteLine("Average lag: " + average / 139);
Console.WriteLine("Highest lag: " + highest + " in world " + highestWorld);
Console.WriteLine("Lowest lag: " + lowest + " in world " + lowestWorld);
Console.Write(Environment.NewLine + "Start the test? (y/n): ");
if (Console.Read() == char.Parse("y"))
{
Console.WriteLine();
Ping();
}
}
}
}