Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning static/same IP address to the Server everytime it logs in

I am working on udp server/client applicataion. Since for communicating with the server, all the clients must know the ip address and port number of the server. For this purpose, I have hard coded the ip and port number of my server to the clients so that everytime, the client connects to same ip and port number. (found the ip address of the server machine using ipconfig command.) But now, the problem is that I am working on DHCP network, and there is a chance that everytime sever machine is restarted, a new ip address may be assigned to it (different from the ip address known by the clients at which they will connect.) So, I always want the ip address hard coded at client side to be assigned to the server machine, everytime it logs in. Is there any way to do it? I have no idea about it. Searched internet but couldn't find anything relevant. Looking forward to help :(

like image 442
Ayse Avatar asked Apr 05 '13 05:04

Ayse


4 Answers

Assuming that your clients are local to the server, why not abandon the hard-coded server IP address, and borrow a page from DHCP and use some kind of service discovery method:

  • Your clients broadcast "where is the server" message when they first come online. The server responds with "I am at IP address X.X.X.X"
  • When the server comes up, it broadcasts "Server is now at IP address Y.Y.Y.Y" so that if the server crashed, the clients start using the new server.
like image 195
angelatlarge Avatar answered Sep 28 '22 06:09

angelatlarge


Presuming you are working on a LAN, that's how I'd do it.

Presuming your DHCP server is configurable enough:

  • Assign a static map MAC address/IP address in the dhcp server, so that the same machine always get the same IP (just for the server, not for every client).

Most entry level all in one devices with DHCP have this functionality, if not it should be quite cheap to buy a new one that has it. If your DHCP server is a real computer, you can surely configure it to do so.

Additionally you might want to tell your clients to use a local DNS and in this local DNS server define a name for your server, so you won't have to hardcode an IP address in your clients. But the address should be located in some configuration file rather than hardcoded in any case.

I have used dnsmasq to serve as both DNS server with local names, and as DHCP server, giving the servers always the same address and pointing all the DNS requests towards itself.

This questions could be useful to find a windows alternative for dnsmasq: https://stackoverflow.com/questions/7709744/is-there-something-like-dnsmasq-for-windows

like image 26
LtWorf Avatar answered Sep 28 '22 04:09

LtWorf


By adding a reservation field in the DHCP server we can attain this. If you are using Windows DHCP server, there is a section named 'Reservations', there we can give the MAC address of your pc and the desired IP address. Then the server will provide the mentioned IP for you.

like image 24
user2198653 Avatar answered Sep 28 '22 05:09

user2198653


With the narrow focus of a developer a DHCP reservation might be the logical step. But using a nameserver is far better. If the network itself changes or maybe the server is moved to another subnet or maybe even into anoher zone, using an IP address from a DHCP reservation fails, because the server's address changes.

You don't have any of these problems if you use a nameserver. That is what DNS is meant to be doing. Think of it as a "serviceprovider finding service" that detaches your service from the host it is running on.

And, like already suggested, you should never hardcode an IP address or DNS name or anything else that might change (even if you think it will not change) unless it is a design goal that things aren't working anymore if something changes (=not configurable).

like image 41
Abu Dun Avatar answered Sep 28 '22 04:09

Abu Dun