【问题标题】:Having Trouble With Strings, Arrays, and IP Addresses (VB.NET)字符串、数组和 IP 地址有问题 (VB.NET)
【发布时间】:2016-09-19 02:04:33
【问题描述】:

我有一段代码可以 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都换行

【问题讨论】:

  • 您使用哪种编程语言?最好将其添加到标签中...
  • 哦,我的错。我正在使用 VB.NET
  • 你确定它返回一个字符串“System.Strings[]”吗?它是一个 DateTime 对象,而不是一个字符串。看起来像字符串列表上的默认 ToString() 。要调试此程序,请在循环中打印每个字符串以查看它实际使用的内容。如果它仍然无法正常工作,您可以在没有任何网络代码的情况下发布更简单的代码。人们会更容易理解。
  • 您是否尝试过输出 Servers() 数组以确保您的拆分按预期工作?
  • 您可以将 My.Settings 设置类型设置为 System.Collections.Specialized.StringCollection 而不是 System.String。这样您就可以像第一个代码一样将设置加载到数组中,而不必担心拆分问题。要将其放入数组中,您只需:My.Settings.Servers.CopyTo(YourArray, 0) - 其中 Servers 是设置的名称,YourArray 是您要放入的数组。跨度>

标签: arrays vb.net string list ip


【解决方案1】:

这是我最终登陆的:

'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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多