Getting the connected IP from TcpListener.AcceptTcpClient

Getting the connected IP from TcpListener.AcceptTcpClient

Post by Benny Raym » Fri, 21 Oct 2005 04:57:55


Is it possible to get the ip address of the pc connecting to my
TcpListener? Here's how my code works so far:


listener = new TcpListener(_myIP, _port);
listener.Start();
// block until we get a connection
TcpClient client = listener.AcceptTcpClient();

// at this point I need to store the IP address for
// future ref... but how!?


Any help would be great - thanks!
 
 
 

Getting the connected IP from TcpListener.AcceptTcpClient

Post by Nicholas P » Fri, 21 Oct 2005 05:11:46

Benny,

In .NET 1.1 and before, if you get the TcpClient, you didn't have access
to the underlying socket. Because of this, you would have to call the
AcceptSocket method to get a socket, and then get the endpoint from there.

In .NET 2.0, the TcpClient class exposes the Client property, which will
give you access to the underlying Socket.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- XXXX@XXXXX.COM