How to get the Internet IP
-
I need to be able to run an application on a local pc that is behind a firewall and get the internet ip address for the computer. does anyone know how this can be done in .net?
You should take a look at the IPAddress Class[^] on MSDN[^]. - Nick Parker
My Blog | My Articles -
I need to be able to run an application on a local pc that is behind a firewall and get the internet ip address for the computer. does anyone know how this can be done in .net?
If you want the IP address of the computer behind the firewall (and you're outside), then you must define your protocol such that the IP address is included in the TCP (or UDP) message - not in the header (which is masked using the firewall's IP address with a NAT'd firewall). It probably won't do you much good, though, since 1) masked IP addresses are typically in the reserved range and don't route throughout the Internet, and 2) you still can't communicate with it directly.
Microsoft MVP, Visual C# My Articles
-
I need to be able to run an application on a local pc that is behind a firewall and get the internet ip address for the computer. does anyone know how this can be done in .net?
I would try this method that I used. IPAddress ipAdress = IPAddress.Any; IPEndPoint Enp = new IPEndPoint(ipAdress, 60001); Socket m_soc = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); m_soc.Bind(Enp); //then do this to get the current IP&source port System.Windows.Forms.MessageBox.Show(m_soc.LocalEndPoint.ToString()); Try that..should work in a x.x.x.x:x format :-D
-
If you want the IP address of the computer behind the firewall (and you're outside), then you must define your protocol such that the IP address is included in the TCP (or UDP) message - not in the header (which is masked using the firewall's IP address with a NAT'd firewall). It probably won't do you much good, though, since 1) masked IP addresses are typically in the reserved range and don't route throughout the Internet, and 2) you still can't communicate with it directly.
Microsoft MVP, Visual C# My Articles
ok but, here's the thing i'm not outside i have a client app that runs on the client pc and i have a server app that needs the ip. the problem i had is that i need the client ip so i can route connection from the server to the client kind of like a one chat. the only restriction is i can't give out the server ip. so that leaves me with the only one option to have the server connect to client. so that's my situation.
-
ok but, here's the thing i'm not outside i have a client app that runs on the client pc and i have a server app that needs the ip. the problem i had is that i need the client ip so i can route connection from the server to the client kind of like a one chat. the only restriction is i can't give out the server ip. so that leaves me with the only one option to have the server connect to client. so that's my situation.
Well, if you intend on actually connecting to a client behind a firewall, make sure you set up port forwarding for it. As for getting the IP, your only choice is to use a machine outside the local network. Something like make a HTTP query to whatsmyip.com and extract the result.
-
ok but, here's the thing i'm not outside i have a client app that runs on the client pc and i have a server app that needs the ip. the problem i had is that i need the client ip so i can route connection from the server to the client kind of like a one chat. the only restriction is i can't give out the server ip. so that leaves me with the only one option to have the server connect to client. so that's my situation.
That's a pretty dumb restriction - knowing an IP isn't a problem; it's making sure that the server is secure that is. Even if the server establishes a connection with the client, it's easy to determine the IP address (it's in the TCP/IP headers!). But you probably can't do anything about that. If both the client and server are on the same LAN, you can use a multicast group. See the class documentation[^] for the
MulticastOption
class in the .NET Framework SDK for more details and a client/server example. This uses UDP to send datagrams (connectionless IP, unlike TCP) to a multicast IP address (reserved range; it's not the server's IP). Everyone in the multicast group can receive the message and process it.Microsoft MVP, Visual C# My Articles