Exemplo n.º 1
0
        private PingResult PingHost(string host, int timeout)
        {
            PingOptions options = new PingOptions(DefaultTtl, DefaultFragmentation);

            byte[] buffer = Enumerable.Repeat(Convert.ToByte('&'), DefaultBufferSize).ToArray();

            using (Ping pinger = new Ping())
            {
                try
                {
                    PingReply reply = pinger.Send(
                        host,
                        timeout,
                        buffer,
                        options
                        );

                    if (reply.Status == IPStatus.Success)
                    {
                        return(PingResult.Succeeded(
                                   IPStatus.Success.ToString(),
                                   reply.Address.ToString(),
                                   reply.RoundtripTime
                                   ));
                    }

                    return(PingResult.Failed(
                               reply.Status.ToString(),
                               string.Empty,
                               -1
                               ));
                }
                catch (PingException exc)
                {
                    SocketException socketException = exc.InnerException as SocketException;

                    if (socketException != null)
                    {
                        return(PingResult.Failed(
                                   socketException.SocketErrorCode.ToString(),
                                   socketException.Message,
                                   -1
                                   ));
                    }

                    throw;
                }
                catch (Exception exc)
                {
                    return(PingResult.Failed(
                               IPStatus.Unknown.ToString(),
                               exc.Message,
                               -1
                               ));
                }
            }
        }
Exemplo n.º 2
0
        private PingResult PingPort(string hostName, int port, int timeout)
        {
            Stopwatch stopwatch = new Stopwatch();

            using (TcpClient tcpClient = new TcpClient())
            {
                stopwatch.Start();

                IAsyncResult asyncResult = tcpClient.BeginConnect(hostName, port, null, null);
                WaitHandle   waitHandle  = asyncResult.AsyncWaitHandle;

                try
                {
                    if (!waitHandle.WaitOne(timeout, false))
                    {
                        tcpClient.Close();
                        throw new SocketException(10060);                         // timeout error
                    }

                    tcpClient.EndConnect(asyncResult);
                }
                catch (SocketException exc)
                {
                    stopwatch.Stop();

                    string errorStatus = exc.SocketErrorCode.ToString();

                    return(PingResult.Failed(
                               errorStatus,
                               exc.Message,
                               -1
                               ));
                }
                finally
                {
                    waitHandle.Close();
                }

                stopwatch.Stop();

                string status    = SocketError.Success.ToString();
                string ipAddress = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();

                return(PingResult.Succeeded(
                           status,
                           ipAddress,
                           stopwatch.ElapsedMilliseconds
                           ));
            }
        }
Exemplo n.º 3
0
        private PingResult PingPort(string hostName, int port, int timeout)
        {
            Stopwatch stopwatch = new Stopwatch();

            using (UdpClient udpClient = new UdpClient())
            {
                udpClient.Client.ReceiveTimeout = timeout;
                stopwatch.Start();

                try
                {
                    udpClient.Connect(hostName, port);
                    IPEndPoint ipAddress = (IPEndPoint)udpClient.Client.RemoteEndPoint;

                    Byte[] sendBytes = Encoding.ASCII.GetBytes("?");
                    udpClient.Send(sendBytes, sendBytes.Length);
                    IPEndPoint remoteIpEndPoint = new IPEndPoint(ipAddress.Address, port);
                    udpClient.Receive(ref remoteIpEndPoint);

                    stopwatch.Stop();

                    string status = SocketError.Success.ToString();

                    return(PingResult.Succeeded(
                               status,
                               ipAddress.Address.ToString(),
                               stopwatch.ElapsedMilliseconds
                               ));
                }
                catch (SocketException exc)
                {
                    stopwatch.Stop();

                    string errorStatus = exc.SocketErrorCode.ToString();

                    return(PingResult.Failed(
                               errorStatus,
                               exc.Message,
                               -1
                               ));
                }
                finally
                {
                    udpClient.Close();
                }
            }
        }