본문 바로가기

C#/Network

C# Ping 테스트, 해당 IP 장치 이름 가져오기

C# Ping 테스트, 해당 IP 장치 이름 가져오기

 

 

public class PingTest
{
    public static double Do(string ip, int timeout = 5000)
    {
        try
        {
            var pingSender = new Ping();
            var options = new PingOptions();
 
            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;
 
            // Create a buffer of 32 bytes of data to be transmitted.
            const string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            var buffer = Encoding.ASCII.GetBytes(data);
            
            TimeSpan checkTime;
            DateTime checkTime1 = DateTime.Now;
            DateTime checkTime2;
            var reply = pingSender.Send(ip, timeout, buffer, options);
            if (reply != null)
            {
                if (reply.Status == IPStatus.Success)
                {
                    checkTime2 = DateTime.Now;
                    checkTime = (checkTime2 - checkTime1);
                    return checkTime.TotalMilliseconds;
                }
                else
                {
 
                }
            }
        }
        catch(Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
 
        return -1d;
    }
 
    public static string GetDeviceName(string ip)
    {
        string result = "";
 
        try
        {
            result = Dns.GetHostEntry(IPAddress.Parse(ip)).HostName.ToString();
        }
        catch(Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
 
        return result;
    }
}

'C# > Network' 카테고리의 다른 글

C# UDP  (0) 2020.08.13
C# WOL 컴퓨터 원격 온오프  (0) 2020.08.07
C# 네트워크 상 맥어드레스 가져오기  (0) 2020.08.07
C# http post header body  (0) 2020.08.07
C# HttpClient RestAPI Post  (0) 2020.08.05