JOCKEL3000
JOCKEL3000

Reputation: 29

How to make Things interact, when a server is online or offline

So im very new to c# and someone asked me to make him a Windows Forms Application, in which you were able to see, if their Servers are online or offline. He wanted me to make an aquarium, in which the fishes(Servers) swim, while they are online and when they are offline, they just lay on the surface of the water. But i dont know, who to include the fishes into my code:

 private static bool IsServer1Up(string hostName)
        {
            bool retVal = false;
            try
            {
                Ping pingSender = new Ping();
                PingOptions options = new PingOptions();
                options.DontFragment = true;
                string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                int timeout = 120;

                PingReply reply = pingSender.Send(hostName, timeout, buffer, options);
                if (reply.Status == IPStatus.Success)
                {
                    retVal = true;
                }
            }
            catch (Exception ex)
            {
                retVal = false;

            }
            return retVal;
        }

This was what i thought of. If you could hlp me that would be very great

PS: Sorry if I missspelled something or there were many grammar mistakes, Im 11 years old and from Germany :D

Upvotes: 2

Views: 58

Answers (1)

SirBirne
SirBirne

Reputation: 297

I try to get you on the right way.

First you need a class representing your servers. This could look something like that:

class Server
{
     public string HostName { get; private set; }
     public bool IsOnline { get; private set; }

     public Server(string hostName)
     {
        HostName = hostName;
     }

     public bool CheckState()
     {
        IsOnline = YourLogicForChecking(HostName);
        return IsOnline;
     }
}

In your Application you need to initialze a Collection of all your servers. Assuming that you know how to draw your fishes you can than implement a method wich does something like that.

public void DrawFish(Server s)
{
   /* your drawing logic, if server is offline the fish is dead, else it is alive */
}

Then you should use a timer which updates the state of your servers and calls the drawing function for each server. Here is an example what your Form could look like.

public MainForm()
{
    InitializeComponent();

    Servers = LoadAllServers();
    timer.Interval = 1000;
    timer.Start();

    timer.Tick += Timer_Tick;

 }

 public void Timer_Tick(object sender, EventArgs e)
 {
      foreach(var s in Servers)
      {
         s.CheckState();
         DrawFish(s);
      }
 }

Upvotes: 1

Related Questions