This article explains how to get the TCP/IP address of a
TCPClient using VB.NET and Reflection.
This is a fairly simple solution...You must use
PropertyInfo from Reflection to reference the private socket of your
TCPClient, and then pull the IP from the
RemoteEndPoint of your
Socket. I added a small class to make this a little cleaner and to return the address info as a separate IP address and port.
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))
Hopefully this will be helpful to someone...I have used it quite a bit for small chat applications to keep track of banned IP addresses etc., while still having the ease of using
TCPClient instead of sockets. Feel free to use this in your software as long as you do not hold me responsible for any damages ;)