<p>I need to find out the <strong>external</strong> IP of the computer a C# application is running on. </p>

<p>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?</p>

<p><em>(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)</em></p>

<p><strong>Solution:</strong><br>
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.</p>

<pre><code>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;
}
</code></pre>

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

<pre><code>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();
}
</code></pre>

<p>I can now send the IP back to the client.</p>

[{'answer_id': 66393, 'author': 'Matt Michielsen', 'author_id': 9769, 'author_profile': 'https://Stackoverflow.com/users/9769', 'pm_score': -1, 'selected': False, 'text': '<p>You can basically parse the page returned by doing a WebRequest of <a href="http://whatismyipaddress.com" rel="nofollow noreferrer">http://whatismyipaddress.com</a></p>\n\n<p><a href="http://www.dreamincode.net/forums/showtopic24692.htm" rel="nofollow noreferrer">http://www.dreamincode.net/forums/showtopic24692.htm</a></p>\n'}, {'answer_id': 66407, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 3, 'selected': False, 'text': '<p>Better to just use <a href="http://www.whatismyip.com/automation/n09230945.asp" rel="nofollow noreferrer">http://www.whatismyip.com/automation/n09230945.asp</a> it only outputs the IP just for the automated lookups.<br/><br />If you want something that does not rely on someone else put up your own page <a href="http://www.unkwndesign.com/ip.php" rel="nofollow noreferrer">http://www.unkwndesign.com/ip.php</a> is just a quick script:</p>\n\n<pre><code>&lt;?php\necho \'Your Public IP is: \' . $_SERVER[\'REMOTE_ADDR\'];\n?&gt;\n</code></pre>\n\n<p>The only downside here is that it will only retrieve the external IP of the interface that was used to create the request.</p>\n'}, {'answer_id': 66408, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n\n<p>So:</p>\n\n<pre><code>IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName());\nstring externalIP = IPHost.AddressList[0].ToString();\n</code></pre>\n\n<p>EDIT:</p>\n\n<p>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.</p>\n'}, {'answer_id': 66415, 'author': 'Alex M', 'author_id': 9652, 'author_profile': 'https://Stackoverflow.com/users/9652', 'pm_score': -1, 'selected': False, 'text': '<p>The most reliable manner of doing this is checking a site like <a href="http://checkip.dyndns.org/" rel="nofollow noreferrer">http://checkip.dyndns.org/</a> 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 <a href="http://www.faqs.org/rfcs/rfc1918.html" rel="nofollow noreferrer">RFC1918</a> private address (<code>192.168.x.x</code> being the most familiar of these.</p>\n\n<p>Failing that, you can implement your own, similar, service sitting external to the firewall, so you will at least know if it\'s broken.</p>\n'}, {'answer_id': 66451, 'author': 'TimK', 'author_id': 2348, 'author_profile': 'https://Stackoverflow.com/users/2348', 'pm_score': 2, 'selected': False, 'text': '<p>If you just want the IP that\'s bound to the adapter, you can use WMI and the Win32_NetworkAdapterConfiguration class.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/aa394217(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa394217(VS.85).aspx</a></p>\n'}, {'answer_id': 66480, 'author': 'Yes - that Jake.', 'author_id': 5287, 'author_profile': 'https://Stackoverflow.com/users/5287', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 66522, 'author': 'Eduardo', 'author_id': 9823, 'author_profile': 'https://Stackoverflow.com/users/9823', 'pm_score': 0, 'selected': False, 'text': '<p>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".</p>\n'}, {'answer_id': 66543, 'author': 'Nathan Strong', 'author_id': 9780, 'author_profile': 'https://Stackoverflow.com/users/9780', 'pm_score': 4, 'selected': False, 'text': '<p>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?"</p>\n\n<p>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.</p>\n\n<p>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).</p>\n\n<p>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.</p>\n\n<p>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).</p>\n'}, {'answer_id': 66564, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 67658, 'author': 'configurator', 'author_id': 9536, 'author_profile': 'https://Stackoverflow.com/users/9536', 'pm_score': 1, 'selected': False, 'text': "<p>Well, assuming you have a <code>System.Net.Sockets.TcpClient</code> connected to your client, you can (on the server) use <code>client.Client.RemoteEndPoint</code>. This will give you a <code>System.Net.EndPoint</code> pointing to the client; that <em>should</em> contain an instance of the <code>System.Net.IPEndPoint</code> subclass, though I'm not sure about the conditions for that. After casting to that, you can check it's <code>Address</code> property to get the client's address.</p>\n\n<p>In short, we have</p>\n\n<pre><code>using (System.Net.Sockets.TcpClient client = whatever) {\n    System.Net.EndPoint ep = client.Client.RemoteEndPoint;\n    System.Net.IPEndPoint ip = (System.Net.IPEndPoint)ep;\n    DoSomethingWith(ip.Address);\n}\n</code></pre>\n\n<p>Good luck.</p>\n"}, {'answer_id': 928106, 'author': 'Justin Tanner', 'author_id': 609, 'author_profile': 'https://Stackoverflow.com/users/609', 'pm_score': 2, 'selected': False, 'text': '<p>Patrik\'s solution works for me!</p>\n\n<p>I made <b>one</b> important change.  In process message I set the <code>CallContext</code> using this code:</p>\n\n<pre><code>// try to set the call context\nLogicalCallContext lcc = (LogicalCallContext)requestMessage.Properties["__CallContext"];\nif (lcc != null)\n{\n    lcc.SetData("ClientIP", ipAddr);\n}\n</code></pre>\n\n<p>This places the ip address in the correct CallContext, so it can later be retrieved with\n<code>GetClientIP()</code>.</p>\n'}, {'answer_id': 1319527, 'author': 'Patrik Svensson', 'author_id': 936, 'author_profile': 'https://Stackoverflow.com/users/936', 'pm_score': 4, 'selected': True, 'text': '<p>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.</p>\n\n<pre><code>public ServerProcessing ProcessMessage(IServerChannelSinkStack sinkStack, \n    IMessage requestmessage, ITransportHeaders requestHeaders, \n    System.IO.Stream requestStream, out IMessage responseMessage, \n    out ITransportHeaders responseHeaders, out System.IO.Stream responseStream)\n{\n    try\n    {\n        // Get the IP address and add it to the call context.\n        IPAddress ipAddr = (IPAddress)requestHeaders[CommonTransportKeys.IPAddress];\n        CallContext.SetData("ClientIP", ipAddr);\n    }\n    catch (Exception)\n    {\n    }\n\n    sinkStack.Push(this, null);\n    ServerProcessing srvProc = _NextSink.ProcessMessage(sinkStack, requestmessage, requestHeaders,\n        requestStream, out responseMessage, out responseHeaders, out responseStream);\n\n    return srvProc;\n}\n</code></pre>\n\n<p>And then later (when I get a request from a client) just get the IP from the CallContext like this.</p>\n\n<pre><code>public string GetClientIP()\n{\n    // Get the client IP from the call context.\n    object data = CallContext.GetData("ClientIP");\n\n    // If the data is null or not a string, then return an empty string.\n    if (data == null || !(data is IPAddress))\n        return string.Empty;\n\n    // Return the data as a string.\n    return ((IPAddress)data).ToString();\n}\n</code></pre>\n\n<p>I can now send the IP back to the client.</p>\n'}]
['https://Stackoverflow.com/questions/66363', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/936/']