tags:

views:

10958

answers:

14

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.

A: 

Screenscrape http://whatismyip.com/

Aaron Maenpaa
Thanks for your reply, but I hardly think this is a standardized way of doing this in C#.
Patrik
As is mentioned in the discussion John Millikin links to: there is know way to know what up are externally known as without the help of an external server.
Aaron Maenpaa
@zacherates: Then what is DNS for?
Geoffrey Chetwood
A: 

You can basically parse the page returned by doing a WebRequest of http://whatismyipaddress.com

http://www.dreamincode.net/forums/showtopic24692.htm

Matt Michielsen
Linking to a forum post here is not recommended. You can summarize the content there here with a quote, and then provide a link.
FlySwat
+3  A: 

Better to just use http://www.whatismyip.com/automation/n09230945.asp it only outputs the IP just for the automated lookups.

If you want something that does not rely on someone else put up your own page http://www.unkwndesign.com/ip.php is just a quick script:

<?php
echo 'Your Public IP is: ' . $_SERVER['REMOTE_ADDR'];
?>

The only downside here is that it will only retrieve the external IP of the interface that was used to create the request.

Unkwntech
Anthony Mastrean
+5  A: 

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.

FlySwat
I ran this code and it gave me an interal IP. In fact, all three addresses IPHost.AddressList contained were 192.168.x.x
Corey
An external ping is the only true way to do this. Think before you down vote people.
mmattax
It does work for me, but I'm guessing that depending on your network configuration, it may or may not work.
FlySwat
Better than writing a bible. Thank you FlySwat
Junior Mayhé
A: 

The most reliable manner of doing this is checking a site like http://checkip.dyndns.org/ or similar, because until you actually go external to your network, you cannot find your external IP. However, hardcoding such a URL is asking for eventual failure. You may wish to only perform this check if the current IP looks like an RFC1918 private address (192.168.x.x being the most familiar of these.

Failing that, you can implement your own, similar, service sitting external to the firewall, so you will at least know if it's broken.

Pi
+1  A: 

Which one? Many computers (especially servers) can have more than one public IP, depending on which interface you're using.

Joel Coehoorn
+2  A: 

If you just want the IP that's bound to the adapter, you can use WMI and the Win32_NetworkAdapterConfiguration class.

http://msdn.microsoft.com/en-us/library/aa394217(VS.85).aspx

TimK
+3  A: 

Jonathan Holland's answer is fundamentally correct, but it's worth adding that the API calls behind Dns.GetHostByName are fairly time consuming and it's a good idea to cache the results so that the code only has to be called once.

Jekke
A: 

I believe theoretically you are unable to do such a thing while being behind a router (e.g. using invalid ip ranges) without using an external "help".

Eduardo
+15  A: 

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).

Nathan Strong
+3  A: 

The main issue is the public IP address is not necessarily correlated to the local computer running the application. It is translated from the internal network through the firewall. To truly obtain the public IP without interrogating the local network is to make a request to an internet page and return the result. If you do not want to use a publicly available WhatIsMyIP.com type site you can easily create one and host it yourself - preferably as a webservice so you can make a simple soap compliant call to it from within your application. You wouldn't necessarily do a screen capture as much as a behind the scenes post and read the response.

+1  A: 

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.

configurator
+1  A: 

Patrik's solution works for me!

I made one important change. In process message I set the CallContext using this code:

// try to set the call context
LogicalCallContext lcc = (LogicalCallContext)requestMessage.Properties["__CallContext"];
if (lcc != null)
{
    lcc.SetData("ClientIP", ipAddr);
}

This places the ip address in the correct CallContext, so it can later be retrieved with GetClientIP().

Justin Tanner
+2  A: 

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.

Patrik