Click here to Skip to main content
15,889,403 members
Articles / Programming Languages / Visual Basic

IP and Port from TcpClient

Rate me:
Please Sign up or sign in to vote.
3.22/5 (4 votes)
3 Oct 2006CPOL 66.4K   1.3K   18   11
How to get the TCP/IP address of a TCPClient using VB.NET and Reflection.

Introduction

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.

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

License

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


Written By
Software Developer (Senior) www.ruskin.com
United States United States
PC Programmer/Analyst

Comments and Discussions

 
Generalasp.net tcpclient Pin
dragon4125-Jul-07 19:07
dragon4125-Jul-07 19:07 
QuestionSending Text Pin
hariram2814-Oct-06 6:27
hariram2814-Oct-06 6:27 
AnswerRe: Sending Text Pin
Polymorpher16-Oct-06 8:39
Polymorpher16-Oct-06 8:39 
GeneralRemoting Pin
hariram2811-Oct-06 6:47
hariram2811-Oct-06 6:47 
GeneralRe: Remoting Pin
Polymorpher11-Oct-06 7:02
Polymorpher11-Oct-06 7:02 
If the object is only editable on the server side...you could set up a chain something like this:

assume object name is 'x'

server->edit x
server->pass x to client1

client1->read x and do what it is suposed to
client1->(this stage is up to you...pass it or report a finish) Inform
server that we have received the data, or we could pass it to the
next client...there are up's and downs to this, a down would be that
a client that crashes could break the chain...probably best to have
the server send out packets to clients and wait to see if they get
them before sending them the next packet.

client2->if you choose to have the clients pass data to the next client...
it is now client2's turn to interact with the data...last client
will pass the data back to the server letting it know that the data
went all the way around the loop.

Depending on your setup...and if there are references to other objects created on the server in your packet; this could work. There are tones of ways to do this, but most of them depend on your setup, language, datatype, etc...

A posibly easyer solution could be to have your clients request the data from the server in a comma delimited string that the client could convert into an object once it was recieved...it really all depends on what you are trying to do.

Hope this was helpfull, let me know if I can help in any other way.

Pablo
Sometimes I think there's no reason to get out of bed . . . then I feel wet, and I realize there is.

QuestionRe: Remoting Pin
hariram2811-Oct-06 7:23
hariram2811-Oct-06 7:23 
AnswerRe: Remoting Pin
Polymorpher11-Oct-06 7:39
Polymorpher11-Oct-06 7:39 
GeneralAlternative Method Pin
Tom Spink9-Oct-06 15:40
Tom Spink9-Oct-06 15:40 
GeneralRe: Alternative Method Pin
Polymorpher9-Oct-06 20:16
Polymorpher9-Oct-06 20:16 
GeneralRe: Alternative Method Pin
Manjunathabe0114-Feb-09 22:06
Manjunathabe0114-Feb-09 22:06 
AnswerRe: Alternative Method Pin
Tom Spink5-Feb-09 1:27
Tom Spink5-Feb-09 1: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.