hellen
hellen

Reputation: 15

Retrieve ip address through Lan

How I can retrieve the IP address for a client when this client is connected to the server through Lan network and show it as a string in textbox? The server code:

    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

End Class

Upvotes: 0

Views: 197

Answers (1)

Richard
Richard

Reputation: 109200

The IP address of the other party is available from the Socket from its RemoteEndPoint property (if it is connected).

Use Serialize to convert to a SocketAddress which overrides ToString() to get the address in a readable format.

Upvotes: 1

Related Questions