Hey all, I am trying to determine the status of some servers over the course of time. Here is my code:
static void Main(string[] args)
{
byte[] readstream = new byte[100];
byte[] sendstream = Encoding.ASCII.GetBytes("PLAYER_JOINED");
string[] IPs = new string[] { "24.15.169.211", "69.198.255.121", "219.79.244.225" };
string[] Name = new string[10];
string[] Port = new string[10];
int timeout = 5000;
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.ReceiveTimeout = timeout;
while (true)
{
int i = 0;
foreach (string str in IPs)
{
IPAddress address = IPAddress.Parse(str);
IPEndPoint CheckingServer = new IPEndPoint(address, 8190);
try
{
s.Connect(CheckingServer);
s.Send(sendstream);
s.Receive(readstream);
Console.WriteLine(Encoding.ASCII.GetString(readstream));
Console.WriteLine("{0}: Server Up", IPs[i]);
}
catch
{
Console.WriteLine("{0}: NO SUCH SERVER", IPs[i]);
}
i++;
}
Thread.Sleep(1000);
}
}
When I run this code, it shows that 24.15.169.211 is up THE FIRST TIME IT RUNS THROUGH, but then reverts to no such server in subsequent tests. Why?