2

Im sorry if this been asked before, but couldnt find anything about this particular matter.

I try to find out with which of my own ip's my computer use to connect to a remote ip. I use some kind of socket setup both ways etc, and im sending my ip (and other stuff with xml) to another server so it knows how to contact me. But how do i figure out which ip i should send to it? I have the servers ip or host name, but trying with

Dns.GetHostEntry("host").AddressList

But that only gives me the ip of the remote host and not how my computer reach it.

Is my question solveable at all or is this just wishful thinking?

Erik Karlsson
  • 570
  • 1
  • 6
  • 17
  • See: http://alperguc.blogspot.com/2008/11/c-how-to-get-computer-ip-address.html maybe? – Ry- Jan 11 '12 at 03:19
  • 2
    Sending your local IP would be useless if you're behind a NAT gateway (e.g most home routers, cellular connection, etc...). It'd be (say) 10.x.x.x and be unreachable from the outside. – Marc B Jan 11 '12 at 03:20
  • Minitech: Was thinking about that as well, but if the user have several lans, for example VM's its hard to know which one is related to the server in connecting to. – Erik Karlsson Jan 11 '12 at 03:24
  • Marc B: That is a calculated risk in this content :) Most user of this will know that they need a public ip to connect over the internet. But this will mainly go in internal networks. – Erik Karlsson Jan 11 '12 at 03:25
  • Is this even necessary? If you connect to the server first, it knows what IP you connected from and thus can use that to "call you back", assuming NAT is not involved. All you need to tell the server is the port number you're listening on. – Sven Jan 11 '12 at 03:37
  • After the socket is connected to the remote host, you can find out which local IP was used for the connection by referencing the LocalEndpoint property of the C# socket. Likewise, the other end can use the RemoteEndpoint property. But I'm with the folks above - it's not a great design to pass your internal IP address around. Here's a link - http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.localendpoint.aspx – selbie Jan 11 '12 at 03:53
  • @ErikKarlsson - After posting my answer I thought of a couple questions that would likely help answer your question: 1. is the remote IP in your LAN, or on the internet? 2. *How* are you connecting to the remote IP (TCP/UDP)? It doesn't appear that you're using WCF though if you are may be helpful to clarify the situation. 3. Is this for an application you're currently developing, for a legacy product you are supporting, or something else? – M.Babcock Jan 11 '12 at 05:31

3 Answers3

0

Can you have the remote host capture that data? That end would surely have it.

itsme86
  • 19,266
  • 4
  • 41
  • 57
  • That is a option i have considered, but that part is not built inhouse. But raising the issue cant hurt, it cant be hard for them to fix it i guess. – Erik Karlsson Jan 11 '12 at 03:38
0

If you're using UDP then this is exactly what the STUN protocol was designed for. STUN is used in VOIP applications (among other P2P systems) to be able to tell what a specific connection looks like on the internet.

One very reliable .NET implementation that implements STUN is included in the Lumisoft.NET library (source code available here). I've used it myself for to satisfy this specific task for applications ranging from VOIP to P2P VPN alternatives. It is very easy to use and is standards compliant.

NOTE: I am NOT in any way affiliated with Lumisoft, I've merely used their library in several different applications

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • On second thought this only applies in limited circumstances and your question is unspecific. It'll work if your application is UDP, you want your application to work on the greater internet, and you're behind a router that supports STUN as a traversal mechanism. – M.Babcock Jan 11 '12 at 05:38
0

Ok, quick update that wont help anyone except myself.

But.

Found out that i can send hostname instead of actual ip, will work for now and most cases, and let the DNS do what its suppose to do :)

Erik Karlsson
  • 570
  • 1
  • 6
  • 17