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
);
}
--
bye
Rob