2012-03-08 34 views
0

我嘗試ping基於IP地址和端口的服務器,使用Ping類, 我必須將IP地址轉換爲字節數組,怎麼了我在做什麼? 我從什麼地方轉換IP地址的字節C#數組

bool IsConnectedToInternet 
    { 
     get 
     { 
      Uri url = new Uri("www.abhisheksur.com"); 
      string pingurl = string.Format("{0}", url.Host); 
      string host = pingurl; 
      bool result = false; 
      Ping p = new Ping(); 
      try 
      { 

       PingReply reply = p.Send(host, 3000); 
       if (reply.Status == IPStatus.Success) 
        return true; 
      } 
      catch { } 
      return result; 
     } 
    } 

把這個方法我非得ping基於IP,而不是一個URL服務器。 謝謝。

+0

爲什麼你需要一個IP地址轉換成字節數組? – pfries 2012-03-08 16:24:42

+0

什麼問題?爲什麼大家都在降低速度? – idish 2012-03-08 16:25:00

+0

@killingswine因爲我沒有服務器的URL。我正在ping一個遊戲服務器。 – idish 2012-03-08 16:25:38

回答

1

難道你們就不能只是這樣做:

public static bool IsConnectedToInternet 
{ 
    get 
    { 
     using (var ping = new Ping()) 
     { 
      try 
      { 
       var reply = ping.Send("173.194.41.168", 3000); 
       return reply.Status == IPStatus.Success; 
      } 
      catch 
      { 
       return false; 
      } 
     } 
    } 
} 
1

雖然您的代碼段不需要它,這裏是一個問題的答案中的標題您的文章:

您可以使用System.Net.Dns.GetHostAddresses("www.abhisheksur.com")來獲取表示主機地址的IPAdresses對象數組。然後,您可以撥打GetAddressBytes()的個人IPAddress對象將其轉換爲字節數組。

+0

我不知道地址,這只是我從互聯網上獲取的一個例子。 – idish 2012-03-08 16:26:50

+0

@idish然後,您需要插入所需的地址字符串來代替硬編碼常量。 – dasblinkenlight 2012-03-08 16:28:16