• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass an IP address that doesn't keep changing?  RSS feed

 
Henrique Aguiar
Greenhorn
Posts: 12
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new to this forum so I'm really sorry if I'm breaking any rules or anything.

So, I'm currently reading Head First Java, and I'm on chapter 15 (looking at socket connections and threads). But to connect different computers to a server, I'd need to get the server's computer's IP address. So I searched up how to get it, and then I discovered I could try ipconfig in the command line. All good and well, it worked, I got to connect two laptops (one of them was running the server).
But a while later, when I restarted my computer in a different place, the client wasn't connecting to the server anymore. When I checked it, it appears that the IP address changed. But well, if my laptop will run the server, how can I get a fixed IP address that works all the time? I can't just pass a new version of my client (with the updated IP) to the other laptops all the time.
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One option, is to ask your system administrator to place your server on a fixed IP address. Generally, it is a good idea to use a protocol like DHCP for clients, as it is easier to automatically manage a group of addresses for the client machines, as it can get really complex to assign (and reassign) them manually. Your system administrator may be willing to assign you a fixed address instead.

Another option, which I am surprised your network isn't using, is a name service (such as DNS). In that setup, you don't care about the IP address of the server machine (at least, not directly). Your client only knows the name of the server, and uses that instead. The name service will resolve the IP address, and should be configured to do so, even after the IP address has changed.

Henry

PS... I am moving this topic to the network (sockets and internet protocol) forum for you.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When DHCP is used to assign IP addresses to machines, the default action is to simply pluck a free IP address from a pool of addresses and assign it to the requesting machine (a DHCP client runs on each machine, and they request IP addresses from a DHCP server). Every assigned IP address is "leased" for a defined amount of time. When the lease expires, the client requests a new lease. If you're lucky, the same IP address will be re-assigned. If not, you'll end up with a brand-new IP address. DHCP servers do tend to recycle the IP addresses, but if you had turned the machine off and back on later, you'd probably not benefit from that.

The network administrator has the ability to nail a "permanent" IP address to a given machine by setting up a mapping between the MAC address of the client machine and the assigned permanent IP address. The MAC address is set by default at the factory by the manufacturer of the network card (or equivalent) that the ethernet cable plugs into (or, in the case of WiFi, it's assigned to the radio unit on the client machine). I've worked in a shop where we override those values and tied the MAC address to a corporate asset ID, but in either case, the result is the same. Each network interface on each client or server machine has a unique MAC ID.

That's what's needed to keep a fixed IP. To be able to refer to a machine by a user-friendly IP-independent name, you need Name Resolution Services. Typically, a machine will have recourse to a number of different name resolvers, with a defined priority to avoid possible conflicts. The most famous of these (and usually the highest priority) is the hosts file, which is /etc/hosts on Linux/Unix systems and named "HOSTS" or "LMHOSTS" on Windows machines. The hosts file is a simple text file with the names and IP addresses that should be resolved in it.

The other popular resolver is Domain Name Services (DNS). DNS is provided by one or more servers which listen on UDP* port 53 and return an IP address for the name requested. Obviously, to query DNS, the client needs to know the IP addres of the DNS servers, since unlike DHCP, DNS clients don't simply send out a general broadcast and hope for a response. That sounds like a chicken-and-egg problem, but the IP address(es) of the DNS server(s) are defined when the network interface goes online. They are either supplied as part of a fixed Iocal configuration or passed back from the DHCP server.

One thing that most people don't realize is that name resolution only resolves IP Addresses. DNS doesn't resolve port numbers. When you make a web request, for example, you have to either explicitly supply a port number (for example, 8080) or the client will pick the port number that's common for the protocol being used (80 for HTTP, 443 for HTTPS, and so forth). It's hard-coded into the client application program (browser or whatever).


==
* Usually UDP. In certain cases, TCP protocol will be used instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!