2016-05-22 4 views
0

Ich habe ein Stück Code, der eine Liste der NIST-Server pingt und die Zeit, die sie haben, abruft. Dies ist der ursprüngliche Code, das funktioniert perfekt:Probleme mit Strings, Arrays und IP-Adressen haben (VB.NET)

'Server IP addresses from 
'http://tf.nist.gov/tf-cgi/servers.cgi - current as of 05/20/2016 
Private Shared Servers() As String = { 
     "129.6.15.28" _ 
    , "129.6.15.29" _ 
    , "129.6.15.30" _ 
    , "98.175.203.200" _ 
    , "66.199.22.67" _ 
    , "64.113.32.5" _ 
    , "198.111.152.100" _ 
    , "216.229.0.179" _ 
    , "24.56.178.140" _ 
    , "132.163.4.101" _ 
    , "132.163.4.102" _ 
    , "132.163.4.103" _ 
    , "128.138.140.44" _ 
    , "128.138.141.172" _ 
    , "198.60.73.8" _ 
    , "131.107.13.100" _ 
    , "216.228.192.69" 
} 

Public Shared LastHost As String = "" 
Public Shared LastSysTime As DateTime 

Public Shared Function GetTime() As DateTime 
    'Returns UTC/GMT using an NIST server if possible, 
    ' degrading to simply returning the system clock 

    'If we are successful in getting NIST time, then 
    ' LastHost indicates which server was used and 
    ' LastSysTime contains the system time of the call 
    ' If LastSysTime is not within 15 seconds of NIST time, 
    ' the system clock may need to be reset 
    ' If LastHost is "", time is equal to system clock 

    Dim host As String 
    Dim result As DateTime 

    LastHost = "" 
    For Each host In Servers 
     Try 
      If My.Computer.Network.Ping(host) Then 
       LastHost = host 
       result = GetNISTTime(host) 
      End If 
     Catch ex As Exception 
      MsgBox("There was an error retrieving the internet time. Please try again.", MsgBoxStyle.Exclamation, "Sync Error") 
     End Try 
    Next 

    If LastHost = "" Then 
     'No server in list was successful so use system time 
     result = DateTime.UtcNow() 
    End If 

    Return result 
End Function 

aber ich versuche, es zu ändern, so kann ich eine Liste von NIST-Server IP-Adressen in einem My.Settings oder My.Resources stattdessen Datei gespeichert verwenden (so Ich kann die I Adressen leicht ändern). Also gehen Sie einfach jede Zeile genau wie oben beschrieben. Außer dem folgenden Code funktioniert nicht wie der obige Code; es gibt nur etwas entlang der Linien von „System.Strings []“:

'Server IP addresses from 
'http://tf.nist.gov/tf-cgi/servers.cgi - current as of 05/20/2016 
'Declares stting that loads server IP addresses 
Public Shared VanServ As String = My.Resources.VanillaServerIPList 
Public Shared Serv As String = My.Settings.ServerIPList 
Public Shared Servers() As String 


Public Shared LastHost As String = "" 
Public Shared LastSysTime As DateTime 

Public Shared Function GetTime() As DateTime 
    'Returns UTC/GMT using an NIST server if possible, 
    ' degrading to simply returning the system clock 

    'If we are successful in getting NIST time, then 
    ' LastHost indicates which server was used and 
    ' LastSysTime contains the system time of the call 
    ' If LastSysTime is not within 15 seconds of NIST time, 
    ' the system clock may need to be reset 
    ' If LastHost is "", time is equal to system clock 
    Servers = Serv.Split(vbCrLf) 

    Dim host As String 
    Dim result As DateTime 

    LastHost = "" 
    For Each host In Servers 
     Try 
      If My.Computer.Network.Ping(host) Then 
       LastHost = host 
       result = GetNISTTime(host) 
      End If 
     Catch ex As Exception 
      MsgBox("There was an error retrieving the internet time. Please try again.", MsgBoxStyle.Exclamation, "Sync Error") 
     End Try 
    Next 

    If LastHost = "" Then 
     'No server in list was successful so use system time 
     result = DateTime.UtcNow() 
    End If 

    Return result 
