通过Lan检索IP地址

时间:2011-05-18 08:31:16

标签: .net vb.net lan

当此客户端通过Lan网络连接到服务器并在文本框中将其显示为字符串时,如何检索客户端的IP地址? 服务器代码:

    Imports System.Net.Sockets

Imports System.Threading
Imports System.Windows.Forms
Imports System.IO

Public Class broadcast


Private thread As Thread
Private listener As New TcpListener(5234)
Private writers As New ArrayList
Private name As String



Public Sub New(ByVal name As String)
    MyBase.New()
    Me.name = name

End Sub

Public Sub start()
    listener.Start()
    thread = New Thread(AddressOf RunServer)
    thread.Start()
End Sub


Public Sub RunServer()
    Try
        While True
            Dim writer As New BinaryWriter(New NetworkStream(listener.AcceptSocket))
            writer.Write(name)

            writers.Add(writer)

        End While

    Catch exception As Exception
        'MessageBox.Show("Server application Closing")

    End Try
End Sub


Public Sub sendCommand(ByVal command As String)
    For i As Integer = 0 To writers.Count
        Try
            Dim writer As BinaryWriter
            writer = CType(writers.Item(i), BinaryWriter)
            writer.Write(command)
            writer.Flush()
        Catch inputputputexception As Exception
        End Try
    Next
End Sub

Public Sub stopAll()
    For i As Integer = 0 To writers.Count
        Try

            Dim writer As BinaryWriter
            writer = CType(writers.Item(0), BinaryWriter)
            writer.Close()
            writers.Remove(0)
        Catch inputputputexception As Exception
        End Try
    Next
    listener.Stop()



End Sub

结束班

1 个答案:

答案 0 :(得分:1)

另一方的IP地址可从Socket的RemoteEndPoint属性(如果已连接)获得。

使用Serialize转换为覆盖ToString()的SocketAddress,以便以可读的格式获取地址。