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.
Related
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.
My application is sending reports to another application (which maintains a database of reports) on the network with simple IPv4 addresses. I can construct a valid IPAddress in two ways:
string address = "200.1.2.41";
IPAddress ip1 = IPAddress.Parse(address);
IPAddress ip2 = (Dns.GetHostEntry(address)).AddressList[0];
If address represents an IP that is reachable, both methods are quick (though IPAddress.Parse is quickest). But if address is not reachable (eg. the server is off or the user has entered the wrong IP in Settings) then Parse is lightning quick...but Dns.GetHostEntry hangs for up to 9s.
I did a parameter-by-parameter check and the final variables ip1 and ip2 are identical. Given that Parse is always quick, and that I'm using standard four-octet IPv4 addresses only, is there any compelling reason to use the Dns.GetHostEntry method? Might I need Dns.GetHostEntry if I switch to IPv6 or named hosts like FOOD.HALL.01 in future?
If you just want get an instance of IPAddress for your IP address string representation, than yes, using the DNS for that purpose is absolute overkill.
All sorts of timeouts, latencies, etc. are absolute expected. At least compared to the purely local parsing and disecting of the string representation that happens in IPAddress.Parse(). What that does is, ask the DNS server to resolve the IP address string into a hostname "entry". From that you get the IP address back that you knew all along (albeit as string and not IPAddress).
Now, if you want to be able to "convert" host names into IP addresses in the future, then yes, you need to go via DNS.
But you could always do it in that manner (conceptually):
// First see if it is a valid IP address rep - fast.
IPAddress ip;
if (!IPAddress.TryParse(address, out ip))
{
// See if it is a hostname - slower.
ip = Dns.GetHostEntry(address).AddressList[0];
}
And yes, IPAddress.TryParse() (or Parse()) can handle IPv4 and IPv6 addresses.
My web application runs on a local IIS server. When calling the api of my web app
using fiddler i get a strange client ip adress.
public static class HttpRequestMessageHelper
{
public static string GetClientIpAddress(this HttpRequestMessage request)
{
string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
if (string.IsNullOrEmpty(ip))
{
return "Unknown IP-Adress";
}
}
return ip;
}
}
I am using this extension method for obtaining the ip.
The ip i am getting looks like this: "fe80::745a:d3fa:db2c:7b94%11"
That's an IPv6 address:
http://en.wikipedia.org/wiki/IPv6
As the world is running of IPv4 addresses (xxx.xxx.xxx.xxx) the new standard is to bring in a lot more addresses.
Sadly they're not as easy to remember, so DNS lookups will be vital IMHO.
You can check you adaptor settings / ipconfig and / or your DHCP server (local router perhaps?) and see, chances are you being issued IPv6 addresses.
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();
how can I get the client's computer name in a web application. The user in a network.
Regards
// Already tryed this option
string IP = System.Web.HttpContext.Current.Request.UserHostAddress;
string compName = DetermineCompName(IP);
System.Net.IPHostEntry teste = System.Net.Dns.GetHostEntry(IP);
ssresult = IP + " - " + teste.HostName;
// TODO: Write implementation for action
private static string DetermineCompName(string IP)
{
IPAddress myIP = IPAddress.Parse(IP);
IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
string[] compName = GetIPHost.HostName.ToString().Split('.');
return compName[0];
}
All of that, gives me only the IP :/
You can't do this in a way that is guaranteed to work.
The closest you will be able to get is to go down the route of doing a reverse dns lookup using System.Net.Dns.GetHostEntry which is what you have already tried.
The problem is that your machine has no way of knowing the hostname of a remote web client via its IP address alone (unless it is on the same subnet, in which case you may be able to retrieve it).
You have to fall back on your DNS infrastructure being able to map the IP back into a hostname [this is what nslookup does when you type in a hostname], and plenty of places just won't bother setting up reverse IP records.
Plus, often if they do, they won't match the hostname. It is quite common to see a reverse lookup for "1.2.3.4" come back as something line "machine-1.2.3.4", instead of the actual hostname.
This problem is exacerbated further if the clients are behind any sort of Network Address Translation so that many client computers have a single IP from an external perspective. This is probably not the case for you since you state "The user in a network".
As an aside, if you go to the server and type in, at a command prompt,
nslookup <some ip>
(where is an example of one of these client machines), do you get a hostname back?
If you do, then System.Net.Dns.GetHostEntry should be able to as well, if not then it probably can't for the reasons mentioned above.
you can get the windows machine name with - System.Environment.MachineName
Assuming your clients are Windows based running IE you could use this client side code to get the names and pass them back to the server:
<script type="text/javascript">
function create()
{
var net = new ActiveXObject("wscript.network");
document.write(net.ComputerName);
}
</script>
Yeah would require you to keep requesting and caching the computers terribly inefficient, was a bad idea.
I would go with running nslookup in the background I haven't tested this code and neither is it handling errors for failures, but basically, you can do:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo sInfo = new System.Diagnostics.ProcessStartInfo("nslookup.exe", "192.168.1.100");
string result = process.StandardOutput.ReadToEnd();
Then just parse the result value.