Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get external IP address over remoting in C#

Tags:

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 server. Is there a good way to get the address of the client on the server side?

(I have edited the question, to be a little more clear. I'm apologize to all kind people who did their best to respond to the question, when I perhaps was a little too vague)

Solution:
I found a way that worked great for me. By implementing a custom IServerChannelSinkProvider and IServerChannelSink where I have access to CommonTransportKeys.IPAddress, it's easy to add the client ip on the CallContext.

public ServerProcessing ProcessMessage(IServerChannelSinkStack sinkStack,      IMessage requestmessage, ITransportHeaders requestHeaders,      System.IO.Stream requestStream, out IMessage responseMessage,      out ITransportHeaders responseHeaders, out System.IO.Stream responseStream) {     try     {         // Get the IP address and add it to the call context.         IPAddress ipAddr = (IPAddress)requestHeaders[CommonTransportKeys.IPAddress];         CallContext.SetData("ClientIP", ipAddr);     }     catch (Exception)     {     }      sinkStack.Push(this, null);     ServerProcessing srvProc = _NextSink.ProcessMessage(sinkStack, requestmessage, requestHeaders,         requestStream, out responseMessage, out responseHeaders, out responseStream);      return srvProc; } 

And then later (when I get a request from a client) just get the IP from the CallContext like this.

public string GetClientIP() {     // Get the client IP from the call context.     object data = CallContext.GetData("ClientIP");      // If the data is null or not a string, then return an empty string.     if (data == null || !(data is IPAddress))         return string.Empty;      // Return the data as a string.     return ((IPAddress)data).ToString(); } 

I can now send the IP back to the client.

like image 575
Patrik Svensson Avatar asked Sep 15 '08 20:09

Patrik Svensson


People also ask

How do I find an external IP address programmatically?

If you want to programmatically capture the external IP address locally, this code will do the job. "http://whatismyip.com/automation/n09230945.asp")); Response. Write("<h2>Your External IP Address is: " + externalIp + "</h2><br />");

How do I find internal and external IP address?

Here are the instructions for windows:Type cmd and press enter. In this new windows type ipconfig and press enter. You will see a bit more information than you may want what your looking for is IPv4 Address. The number across from that is your local IP address.


2 Answers

This is one of those questions where you have to look deeper and maybe rethink the original problem; in this case, "Why do you need an external IP address?"

The issue is that the computer may not have an external IP address. For example, my laptop has an internal IP address (192.168.x.y) assigned by the router. The router itself has an internal IP address, but its "external" IP address is also internal. It's only used to communicate with the DSL modem, which actually has the external, internet-facing IP address.

So the real question becomes, "How do I get the Internet-facing IP address of a device 2 hops away?" And the answer is generally, you don't; at least not without using a service such as whatismyip.com that you have already dismissed, or doing a really massive hack involving hardcoding the DSL modem password into your application and querying the DSL modem and screen-scraping the admin page (and God help you if the modem is ever replaced).

EDIT: Now to apply this towards the refactored question, "How do I get the IP address of my client from a server .NET component?" Like whatismyip.com, the best the server will be able to do is give you the IP address of your internet-facing device, which is unlikely to be the actual IP address of the computer running the application. Going back to my laptop, if my Internet-facing IP was 75.75.75.75 and the LAN IP was 192.168.0.112, the server would only be able to see the 75.75.75.75 IP address. That will get it as far as my DSL modem. If your server wanted to make a separate connection back to my laptop, I would first need to configure the DSL modem and any routers inbetween it and my laptop to recognize incoming connections from your server and route them appropriately. There's a few ways to do this, but it's outside the scope of this topic.

If you are in fact trying to make a connection out from the server back to the client, rethink your design because you are delving into WTF territory (or at least, making your application that much harder to deploy).

like image 113
Nathan Strong Avatar answered Oct 02 '22 15:10

Nathan Strong


Dns.GetHostEntry(Dns.GetHostName()); will return an array of IP addresses. The first one should be the external IP, the rest will be the ones behind NAT.

So:

IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName()); string externalIP = IPHost.AddressList[0].ToString(); 

EDIT:

There are reports that this does not work for some people. It does for me, but perhaps depending on your network configuration, it may not work.

like image 45
FlySwat Avatar answered Oct 02 '22 16:10

FlySwat