Click here to Skip to main content
Click here to Skip to main content

IP and Port from TcpClient

, 3 Oct 2006 CPOL
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.

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.

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

License

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

Share

About the Author

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

Comments and Discussions

 
Generalasp.net tcpclient Pinmemberdragon4125-Jul-07 19:07 
QuestionSending Text Pinmemberhariram2814-Oct-06 6:27 
AnswerRe: Sending Text PinmemberPolymorpher16-Oct-06 8:39 
GeneralRemoting Pinmemberhariram2811-Oct-06 6:47 
GeneralRe: Remoting PinmemberPolymorpher11-Oct-06 7:02 
QuestionRe: Remoting Pinmemberhariram2811-Oct-06 7:23 
AnswerRe: Remoting PinmemberPolymorpher11-Oct-06 7:39 
GeneralAlternative Method PinmemberTom Spink9-Oct-06 15:40 
GeneralRe: Alternative Method PinmemberPolymorpher9-Oct-06 20:16 
GeneralRe: Alternative Method PinmemberManjunathabe0114-Feb-09 22:06 
AnswerRe: Alternative Method PinmemberTom Spink5-Feb-09 1:27 
Hi,
 
/// C#
TcpClient cli;
IPEndPoint ep = (IPEndPoint)cli.Client.RemoteEndPoint;
///
 
The 'ep' variable now contains the remote endpoint of the TCP client, from which you can derive the IP address by using the 'Address' property, and the port from the 'Port' property:
 
///
IPAddress addr = ep.Address;
int port = ep.Port;
///
 
Hope this helps.
 
-- Tom Spink, Über Geek.

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 3 Oct 2006
Article Copyright 2006 by Polymorpher
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid