[英]Having Trouble With Strings, Arrays, and IP Addresses (VB.NET)
我有一段代碼可以ping NIST服務器列表並檢索它們的時間。 這是完美的原始代碼:
'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
但是,我正在嘗試對其進行修改,以便可以使用保存在My.Settings或My.Resources文件中的NIST服務器IP地址列表(以便可以輕松修改I地址)。 所以基本上就像上面的工作代碼一樣,沿着每一行走下去。 除了下面的代碼不能像上面的代碼一樣工作; 它只是返回類似於“ 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
My.Resources.VanillaServerIPList和My.Settings.ServerIPList如下所示(在您說要檢查列表是否為空之前,兩個列表實際上都包含以下值):
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
...等等; 每個IP都在新的一行
這是我終於登陸的東西:
'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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.