So here is an application which tests each server of popular RuneScape game. I am running ping on each of 139 servers and add lag value to array. While looping through list entries I can calculate the average, minimum, and maximum lag for each server.
Now, in my code when one of the unreachable servers is noted the lowest lag displays as 0. I’m not sure how to handle if my ping response has reached it’s timeout and posts 0 to array list.
What can I do to avoid that 0 as my lowest ping when server is down?
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();
}
}
}
}
Can you just skip over zeroes? Also, you might as well put all the calculations inside a single loop.