Imports System.Net
Imports System.Net.Sockets
Imports System.Reflection
Public Class winTCPIPUtils
Public Class AddressInfo
Private m_IP As String
Private m_Port As String
Public Sub New()
m_IP = "127.0.0.1"
m_Port = "999"
End Sub
Public Sub New(ByVal theIP As String, ByVal thePort As String)
m_IP = theIP
m_Port = thePort
End Sub
Public Property IP() As String
Get
Return m_IP
End Get
Set(ByVal Value As String)
m_IP = Value
End Set
End Property
Public Property Port() As String
Get
Return m_Port
End Get
Set(ByVal Value As String)
m_Port = Value
End Set
End Property
End Class
Public Shared Function AddressInfoFromTCPClient(ByVal theClient As TcpClient) As AddressInfo
Dim pi As PropertyInfo = theClient.GetStream.GetType.GetProperty("Socket", BindingFlags.NonPublic Or BindingFlags.Instance)
Dim theSocket As Socket = pi.GetValue(theClient.GetStream, Nothing)
Dim theEnd As EndPoint = theSocket.RemoteEndPoint
Dim theAddress() As String = theEnd.ToString.Split(":")
Dim theInfo As New AddressInfo(theAddress(0), theAddress(1))
Return theInfo
End Function
End Class
Tag : .NET, Win (Windows App), Class Library, VB.NET, C#, VS 2010 (.NET 4.x)