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 Su开发者_高级运维b
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
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.
精彩评论