Getting remote ip from TcpClient object

  • Thread starter Thread starter Ethan
  • Start date Start date
E

Ethan

Hi,

In quite a fix here. Hope someone can help ASAP. I need
to get the IP address of the client that connects when I
use TcpListener.AcceptTcpConnections. I know I can get
the information using the socket class but I really need
to know how to get this information using the TcpClient
class. I tried writing a class from TcpCLient but then
found out that I can not cast down the inheritance
hierarchy, only up. Any help is greatly
appreciated !!!!! Thanks in advance!!!

Regards,

Ethan
 
Hi Ethan,
In quite a fix here. Hope someone can help ASAP. I need
to get the IP address of the client that connects when I
use TcpListener.AcceptTcpConnections. I know I can get
the information using the socket class but I really need
to know how to get this information using the TcpClient
class. I tried writing a class from TcpCLient but then
found out that I can not cast down the inheritance
hierarchy, only up. Any help is greatly
appreciated !!!!! Thanks in advance!!!

You have to use TcpListener.AcceptSocket to be able to
obtain the peer's address. It isn't that hard to deal
with the Socket class.

--
If you promise, that you will use the Socket class in future,
here is an untested hotfix using reflection on non public
members, which is a no-no-no!!!!! Don't do that at home!

using System.Reflection;

static Socket GetUnderlyingSocket(TcpClient c) {
return (Socket) c.GetType.InvokeMember(
"Client",
BindingFlags.Instance | BindingFlags.GetProperty |
BindingFlags.NonPublic,
null,
c,
null
);
}
 

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

Back
Top