public string CheckConnection(IPingerAddress pingerAddress) { WebRequest request = WebRequest.Create(pingerAddress.GetEndPoint()); request.Method = "GET"; using (WebResponse response = request.GetResponse()) { try { int statusCode = (int)((HttpWebResponse)response).StatusCode; int?validCode = ((IPingerAdressWithValidation)pingerAddress)?.GetValidStatusCode(); if (!Equals(statusCode, validCode)) { throw new ConnectionFailedException(); } pingerAddress.SetLastState("Ok"); } catch (UriFormatException ex) { pingerAddress.SetLastState("Failed"); pingerAddress.SetMessage(ex.Message); } catch (ConnectionFailedException ex) { pingerAddress.SetLastState("Failed"); pingerAddress.SetMessage(ex.Message); } } return(pingerAddress.GetLastState()); }
public string CheckConnection(IPingerAddress pingerAddress) { var ipPoint = pingerAddress.GetEndPoint(); using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { try { sock.Connect(ipPoint); if (!sock.Connected) { throw new ConnectionFailedException(); } pingerAddress.SetLastState("Ok"); } catch (FormatException ex) { pingerAddress.SetLastState("Failed"); pingerAddress.SetMessage(ex.Message); } catch (ConnectionFailedException ex) { pingerAddress.SetLastState("Failed"); pingerAddress.SetMessage(ex.Message); } catch (SocketException ex) { pingerAddress.SetLastState("Failed"); pingerAddress.SetMessage(ex.Message); } } return(pingerAddress.GetLastState()); }
public string CheckConnection(IPingerAddress pingerAddress) { Ping pingSender = new Ping(); string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 20000; PingOptions options = new PingOptions(64, true); try { // Send the request. PingReply reply = pingSender.Send(pingerAddress.GetEndPoint(), timeout, buffer, options); if (reply?.Status == IPStatus.Success) { pingerAddress.SetLastState("Ok"); } else { pingerAddress.SetLastState("Failed"); pingerAddress.SetMessage(reply?.Status.ToString()); } } catch (PingException ex) { pingerAddress.SetLastState("Failed"); pingerAddress.SetMessage(ex.Message); } return(pingerAddress.GetLastState()); }