⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0163.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0162.html">Prev</a> | <a href="tij0164.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
Identifying
a machine
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Of
course, in order to tell one machine from another and to make sure that you are
connected with the machine you want, there must be some way of uniquely <A NAME="Index2614"></A>identifying
machines on a network. Early networks were satisfied to provide unique names
for machines within the local network. However, Java works within the Internet,
which requires a way to uniquely identify a machine from all the others 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>in
the world
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This is accomplished with the <A NAME="Index2615"></A><A NAME="Index2616"></A><A NAME="Index2617"></A>IP
(Internet Protocol) address that can exist in two forms:
</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	The
familiar <A NAME="Index2618"></A><A NAME="Index2619"></A>DNS
(Domain Name Service) form. My domain name is 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>bruceeckel.com</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
so suppose I have a computer called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Opus</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in my domain. Its domain name would be 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Opus.bruceeckel.com</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This is exactly the kind of name that you use when you send email to people,
and is often incorporated into a World-Wide-Web address.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	Alternatively,
you can use the &#8220;<A NAME="Index2620"></A><A NAME="Index2621"></A>dotted
quad&#8221; form, which is four numbers separated by dots, such as 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>123.255.28.120</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
both cases, the IP address is represented internally as a 32-bit number
</FONT><A NAME="fnB63" HREF="#fn63">[63]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(so each of the quad numbers cannot exceed 255), and you can get a special Java
object to represent this number from either of the forms above by using the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InetAddress.getByName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method that&#8217;s in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.net</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The result is an object of type 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InetAddress</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that you can use to build a &#8220;socket&#8221; as you will see later.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
a simple example of using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InetAddress.getByName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
consider what happens if you have a dial-up Internet service provider (ISP).
Each time you dial up, you are assigned a temporary IP address. But while
you&#8217;re connected, your IP address has the same validity as any other IP
address on the Internet. If someone connects to your machine using your IP
address then they can connect to a Web server or FTP server that you have
running on your machine. Of course, they need to know your IP address, and
since it&#8217;s assigned each time you dial up, how can you find out what it is?
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
following program uses 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InetAddress.getByName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to produce your IP address. To use it, you must know the name of your computer.
It has been tested only on Windows 95, but there you can go to
&#8220;Settings,&#8221; &#8220;Control Panel,&#8221; &#8220;Network,&#8221; and
then select the &#8220;Identification&#8221; tab. &#8220;Computer name&#8221;
is the name to put on the command line.
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: WhoAmI.java</font>
<font color="#009900">// Finds out your network address when you're </font>
<font color="#009900">// connected to the Internet.</font>
<font color="#0000ff">package</font> c15;
<font color="#0000ff">import</font> java.net.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> WhoAmI {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) 
      <font color="#0000ff">throws</font> Exception {
    <font color="#0000ff">if</font>(args.length != 1) {
      System.err.println(
        "Usage: WhoAmI MachineName");
      System.exit(1);
    }
    InetAddress a = 
      InetAddress.getByName(args[0]);
    System.out.println(a);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
my case, the machine is called &#8220;Colossus&#8221; (from the movie of the
same name, because I keep putting bigger disks on it). So, once I&#8217;ve
connected to my ISP I run the program:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">java
WhoAmI Colossus
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">I
get back a message like this (of course, the address is different each time):
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Colossus/199.190.87.75</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
I tell my friend this address, he can log onto my personal Web server by going
to the URL 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>http://199.190.87.75</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(only as long as I continue to stay connected during that session). This can
sometimes be a handy way to distribute information to someone else or to test
out a Web site configuration before posting it to a &#8220;real&#8221; server.
</FONT><a name="_Toc375545493"></a><a name="_Toc408018766"></a><P></DIV>
<A NAME="Heading513"></A><H3 ALIGN=LEFT>
Servers
and clients
</H3>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -