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

📄 chap15.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:
you find out what it is? 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I13' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I14>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The following program uses
<B>InetAddress.getByName(&#160;)</B> to produce your IP address. To use it, you
must know the name of your computer. On Windows 95/98, 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><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c15:WhoAmI.java</font>
<font color=#009900>// Finds out your network address when</font>
<font color=#009900>// you're connected to the Internet.</font>
<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(
        <font color=#004488>"Usage: WhoAmI MachineName"</font>);
      System.exit(1);
    }
    InetAddress a = 
      InetAddress.getByName(args[0]);
    System.out.println(a);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In this case, the machine is called
&#8220;peppy.&#8221; So, once I&#8217;ve connected to my ISP I run the
program:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>java WhoAmI peppy</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">I get back a message like this (of
course, the address is different each time):</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>peppy/199.190.87.75</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If I tell my friend this address and I
have a Web server running on my computer, he can connect to it by going to the
URL <I>http://199.190.87.75</I> (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.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I14' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I15>
</FONT><A NAME="_Toc375545493"></A><BR></P></DIV>
<A NAME="Heading513"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Servers and clients</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The whole point of a network is to allow
two machines to connect and talk to each other. Once the two machines have found
each other they can have a nice, two-way conversation. But how do they find each
other? It&#8217;s like getting lost in an amusement park: one machine has to
stay in one place and listen while the other machine says, &#8220;Hey, where are
you?&#8221; 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I15' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I16>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The machine that &#8220;stays in one
place&#8221; is called the
<A NAME="Index2053"></A><A NAME="Index2054"></A><I>server</I>, and the one that
seeks is called the
<A NAME="Index2055"></A><A NAME="Index2056"></A><I>client</I>. This distinction
is important only while the client is trying to connect to the server. Once
they&#8217;ve connected, it becomes a two-way communication process and it
doesn&#8217;t matter anymore that one happened to take the role of server and
the other happened to take the role of the client.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I16' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I17>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">So the job of the server is to listen for
a connection, and that&#8217;s performed by the special server object that you
create. The job of the client is to try to make a connection to a server, and
this is performed by the special client object you create. Once the connection
is made, you&#8217;ll see that at both server and client ends, the connection is
magically turned into an I/O stream object, and from then on you can treat the
connection as if you were reading from and writing to a file. Thus, after the
connection is made you will just use the familiar I/O commands from Chapter 11.
This is one of the nice features of Java networking.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I17' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I18>
</FONT><BR></P></DIV>
<A NAME="Heading514"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Testing programs without a network<BR><A NAME="Index2057"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For many reasons, you might not have a
client machine, a server machine, and a network available to test your programs.
You might be performing exercises in a classroom situation, or you could be
writing programs that aren&#8217;t yet stable enough to put onto the network.
The creators of the Internet Protocol were aware of this issue, and they created
a special address called
<A NAME="Index2058"></A><A NAME="Index2059"></A><B>localhost</B> to be the
<A NAME="Index2060"></A><A NAME="Index2061"></A>&#8220;local loopback&#8221; IP
address for testing without a network. The generic way to produce this address
in Java is:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>InetAddress addr = InetAddress.getByName(<font color=#0000ff>null</font>);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you hand <B>getByName(&#160;)</B> a
<B>null</B>, it defaults to using the <B>localhost</B>. The <B>InetAddress</B>
is what you use to refer to the particular machine, and you must produce this
before you can go any further. You can&#8217;t manipulate the contents of an
<B>InetAddress </B>(but you can print them out, as you&#8217;ll see in the next
example). The only way you can create an <B>InetAddress</B> is through one of
that class&#8217;s overloaded <B>static</B> member methods
<B>getByName(&#160;)</B> (which is what you&#8217;ll usually use),
<B>getAllByName(&#160;)</B>, or <B>getLocalHost(&#160;)</B>.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I18' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I19>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can also produce the local loopback
address by handing it the string <B>localhost</B>:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>InetAddress.getByName(<font color=#004488>"localhost"</font>);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">(assuming &#8220;localhost&#8221; is
configured in your machine&#8217;s &#8220;hosts&#8221; table), or by using its
dotted quad form to name the reserved IP number for the
loopback:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>InetAddress.getByName(<font color=#004488>"127.0.0.1"</font>);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All three forms produce the same result.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I19' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I20>
</FONT><A NAME="_Toc375545494"></A><BR></P></DIV>
<A NAME="Heading515"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Port: a unique place <BR>within the machine</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">An IP address isn&#8217;t enough to
identify a unique server, since many servers can exist on one machine. Each IP
machine also contains <I>ports</I>, and when you&#8217;re setting up a client or
a server you must choose a <A NAME="Index2062"></A><A NAME="Index2063"></A>port
where both client and server agree to connect; if you&#8217;re meeting someone,
the IP address is the neighborhood and the port is the bar.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I20' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I21>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The port is not a physical location in a
machine, but a software abstraction (mainly for bookkeeping purposes). The
client program knows how to connect to the machine via its IP address, but how
does it connect to a desired service (potentially one of many on that machine)?
That&#8217;s where the port numbers come in as a second level of addressing. The
idea is that if you ask for a particular port, you&#8217;re requesting the
service that&#8217;s associated with the port number. The time of day is a
simple example of a service. Typically, each service is associated with a unique
port number on a given server machine. It&#8217;s up to the client to know ahead
of time which port number the desired service is running on. 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I21' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I22>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The system services reserve the use of
ports 1 through 1024, so you shouldn&#8217;t use those or any other port that
you know to be in use. The first choice for examples in this book will be port
8080 (in memory of the venerable old 8-bit Intel 8080 chip in my first computer,
a CP/M machine).

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I22' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I23>
</FONT><A NAME="_Toc375545495"></A><A NAME="_Toc481064869"></A><BR></P></DIV>
<A NAME="Heading516"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Sockets</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <I>socket</I> is the software
abstraction used to represent the &#8220;terminals&#8221; of a connection
between two machines. For a given connection, there&#8217;s a socket on each
machine, and you can imagine a hypothetical &#8220;cable&#8221; running between
the two machines with each end of the &#8220;cable&#8221; plugged into a socket.
Of course, the physical hardware and cabling between machines is completely
unknown. The whole point of the abstraction is that we don&#8217;t have to know
more than is necessary. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I23' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I24>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In Java, you create a socket to make the
connection to the other machine, then you get an <B>InputStream</B> and
<B>OutputStream</B> (or, with the appropriate converters, <B>Reader</B> and
<B>Writer</B>)<B> </B>from the socket in order to be able to treat the
connection as an I/O stream object. There are two stream-based socket classes: a
<B>ServerSocket</B> that a server uses to &#8220;listen&#8221; for incoming
connections and a <B>Socket</B> that a client uses in order to initiate a
connection. Once a client makes a socket connection, the <B>ServerSocket</B>
returns (via the <B>accept(&#160;)</B>
<A NAME="Index2064"></A><A NAME="Index2065"></A>method) a corresponding
<B>Socket</B> through which communications will take place on the server side.
From then on, you have a true <B>Socket</B> to <B>Socket</B> connection and you
treat both ends the same way because they <I>are</I> the same. At this point,
you use the methods
<A NAME="Index2066"></A><A NAME="Index2067"></A><B>getInputStream(&#160;)</B>
and
<A NAME="Index2068"></A><A NAME="Index2069"></A><B>getOutputStream(&#160;)</B>
to produce the corresponding <B>InputStream</B> and <B>OutputStream</B> objects
from each <B>Socket</B>. These must be wrapped inside buffers and formatting
classes just like any other stream object described in Chapter 11.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER15_I24' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER15_I25>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The use of the term <B>ServerSocket</B>
would seem to be another example of a confusing naming scheme in the Java
libraries. You might think <B>ServerSocket</B> would be better named
&#8220;ServerConnector&#8221; or something without the word &#8220;Socket&#8221;
in it. You might also think that <B>ServerSocket</B> and <B>Socket</B> should
both be inherited from some common base class. Indeed, the two classes do have
several methods in common, but not enough to give them a common base class.
Instead, <B>ServerSocket</B>&#8217;s job is to wait until some other machine
connects to it, then to return an actual <B>Socket</B>. This is why
<B>ServerSocket</B> seems to be a bit misnamed, since its job isn&#8217;t really

⌨️ 快捷键说明

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