In this code find only system IP address and system name and I want to find all IP addresses and names available on network.
String StringHost;
StringHost = System.Net.Dns.GetHostName();
IPHostEntry ipEntry =System.Net.Dns.GetHostEntry(StringHost);
IPAddress[] address = ipEntry.AddressList;
for (int i = 1; i < address.Length; i++)
{
IP_Address_Datagridview.Rows.Add();
IP_Address_Datagridview.Rows[i].Cells[0].Value = StringHost;
IP_Address_Datagridview.Rows[i].Cells[1].Value = address[i].ToString();
}
The easiest way to see if a computer is on a local network is to use ICMP to 'ping' each address on your subnet. If the ICMP service is enabled on the IP address you are trying, (which is usually is on workstations, but not always on servers), then you will get a response.
Then you will want to take each valid IP address and do a reverse namespace lookup.
Related
I am trying to get IP Addresses of the different computers which are in the same domain by providing HostName to the Dns.GetHostAddresses() member functon but it returns 69.172.200.109 IP Address for all the computers. I failed to know the exact solution for this. Solution for this is much appreciated.
IPAddress[] ips;
ips = Dns.GetHostAddresses(hostName);
foreach (IPAddress ip in ips)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip.ToString();
break;
}
}
The scenario you are encountering is very much possible if there is another server in between, that routes the requests to appropriate server based on host name received in request. The address you are being served might be the address of that intermediate server. Please see Virtual web hosting for more details.
Secondly, if it's a local DNS that you are querying, make sure you double check the addresses configured there.
you can try bellow code to get all ip address your domain
String strHostName = Dns.GetHostName();
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
// Enumerate IP addresses
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
....
}
I have 3 PCs, one is server others are clients. Clients connects to server by entering the server local IP. All works good but problem occurs when the router restarts and server get assigned a different local IP. Now, need to enter IP address again of server in clients. I can solve this by using a local static IP but is it possible to connect without setting local static IP ?
Edit:
Using TCP Socket.
Use the hostname for connecting to remote computer rather its IP address.
You will have to rely on DNS lookup though.
Your problem occurs because you use DHCP function of router. D of DHCP stands for "Dynamic", so IP addresses may be changed in some occasion.
The most simple solution is 'Not to use DHCP'.
[Detail of solution]
Assign fixed IP addresses for your PCs, and use that IP addresses to access among them.
Usually, router uses local IP address in range from 192.168.1.1 to 192.168.1.255 and 192.168.1.1 is used for router itself.
You may be able to use IP addresses 192.168.1.2, 192.168.1.3 and 192.168.1.4 for 3 PCs respectively.
One thing you could do, how about you give the server an endpoint that is unique, something like
http://<server>/isthisme
Then you just go through all IP adresses in the network and then try to reach that endpoint. The one where it returns 200 that is your server.
To get the IPs is a bit complicated, you first need to get your own IP, then go with the subnetmask over it and at the end you can just go the following way:
for (int p1 = 1; p1 < netmask[0]; p1++) {
for (int p2 = 1; p2 < netmask[1]; p2++) {
for (int p3 = 1; p3 < netmask[2]; p3++) {
for (int p4 = 1; p4 < netmask[3]; p4++) {
var ip = new IPAdress(p1, p2, p3, p4);
if (trytoreach(ip)) {
return ip;
}
}
}
}
}
This is a way you could go by. It is not optimized so feel free ^^
Here is a gist with the method https://gist.github.com/DerKnerd/ff9c34087955efce0970. Just the part with the subnetmask you need to figure out. I don't guarantee that it will work though.
as i'm going to create Maintenance Request Page, so any employee can use this page from our local Network without internet, and i want to get the computer name and Ip for the one which made the request,
i search and read about this, but most if what i got it is: how to get the internet ip or host name and IP but my question is "how to get Network Compter Name and Netwok IP "Not internet IP" in Asp.Net"??
some of what i found it about Internet Ip and Host Info is:
using System.Net;
//Get the Host name:
string strHostName = string.Empty;
strHostName = Dns.GetHostName();
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
//Get the IP address
IPAddress[] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Response.Write("IP Address {0}: {1} " + i + addr[i].ToString() + ", HostName: " + strHostName);
}
// end of host info.
and this way for internet info:
Response.Write("Your IP Address: " + Request.UserHostAddress + "<BR>");
Response.Write("Your Computer Name: " + System.Net.Dns.GetHostEntry(Request.UserHostAddress).HostName);
so is there any way to get the local Ip and Name of computer??
If you have DNS correctly configured to be updated with the names of computers as they connect and disconnect, then you can use reverse resolution to get from the client IP address back to the host name.
If your web site is inside the firewall, the connected user host address will be the internal address and can be resolved. If the site is hosted outside (or possibly in the DMZ) then you will not be able to get the name without requiring the user to provide it - or by running some trusted code on the client to access the local information.
How can I get all of the IP addresses attached to the machine that my application (C# NET Console app) is running on? I need to bind a WCF service to the primary IP address, and return a list of the full IP address list.
using System.Net;
string myHostName = Dns.GetHostName().ToString();
string ipAddress = Dns.Resolve(HostName).AddressList[0].ToString();
This is what I am using right now to get the Primary IP address, but I can't figure out how to get the rest to return them.
If I bind a WCF service to localhost:8000, will that expose it on the primary?
The DNS variants work across the network, but one DNS entry can have many IP addresses and one IP address can have many DNS entries.
More importantly, an address needn't be bound to a DNS entry at all.
For the local machine try this:
foreach (NetworkInterface netInterface in
NetworkInterface.GetAllNetworkInterfaces())
{
Console.WriteLine("Name: " + netInterface.Name);
Console.WriteLine("Description: " + netInterface.Description);
Console.WriteLine("Addresses: ");
IPInterfaceProperties ipProps = netInterface.GetIPProperties();
foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
{
Console.WriteLine(" " + addr.Address.ToString());
}
Console.WriteLine("");
}
I think this example should help you.
// Get host name
String strHostName = Dns.GetHostName();
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
// Enumerate IP addresses
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
....
}
Edit:
"There's no such thing as a "primary" IP address.
The routing table determines which outward-facing IP address is used depending on the destination IP address (and by extension, the network interface, which itself can be virtual or physical)."
Why not just bind to 0.0.0.0 ?
That way you listen on all ips
You should probably bind to 0.0.0.0:8000, that will expose it on all available IP addresses and only bind to a particular IP address if the user/administrator demands so.
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
I think the OP is asking about how to get all addresses on a local NIC, not just those addresses known to DNS.
By primary he probably means the main address under "use the following IP address" in the adapter properties, and by "the rest" he probably means those listed in Advanced > (Additional) IP Addesses.
DNS will not necessarily know those.
I have managed to get the connected clients IP with the code below but can't seem to get the hostname.
Globals.connectedIPAddress = "" + IPAddress.Parse(((
IPEndPoint)_client.Client.RemoteEndPoint).Address.ToString());
Well, not every IP address has a name. However, given the IPAddress you can use Dns.GetHostEntry to try to resolve it. Also note that if it's being a NAT router, you'll be getting the router's IP address rather than their actual machine.
And just to address the point in the comments, I agree that there's no point in ToString/Parse/ToString:
IPAddress address = ((IPEndPoint)_client.Client.RemoteEndPoint).Address;
Globals.connectedIPAddress = address.ToString();