类型为“System.Net.Sockets.SocketException”LAN网络的未处理异常

时间:2015-05-15 07:11:06

标签: vb.net

我创建了一个TreeView,显示我的局域网网络上的所有用户,一切正常。

  • 当我点击任何用户时,它显示IP和PCname没有任何问题,但是
  • 当我点击主网络“HomeGroup”时,我收到以下信息

System.dll

中发生了'System.Net.Sockets.SocketException'类型的未处理异常

并突出显示以下一行:

使用System.Net.Dns.GetHostByName(CompName)

<h:form>
    <h:panelGrid columns="2" id="matchGrid" cellpadding="5">                   
        <h:outputLabel for="pwd1" value="Password 1: *" />
        <p:password id="pwd1" value="password"  label="Password" required="true" />
        <p:message for="pwd1"/>


        <h:outputLabel for="pwd2" value="Password 2: *" />
        <p:password id="pwd2" value="confirmPassword" onkeyup ="checkPasswordMatch();" label="Confirm Password" required="true" />
        <p:message for="pwd2"/>
    </h:panelGrid>

    <p:commandButton update="matchGrid" value="Save" />
</h:form>

更新

Function GetIPAddress(ByVal CompName As String) As String
    Dim oAddr As System.Net.IPAddress
    Dim sAddr As String
    Try
        With System.Net.Dns.GetHostByName(CompName)
            oAddr = New System.Net.IPAddress(.AddressList(0).Address)
            sAddr = oAddr.ToString
        End With
        GetIPAddress = sAddr
    Finally
    End Try

End Function

1 个答案:

答案 0 :(得分:2)

以下是VB.NET Forms应用程序中的一个示例。

我在代码中添加了评论,但如果您有更多问题,请随时提出:

Public Class Form4

    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'an array of predefined users, for testing purposes. 
        'you can use your LAN users instead.
        Dim USR_Array() As String = {"user1", "user2", "user3"}

        'first add a homegroup node, you can add yours.
        Dim mainNode As New TreeNode()
        'name of the node will hold the node type, GROUP in this case
        mainNode.Name = "GROUP"
        mainNode.Text = "HomeGroup"
        Me.TreeView1.Nodes.Add(mainNode)

        'loop through the user list and add them as well
        For Each USR In USR_Array
            Dim usrNode As New TreeNode()
            'the name of those nodes will be USER to represent
            'the type of those nodes
            usrNode.Name = "USER"
            usrNode.Text = USR
            mainNode.Nodes.Add(usrNode)
        Next USR
    End Sub
    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
        'after select, check if it was performed by a mouse click
        If e.Action = TreeViewAction.ByMouse Then
            Select Case e.Node.Name
                Case "GROUP"
                    MsgBox("Group node clicked")
                Case "USER"
                    MsgBox("User node clicked")
                    'call the GetIPAddress function here
            End Select
        End If
    End Sub
End Class