Reputation: 1967
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.
Upvotes: 0
Views: 442
Reputation: 2002
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.
Upvotes: 1
Reputation: 1091
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.
Upvotes: 1
Reputation: 987
Use the hostname for connecting to remote computer rather its IP address. You will have to rely on DNS lookup though.
Upvotes: 2