I'm a little confuse about getting the ip address of a PC. I have a winforms application running in multiples pc. The application in certain circunstances must send messages to the other apps. To do that I write to the database the infomation about the pc that is running the app, among the data is the IP Address. The idea is to use WCF to comunicate and broadcast messages to running applications.
So, the problem is how can I get the working Ip Address. Image a notebook with an ethernet ip address, a wi fi address, and a vmware address. Which of them do i have to choose?
Here is the code i use. But in the case of the notebook it is returning the vmware address, and it is useless.
private String GetMyIp()
{
String ipAddress =
System.Net.Dns.GetHostEntry(
System.Net.Dns.GetHostName())
.AddressList.First(i => i.AddressFamily.Equals(
System.Net.Sockets.AddressFamily.InterNetwork))
.ToString();
return ipAddress;
}
The main idea is to get the ip address which has a connection. Hope be clear.
Thanks in Advance.
Well unless you make a connection its a lot of work for your computer to determine which card its going to use.. or did use.
Heres the way I did it.. I made a connection then asked, what it used to do it, actually I used part of your code, it certainly seems to work
Also, of course the other end of the connection can also check the remote (eg client) address in a similar manner..
class Program
{
static void Main(string[] args)
{
TcpClient t = new TcpClient("www.microsoft.com", 80);
Console.WriteLine(GetMyIp(t.Client.LocalEndPoint.AddressFamily));
t.Close();
}
static private String GetMyIp(AddressFamily addr)
{
String ipAddress = System.Net.Dns.GetHostEntry(
System.Net.Dns.GetHostName()
)
.AddressList.First(i => i.AddressFamily.Equals(addr)).ToString();
return ipAddress;
}
}
There are several things you need to consider when enumerate the active network connection. if you have wifi adapter and LAN connection and both are active , which you would like the running application to use ?
why not using a good practice of central WCF publish-subsribe method, let the running client to request the central WCF service and probably on the WCF server end to store the EndPoint address ?
Related
I'm currently working with TCP/IP Sockets, My client console program has to connect with server which is my PC, If the destination in the client program is specified as a local host it works fine, what I need to get done with is to connecting the client with my server through internet, what I did is looked up for my IP address on http://www.whatismyip.com/ and tried but it didn't work as I think it's a Network Interface IP address, then I altered destination IP address in client program specifically to the address of my computer which I want it to be a server, but that didn't work also. Here's my code.
Ip = (IPAddress.Parse("192.168.1.4"));
MyClient.Connect(Ip,6000);
GetStream = MyClient.GetStream();
Console.WriteLine("CONNECTED TO SERVER");
Read = new BinaryReader(GetStream);
Write = new BinaryWriter(GetStream);
There 2 things (at least) that you should be aware of:
1. To access your computer from Internet (from the public address, the one you get with whatismyip.com) you need to open the port (6000) at the router, and tell the router to what IP it should forward the incoming connection. You could specified specifics ports or put a DMZ host where all the incoming connections will be routed to that host/PC. Read your router manual to see how that is done.
2. You cannot access your public IP from the inner side of the router (intranet), if you want to connect to your public IP you need to be in another network.
If you have a dynamic IP (is the default) every time the router is powered off and on then, most probably, that IP would change, you need to investigate thru whatismyip.com in order to know what IP has been assigned. You could connect to dyndns.org and ask for a host name myhost.mydomain.com (.es, .fr, etc), and in the router tell the DDNS (Dynamic DNS) to update that host every time the IP changes. In your client program you then connect to MyClient.Connect("myhost.mydomain.com", 6000);
Hope I explain myself well enough, anyway, if you have any question let me know.
I try to get my correct IP but I can't
I'm using this code:
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
But it does not work for me!
For example my real IP is 151.246.147.86 but with this code I get 192.168.1.2.
Note: I have 4 Network adapters and in DOC with IPConfig I see this:
Network adapter: Local Area Connection 6
Media state : Media disconnected
Network adapter: Local Area Connection 4
Media state : Media disconnected
Network adapter: Local Area Connection 3
IP Address: 10.10.255.222
Network adapter: Local Area Connection
IP Address: 192.168.1.2
Now I connect to net and using with "Local Area Connection"; the public IP of my WAN connection is 151.246.147.86. I want to get this public IP (151.246.147.86), how can I do that?
Note : I don't want (and I can't, since I am using a VPN) use third party websites to get my IP
Please help!
-------EDIT :-------------
Note : I using from VPN and my VPN IP (for example) is : 176.227.197.111. But the IP of my WAN is: 151.246.147.86 and i want to get this address.
You (probably) are using a router/modem, therefore 192.168.1.2 is your "real IP". Your modem/router will be on the same (private) network, and its public interface (WAN) will have a public IP. So you need to get the IP on the public interface of your modem/router.
How to get the WAN IP of your modem depends on your mark and model; if it supports UPnP you can probably do it, or maybe if it is an enterprise class router it may also support SNMP...
You should specify your make and model.
Another way without using external sites: do a tracert to a known site (google?) the first hop will be your route.
Also, if you are on a VPN, you may be able to use the same technique. For example, if you know another host on the VPN (A server maybe?) you can ping/tracert it and discover your router from there. I don't know if in this case you will obtain what you call "a real IP" (by the way, how do you know this IP in the first place? You may be able to obtain it in the same way, programmatically).
Another solution for your VPN-based scenario: you can use Windows to help you. Windows has some kind of VPN management (RAS) which may help you; I would suggest starting from here to understand the basics, and then look for a library/SDK to help you (a quick google returned http://dotras.codeplex.com/).
What you ask is not possible unless there is some way to query your router/modem/external-most endpoint for its WAN address. The only IP address your computer knows about is its own (internal IP).
Technical note: there is nothing non-"real" about the IP address 192.168.1.2 - this is your computer's address. It is simply local to your given internal network and all but useless to anything outside.
If your router supports uPnP, you will need to query GetExternalIPAddress (starting point Is there a UPnP Library for .NET (C# or VB.NET)?). However since uPnP is considered dangerous and many security-conscious users turn it off, I would not count on it being enabled.
Querying an external service will be your most reliable bet for getting your external IP, whether it is one you write, or a third party service (see also https://superuser.com/questions/420969/services-that-just-return-your-public-ip-as-text).
There are many methods and tutorials that teach you how to find an ip address,I use this tool http://www.ipgp.net/ to display information about any ip address, just enter any IP address into the box and you will get the country, city, region, ISP, street address and the satellite location map for every query.
you're trying to get your IP address from your computer or a server? It will make a lot of difference. If you're trying to get your IP address from your computer/laptop use http://www.howtochangeipaddress.com. Your IP will pop up in front of you soon you enter the site.
Good afternoon,
Can anyone give any examples of how to obtain the IP Address of the local machine when it's connected to a remote windows domain network via VPN (RAS)? i.e. I need the VPN address and not the remote users local network address.
For example, my Server Side Windows Service communicates with my client side application and needs to create a log of all connected users and their IP Addresses.
This solution is easy enough when using a computer on the local network, but I wondered how I can go about getting the IP addresses of the users who are connected to the server via VPN. Please note that the IP address get method will be executed client side and sent to the server.
Here's my current code that works only when locally connected to the domain network:
public static string GetLocalIPv4()
{
string ipv4Address = String.Empty;
foreach (IPAddress currrentIPAddress in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (currrentIPAddress.AddressFamily.ToString() == System.Net.Sockets.AddressFamily.InterNetwork.ToString())
{
ipv4Address = currrentIPAddress.ToString();
break;
}
}
return ipv4Address;
}
Our internal network is controlled by Windows SBS and uses a domain such as mycompany.local.
Thank you very much for your time and I look forward to reading your responses.
Rob
As the comment from #MarcB notes, cannot think of a good reason why you might want that info... so would be interesting if you could explain a use for this in an application just out of curiosity.
However, there are a lot of incorrect answers on here and online regarding enumerating IP addresses for a machine by using Dns.GetHostAddresses. It appears most people are not realizing the difference between looking up a machine name in the configured DNS resolver versus enumerating the machine address. These are very different things and while it might seem to return the right results in many cases, this is absolutely the wrong way to go about it because they are not the same thing. For example, see this link to an article on here where the original poster flagged an incorrect response as the answer but the correct response is below by #StephanCleary. See:
Get IPv4 Addresses
The difference is you want to look at the machines configuration and enumerate whatever IP address you are interested in locating from the machines own TCPIP stack. The code above and many of the incorrect responses try to lookup the name in the DNS resolver. Once you have that part correct, then you should be able to determine the VPN connection based on the network adaptor (by name or other attribute).
I am using a library called Lidgren network that handles all the network part of my application. It uses UDP.
I never had any problem with TCP applications and port forwarding (apache, FTP, SMTP, etc). I did some TCP applications using TCP and I could connect without a problem.
I am making an online game right now and I use UDP for network transmission. I port forwarded it in my router and I have it running. When I use my remote IP Address (190.136.243.40) to connect to it, I can't. When I use loopback (127.0.0.1) I can connect without any kind of problem.
I turned off Windows firewall, I rebooted the computer, but I still can't connect using my remote IP Address.
Here is part of my code:
public static class ServerInfo
{
static ServerInfo()
{
ServerAddress = Dns.GetHostAddresses("leboran.dyndns.org")[0];
}
public const short Port = 6483;
public static IPAddress ServerAddress;
public const string AppIdentifier = "UnnamedGameNet";
public static byte[] MagicByte
{
get
{
return new byte[4] { 0xF1, 0x64, 0x83, 0xC4 };
}
}
}
This is the part where I start my server, probably not important:
public void StartServer()
{
this.listenThread = new Thread(ListenForClients);
peerConfig = new NetPeerConfiguration(SharedConstants.ServerInfo.AppIdentifier);
peerConfig.Port = SharedConstants.ServerInfo.Port;
peerConfig.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
peerConfig.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
peerConfig.ConnectionTimeout = 2144124;
netServer = new NetServer(peerConfig);
listenThread.Start();
}
The port I am using is 6483. I am running a Minecraft server at port 25565 and it works perfectly, so it is not a bad configuration on the router
Some computers have multiple ips in fact they all do, what you are probably experiencing is you've bound you app to listen to port 127.0.0.1 rather than 64.323.23.234 or ideally 0.0.0.0 which would bind all your ips. Check to see if peerConfig lets you pick what ip you are binding to the default may be 127.0.0.1
Also could be a problem with a Router. Try making your computer the DMZ or enabling port forwarding.
I dont think this is code related.
Your router would need to have the host configured because you are trying to connect directly to the public end of your router from the private side of it.
If you just want to be able to connect to the dyndns address so you dont have to change code you can also alter the windows hosts file.
Many consumer routers use the source of a packet to determine whether to treat the data as internal or external. Put another way, if you are on your internal network and try to access any of the IP addresses of your router, your router will treat it as being internal (i.e. responding to port 80 with a customization website, most others with no response).
Unfortunately solutions are hard to come by.
My GPRS modem has got a sim card. it can connect Web. Web service give it a ip number. i need it. like that: http://www.your-ip-address.com/
How can i do that C#?
You can use the static method WebClient.DownloadString(url) to read your external IP address from any web service providing such data:
string ip = System.Net.WebClient.DownloadString("http://whatismyip.org/");
If you are going to use this in a production environment, better make sure that the URL you are pointing to, is guaranteed to stay around for the entire lifespan of your application. The best way is probably to host the web service yourself.
Also, you should add some error checking around this code, as it will fail if the internet connection or the web service is unavailable.
You can get a list of your IP addresses via DNS using the following code:
var name = Dns.GetHostName();
var entry = Dns.GetHostEntry(name);
foreach (var address in entry.AddressList) {
Console.WriteLine(address);
}
If you want the IP address as a property of the hardware, you can use the System.Management.ManagementClass with the name Win32_NetworkAdapterConfiguration. See http://msdn.microsoft.com/en-us/library/system.management.managementclass.aspx for details.
You can create a WebRequest to http://whatismyip.com/automation/n09230945.asp which houses only your IP address
Start here