-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ping.cs
60 lines (48 loc) · 2.37 KB
/
Ping.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Net.NetworkInformation;
class Program {
static void Main(string[] args) {
// Add a title to the window
Console.Title = "Ping Explorer";
// Create a new Ping object to send ping requests
Ping pingSender = new Ping();
// Define the list of server names and regions to ping
string[] serverNames = {"google.com", "www.amazon.fr", "baidu.com", "www.spiegel.de", "yandex.ru"};
string[] serverRegions = {"US", "FR", "CN", "DE", "RU"};
// Loop through each server and ping it
for (int i = 0; i < serverNames.Length; i++) {
// Get the name and region of the current server
string serverName = serverNames[i];
string serverRegion = serverRegions[i];
// Send a ping request to the server and wait for the response
PingReply reply = pingSender.Send(serverName);
// If the ping request was successful, print the ping time and color it based on the value
if (reply.Status == IPStatus.Success) {
// Get the ping time in milliseconds
int pingTime = (int) reply.RoundtripTime;
// Get the current console text color
string pingColor = Console.ForegroundColor.ToString();
// Choose the color for the ping time based on its value
if (pingTime < 80) {
Console.ForegroundColor = ConsoleColor.Green; // green for fast ping times
}
else if (pingTime < 140) {
Console.ForegroundColor = ConsoleColor.DarkYellow; // yellow for moderate ping times
}
else {
Console.ForegroundColor = ConsoleColor.Red; // red for slow ping times
}
// Print the server name, region, and ping time
Console.WriteLine($"Server {serverRegion} : {pingTime} ms");
// Reset the console text color to white
Console.ForegroundColor = ConsoleColor.White;
}
else {
// If the ping request failed, print an error message
Console.WriteLine($"Unable to contact the server {serverRegion}");
}
}
// Wait for the user to press a key before closing the console window
Console.Read();
}
}