Get external IP address over remoting in C#

前端 未结 12 1696
夕颜
夕颜 2020-12-05 05:16

I need to find out the external IP of the computer a C# application is running on.

In the application I have a connection (via .NET remoting) to a

12条回答
  •  时光说笑
    2020-12-05 05:53

    Well, assuming you have a System.Net.Sockets.TcpClient connected to your client, you can (on the server) use client.Client.RemoteEndPoint. This will give you a System.Net.EndPoint pointing to the client; that should contain an instance of the System.Net.IPEndPoint subclass, though I'm not sure about the conditions for that. After casting to that, you can check it's Address property to get the client's address.

    In short, we have

    using (System.Net.Sockets.TcpClient client = whatever) {
        System.Net.EndPoint ep = client.Client.RemoteEndPoint;
        System.Net.IPEndPoint ip = (System.Net.IPEndPoint)ep;
        DoSomethingWith(ip.Address);
    }
    

    Good luck.

提交回复
热议问题