Getting the connected IP from TcpListener.AcceptTcpClient

B

Benny Raymond

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!
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top