Of course, in order to distinguish a machine from another, and to ensure that it is connected to the desired machine, there must be a mechanism to uniquely identify each machine within the network. The early network only resolved how to provide a unique name for the machine in the local network environment. But Java is geared towards the entire Internet, which requires a mechanism to identify machines from around the world. To achieve this goal, we have adopted the concept of IP (Internet address). IP exists in two different forms:
(1) The most familiar form of DNS (Domain Name service). My own domain name is bruceeckel.com. So suppose I have a computer named Opus in my domain, and its domain name can be Opus.bruceeckel.com. This is the name that everyone uses to send e-mail to others and is usually integrated into a World Wide Web (WWW) address.
(2) In addition, the "four-point" format, which is divided into four sets of numbers, such as 202.98.32.111, is also used.
In either case, the IP address is internally expressed as a number consisting of 32 bits (the annotation ①), so that each number of IP addresses cannot exceed 255. Using the static Inetaddress.getbyname () provided by java.net, we can have a particular Java object Express any of these forms of numbers. The result is an object of type inetaddress, which can be used to form a socket, which everyone sees later.
①: This means that you can get up to 4 billion of the digital combination, and people around the world will soon run out of it. However, according to the new IP address scheme currently under study, it will take the number of 128 bit, so that the unique IP addresses may not be used up in hundreds of years.
As a simple example of using Inetaddress.getbyname (), consider what happens if you have a dial-up connection ISPs (ISP). A temporary IP address is assigned each time a dial-up connection occurs. But during the connection, that IP address has the same validity as other IP addresses on the Internet. If someone connects your machine to your IP address, they may be able to use a Web or FTP server program that runs on your machine. Of course there is a premise that the other side must know exactly what IP you are currently assigned to. Because each dial-up connection to obtain the IP is random, how to accurately master your IP?
The following program uses Inetaddress.getbyname () to generate your IP address. In order for it to run, you must know the name of the computer beforehand. The program is only tested in Windows 95, but you can go to your start, settings, Control Panel, network, and then go to the identification card. where "computer name" is what should be entered at the command line.
: Whoami.java
//finds out your network address as you ' re
//connected to the Internet.
Package C15;
Import java.net.*;
public class WhoAmI {public
static void Main (string[] args)
throws Exception {
if (args.length!= 1) {
Sy Stem.err.println (
"Usage:whoami machinename");
System.exit (1);
}
InetAddress a =
inetaddress.getbyname (args[0]);
System.out.println (a);
}
///:~
In my own case, the name of the machine is called "Colossus" (from the movie "The Giant"). I have a big hard drive on this machine. So once you connect to my ISP, execute the program as follows:
Java WhoAmI Colossus
The results are as follows (of course, this address may be different each time):
colossus/202.98.41.151
If I tell a friend about this address, he can log on to my personal Web server immediately, just specify the destination address http://202.98.41.151 (of course, I can't get disconnected at this point). Sometimes it's a handy way to send a message to someone else or test it before it is officially published on your own web site.