2008-09-27 12 views
26

में ट्रेस रूट और पिंग क्या किसी के पास लक्ष्य कंप्यूटर पर पिंग और ट्रैसरआउट करने के लिए सी # कोड आसान है? मैं एक शुद्ध कोड समाधान की तलाश में हूं, न कि मैं अब क्या कर रहा हूं, जो ping.exe और tracert.exe प्रोग्राम का आविष्कार कर रहा है और आउटपुट को पार्स कर रहा है। मुझे कुछ और मजबूत चाहिए।सी #

उत्तर

0

मैं System.Net नाम स्थान

7

पिंग भाग के लिए में उनके लिए तलाश शुरू होता है, MSDN पर Ping class पर एक नज़र डालें।

+0

निम्नलिखित कोड उदाहरण पिंग क्लास को सिंक्रनाइज़ेशन का उपयोग करके प्रदर्शित करता है। पिंग क्लास देखें। – Kiquenet

2

पर एक नजर डालें - आप भुगतान करने के लिए खर्च कर सकते हैं, तो

http://www.codeproject.com/KB/IP/Tracert-ping.aspx

, डार्ट नेटवर्किंग विजेट काफ़ी अच्छा कर रहे हैं:

http://www.dart.com/ctl_tracenet.aspx

वहाँ एक और freebie कार्यान्वयन यहाँ है:

http://www.digigrupp.com/ping/ और यहां http://www.mltek.co.uk/traceclass.aspx

0

यह एक का उपयोग किए बिना पिंग समस्या का हल:

हालांकि, एक त्वरित खोज दो ओपन-सोर्स प्रयास करता है, सी # में पहली C++ दूसरे का पता चलता है तृतीय पक्ष

http://www.aspnettutorials.com/tutorials/network/net-ping-aspnet2-csharp.aspx

5

यहां ट्रेसआउट http://coding.infoconex.com/post/2009/01/C-Traceroute-using-net-framework.aspx

और अन्य को पिंग को कार्यान्वित करने का तरीका बताया गया है। http://coding.infoconex.com/post/2009/01/C-Ping-and-Traceroute.aspx

उपर्युक्त विंडोज़ पिंग और ट्रैसरआउट के समान व्यवहार को दोहराता है और इसे लागू करने के लिए बहुत आसान है।

+0

लिंक आज मृत हैं (404)। – Zartag

55

यह देखते हुए कि मुझे आज ट्रेस रूट क्लास लिखना पड़ा था, मुझे लगा कि मैं स्रोत कोड भी साझा कर सकता हूं।

using System.Collections.Generic; 
using System.Net.NetworkInformation; 
using System.Text; 
using System.Net; 

namespace Answer 
{ 
    public class TraceRoute 
    { 
    private const string Data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 

    public static IEnumerable<IPAddress> GetTraceRoute(string hostNameOrAddress) 
    { 
     return GetTraceRoute(hostNameOrAddress, 1); 
    } 
    private static IEnumerable<IPAddress> GetTraceRoute(string hostNameOrAddress, int ttl) 
    { 
     Ping pinger = new Ping(); 
     PingOptions pingerOptions = new PingOptions(ttl, true); 
     int timeout = 10000; 
     byte[] buffer = Encoding.ASCII.GetBytes(Data); 
     PingReply reply = default(PingReply); 

     reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions); 

     List<IPAddress> result = new List<IPAddress>(); 
     if (reply.Status == IPStatus.Success) 
     { 
     result.Add(reply.Address); 
     } 
     else if (reply.Status == IPStatus.TtlExpired || reply.Status == IPStatus.TimedOut) 
     { 
     //add the currently returned address if an address was found with this TTL 
     if (reply.Status == IPStatus.TtlExpired) result.Add(reply.Address); 
     //recurse to get the next address... 
     IEnumerable<IPAddress> tempResult = default(IEnumerable<IPAddress>); 
     tempResult = GetTraceRoute(hostNameOrAddress, ttl + 1); 
     result.AddRange(tempResult); 
     } 
     else 
     { 
     //failure 
     } 

     return result; 
    } 
    } 
} 

और किसी को भी चाहता है कि के लिए एक वीबी संस्करण/जरूरत है यह

Public Class TraceRoute 
    Private Const Data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 

    Public Shared Function GetTraceRoute(ByVal hostNameOrAddress As String) As IEnumerable(Of IPAddress) 
     Return GetTraceRoute(hostNameOrAddress, 1) 
    End Function 
    Private Shared Function GetTraceRoute(ByVal hostNameOrAddress As String, ByVal ttl As Integer) As IEnumerable(Of IPAddress) 
     Dim pinger As Ping = New Ping 
     Dim pingerOptions As PingOptions = New PingOptions(ttl, True) 
     Dim timeout As Integer = 10000 
     Dim buffer() As Byte = Encoding.ASCII.GetBytes(Data) 
     Dim reply As PingReply 

     reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions) 

     Dim result As List(Of IPAddress) = New List(Of IPAddress) 
     If reply.Status = IPStatus.Success Then 
      result.Add(reply.Address) 
     ElseIf reply.Status = IPStatus.TtlExpired Then 
      'add the currently returned address 
      result.Add(reply.Address) 
      'recurse to get the next address... 
      Dim tempResult As IEnumerable(Of IPAddress) 
      tempResult = GetTraceRoute(hostNameOrAddress, ttl + 1) 
      result.AddRange(tempResult) 
     Else 
      'failure 
     End If 

     Return result 
    End Function 
