Bricklayer.Client.Networking.ServerPinger.PingServer C# (CSharp) Method

PingServer() public method

public PingServer ( string host, int port, string &error ) : ServerPingData
host string
port int
error string
return ServerPingData
        public ServerPingData PingServer(string host, int port, out string error)
        {
            ServerPingData pingData = new ServerPingData();
            error = string.Empty;
            //Attempt to contact the server
            try
            {
                using (TcpClient client = new TcpClient())
                {
                    IAsyncResult ar = client.BeginConnect(host, port, null, null);
                    System.Threading.WaitHandle wh = ar.AsyncWaitHandle;
                    try
                    {
                        if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(Common.GlobalSettings.ConnectTimeout), false))
                        {
                            client.Close();
                            error = "Offline (Connection Timeout)";
                            pingData.Error = true;
                            return pingData;
                        }

                        client.EndConnect(ar);
                    }
                    finally
                    {
                        wh.Close();
                    }
                    //Send a single message notifying the server we would like it's stats (0 is the ping request ID)
                    data = new byte[1] { 0x00 };

                    //Get a client stream for reading and writing
                    stream = client.GetStream();

                    //Send the message to the connected TcpServer.
                    stream.Write(data, 0, data.Length);

                    int size = (stream.ReadByte() << 8) + stream.ReadByte();
                    Debug.WriteLine("ServerPinger Sent: " + data.ToString());

                    //Buffer to store the response bytes.
                    data = new byte[size];

                    //Read the first batch of the TcpServer response bytes
                    int bytes = stream.Read(data, 0, data.Length);

                    using (MemoryStream ms = new MemoryStream(data))
                    {
                        using (StreamReader reader = new StreamReader(ms))
                        {
                            pingData.Online = (int)byte.Parse(reader.ReadLine());
                            pingData.MaxOnline = (int)byte.Parse(reader.ReadLine());
                            pingData.Description = reader.ReadLine();
                        }
                    }
                    client.Close();
                }
            }
            catch (Exception e)
            {
                error = e.Message;
                pingData.Error = true;
                Debug.WriteLine(e.ToString());
                if (e is SocketException)
                {
                    //Provide some better error messages
                    int id = (e as SocketException).ErrorCode;
                    if (id == 10061) //No connection could be made because the target machine actively refused it
                        error = "Offline (Target Online, Server Not Accessible)";
                    else if (id == 10060) //A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
                        error = "Offline (Connection Timeout)";
                }
            }
            finally
            {
                if (client != null)
                    client.Close();
            }
            return pingData;
        }

Usage Example

 private void PingServer(ServerPinger pinger)
 {
     string error = "";
     ServerPingData ping = pinger.PingServer(Data.IP, Data.Port, out error);
     Ping = ping;
     //If no error
     if (error == string.Empty)
     {
         //Set the controls with the recieved data
         Stats.Text = ping.Online + "/" + ping.MaxOnline;
         Stats.Left = (ClientWidth - (int)Manager.Skin.Fonts["Default14"].Resource.MeasureString(Stats.Text).X) - 4 - 32;
         Motd.Text = ping.Description;
     }
     else
     {
         Motd.Text = error;
         Motd.TextColor = Color.Gold;
     }
 }
ServerPinger