End Function 

Die My.Resources.VanillaServerIPList und My.Settings.ServerIPList wie folgt aussehen (und bevor Sie sagen, zu überprüfen, ob die Listen null sind , beide Listen in der Tat haben die folgenden Werte enthalten):

129.6.15.28

129.6.15.29

129.6.15.30

98.175.20 3,200

66.199.22.67

64.113.32.5

198.111.152.100

216.229.0.179

... und so weiter; jeder IP ist auf einer neuen Zeile

+0

die Programmiersprache sind Sie mit? besser zu den Tags hinzufügen ... – alfasin

+0

Oh mein Schlechter. Ich benutze VB.NET –

+0

Sind Sie sicher, dass es eine Zeichenfolge "System.Strings []" zurückgibt? Es ist ein DateTime-Objekt, keine Zeichenfolge. Sieht wie der Standard ToString() in einer Liste von Zeichenfolgen aus. Um dieses Programm zu debuggen, drucken Sie jeden String innerhalb der Schleife, um zu sehen, was er tatsächlich verwendet. Wenn es immer noch nicht funktioniert, können Sie den viel einfacheren Code ohne Netzwerkcode posten. Es wird leichter für die Leute zu verstehen sein. – user1318499

Antwort

0

Hier ist, was ich schließlich auf gelandet:

'Variables 
'------------------------------------------------------------------------------- 
Private Shared VanServArray As Array 'Used to store "vanilla" list of NIST server IPs stored in My.Settings.VanillaServerIPList 
Private Shared ServArray As Array 'Used to store list of NIST server IPs stored in My.Settings.ServerIPList 
Private Shared Result As DateTime 'Stores DateTime retrieved from NIST Server 

Public Shared LastHost As String = "" 
Public Shared LastSysTime As DateTime 'Stores last available system DateTime 

'Functions 
'------------------------------------------------------------------------------- 
Public Shared Function GetTime() As DateTime 
    'Returns UTC/GMT using an NIST server if possible, 
    ' degrading to simply returning the system clock 

    If My.Settings.ServerIPList Is Nothing Then 
     'If the user modifiable list of NIST server IP addresses IS null (null = TRUE) then 
     'Put pre-stored, read-only "vanilla" StringsCollection of IP Addresses into the Array 
     ServArray = My.Settings.VanillaServerIPList.Cast(Of String)().ToArray 
    Else 
     'If the user modifiable list of NIST server IP addresses IS NOT null (null = FALSE) then 
     'Put the user modifiable StringsCollection of IP Addresses into an Array 
     ServArray = My.Settings.ServerIPList.Cast(Of String)().ToArray 
    End If 

    'Assign null value to LastHost 
    LastHost = "" 

    'Runs the "If Then" logic that pings all IPs in the array 
    'Ping each IP and try to retrieve results 
    For Each IP As String In ServArray 
     Try 
      'Shows each IP in Debug console 
      Debug.WriteLine(IP, "IP Addresses") 

      If My.Computer.Network.Ping(IP) Then 
       LastHost = IP 
       Result = GetNISTTime(IP) 
      End If 
     Catch ex As Exception 
      'Return "Sync Error 0x01" 
      MsgBox("There was a sync error while retrieving the updated internet time. Please try again.", MsgBoxStyle.Critical, "Sync Error 0x01") 
      Debug.WriteLine("There was a sync error while retrieving the updated internet time. Please try again.", "Sync Error 0x01") 
     End Try 
    Next 

    'No server in list was successful so use system time 
    If LastHost = "" Then 
     'Return "Sync Error 0x02" 
     MsgBox("No server in list returned the time. The sytem time has not been updated.", MsgBoxStyle.Critical, "Sync Error 0x02") 
     Debug.WriteLine("No server in list returned the time. The sytem time has not been updated.", "Sync Error 0x02") 
     Result = DateTime.UtcNow() 
    End If 

    Return Result 

End Function