End Class 
+2

कूल समाधान। मामूली बिंदु लेकिन चूंकि आपने एक आईनेमरेबल <> वापसी का उपयोग किया है, इसलिए आप सूची को पॉप्युलेट करने के बजाय उपज रिटर्न करने पर विचार कर सकते हैं। – Jeff

+1

यदि कोई इस पर ठोकर खाता है, तो इसमें कुछ समस्याएं हैं, जिनमें शामिल हैं कि इसमें कभी भी वापस आने और हमेशा के लिए स्पिन करने की क्षमता नहीं है। कृपया https://stackoverflow.com/a/45565253/184746 देखें – caesay

0

ऊपर Scotts कोड जवाब देने के लिए बजे सुधार के रूप में, मैंने पाया कि उनके समाधान करता है, तो मार्ग में कुछ भी नहीं में बंद tapers काम नहीं करता है गंतव्य तक पहुंचने से पहले - यह कभी वापस नहीं आता है। कम से कम आंशिक मार्ग के साथ एक बेहतर समाधान यह हो सकता है (जिसे मैंने परीक्षण किया है और यह अच्छी तरह से काम करता है)। आप लूप के लिए '20' को कुछ बड़े या छोटे से बदल सकते हैं या यह पता लगाने की कोशिश कर सकते हैं कि यदि आप पुनरावृत्तियों की संख्या को किसी अन्य तरीके से नियंत्रित करना चाहते हैं तो यह बहुत लंबा समय ले रहा है। मूल कोड के लिए स्कॉट को पूर्ण क्रेडिट - धन्यवाद।

using System.Collections.Generic; 
    using System.Net.NetworkInformation; 
    using System.Text; 
    using System.Net; 

    ... 

    public static void TraceRoute(string hostNameOrAddress) 
    { 
     for (int i = 1; i < 20; i++) 
     { 
      IPAddress ip = GetTraceRoute(hostNameOrAddress, i); 
      if(ip == null) 
      { 
       break; 
      } 
      Console.WriteLine(ip.ToString()); 
     } 
    } 

    private static IPAddress GetTraceRoute(string hostNameOrAddress, int ttl) 
    { 
     const string Data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 
     Ping pinger = new Ping(); 
     PingOptions pingerOptions = new PingOptions(ttl, true); 
     int timeout = 10000; 
     byte[] buffer = Encoding.ASCII.GetBytes(Data); 
     PingReply reply = default(PingReply); 

     reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions); 

     List<IPAddress> result = new List<IPAddress>(); 
     if (reply.Status == IPStatus.Success || reply.Status == IPStatus.TtlExpired) 
     { 
      return reply.Address; 
     } 
     else 
     { 
      return null; 
     } 
    } 
2

क्या इस प्रकार बहुत बेहतर कोई tracert की सी # कार्यान्वयन की तुलना में अब तक अन्य उत्तर में मौजूद है। कि अन्य उत्तर में मौजूद हैं

public static IEnumerable<IPAddress> GetTraceRoute(string hostname) 
{ 
    // following are the defaults for the "traceroute" command in unix. 
    const int timeout = 10000; 
    const int maxTTL = 30; 
    const int bufferSize = 32; 

    byte[] buffer = new byte[bufferSize]; 
    new Random().NextBytes(buffer); 
    Ping pinger = new Ping(); 

    for (int ttl = 1; ttl <= maxTTL; ttl++) 
    { 
     PingOptions options = new PingOptions(ttl, true); 
     PingReply reply = pinger.Send(hostname, timeout, buffer, options); 

     if (reply.Status == IPStatus.Success) 
     { 
      // Success means the tracert has completed 
      yield return reply.Address; 
      break; 
     } 
     if (reply.Status == IPStatus.TtlExpired) 
     { 
      // TtlExpired means we've found an address, but there are more addresses 
      yield return reply.Address; 
      continue; 
     } 
     if (reply.Status == IPStatus.TimedOut) 
     { 
      // TimedOut means this ttl is no good, we should continue searching 
      continue; 
     } 

     // if we reach here, it's a status we don't recognize and we should exit. 
     break; 
    } 
} 

नुकसान यहाँ तय शामिल हैं:

  • यह आलसी है। पूर्व: यह उचित रूप से समेकित/एक पुनरावर्तक का उपयोग करता है इसलिए पूरे पेड़ की गणना करने की आवश्यकता नहीं है, आप किसी भी बिंदु पर अपने स्वयं के उपभोग करने वाले पाश से बाहर निकल सकते हैं।
  • maxTTL लागू किया गया ताकि फ़ंक्शन हमेशा के लिए स्पिन न हो।
  • bufferSize विकल्प जो अन्य ट्रैकर्स कार्यान्वयन के साथ संगत है।
  • यह बहुत संक्षिप्त और साफ है। यह एक ही विधि में निहित है और यहां अन्य विकल्पों की तुलना में काफी छोटा है।