Click here to Skip to main content
13,291,510 members (64,843 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 3 Oct 2006

IP and Port from TcpClient

, 3 Oct 2006
Rate this:
Please Sign up or sign in to vote.
How to get the TCP/IP address of a TCPClient using VB.NET and Reflection.


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))

    Return theInfo
End Function

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 ;)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
PC Programmer/Analyst

You may also be interested in...

Comments and Discussions tcpclient Pin
dragon4125-Jul-07 20:07
memberdragon4125-Jul-07 20:07 
QuestionSending Text Pin
hariram2814-Oct-06 7:27
memberhariram2814-Oct-06 7:27 
AnswerRe: Sending Text Pin
Polymorpher16-Oct-06 9:39
memberPolymorpher16-Oct-06 9:39 
GeneralRemoting Pin
hariram2811-Oct-06 7:47
memberhariram2811-Oct-06 7:47 
Dear Sirs,
Could you help how me to resolve my problem in remoting in VB.NET

I like to inform you that I’m in between beginner and moderate level. I’m developing a application in VB.NET using remoting concept for my academic purpose. I need replica of a object which is created in a server application to be accessed in other remote client machine…
Kindly help me as I’m nearing my dealine…

I’ll explain you briefly through a sample code…

First I’ll run this application and initialize the object…

Application 1(server):

Class file :

Class x
Public y as integer

End class

VB file:

Function init()
Dim ab as x

End function

After running the application 1, then I’m running the client machine.

Application 2(Remote Client):

VB file:

Dim obj as remoteobj
dim x as integer


so that the value gets stored in the variable x.

GeneralRe: Remoting Pin
Polymorpher11-Oct-06 8:02
memberPolymorpher11-Oct-06 8:02 
QuestionRe: Remoting Pin
hariram2811-Oct-06 8:23
memberhariram2811-Oct-06 8:23 
AnswerRe: Remoting Pin
Polymorpher11-Oct-06 8:39
memberPolymorpher11-Oct-06 8:39 
GeneralAlternative Method Pin
Tom Spink9-Oct-06 16:40
memberTom Spink9-Oct-06 16:40 
GeneralRe: Alternative Method Pin
Polymorpher9-Oct-06 21:16
memberPolymorpher9-Oct-06 21:16 
GeneralRe: Alternative Method Pin
Manjunathabe0114-Feb-09 23:06
memberManjunathabe0114-Feb-09 23:06 
AnswerRe: Alternative Method Pin
Tom Spink5-Feb-09 2:27
memberTom Spink5-Feb-09 2:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 3 Oct 2006
Article Copyright 2006 by Polymorpher
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid