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

📄 tij0163.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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;
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
machine that &#8220;stays in one place&#8221; is called the <A NAME="Index2622"></A><A NAME="Index2623"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>server</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and the one that seeks is called the <A NAME="Index2624"></A><A NAME="Index2625"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>client</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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 just magically turned into an IO
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 IO commands from Chapter 10. This is one of the nice
features of Java networking.
</FONT><P></DIV>
<A NAME="Heading514"></A><H4 ALIGN=LEFT>
Testing
programs without a network
<P><A NAME="Index2626"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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="Index2627"></A><A NAME="Index2628"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>localhost</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to be the <A NAME="Index2629"></A><A NAME="Index2630"></A>&#8220;local
loopback&#8221; IP address for testing without a network. The generic way to
produce this address in Java is:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">InetAddress
addr = InetAddress.getByName(null);
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you hand 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getByName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
it defaults to using the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>localhost</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InetAddress</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InetAddress
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">(but
you can print them out, as you&#8217;ll see in the next example). The only way
you can create an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InetAddress</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is through one of that class&#8217;s 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
member methods 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getByName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(which is what you&#8217;ll usually use), 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getAllByName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getLocalHost(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can also produce the local loopback address by handing it the string 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>localhost</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">InetAddress.getByName("localhost");</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">or
by using its dotted quad form to name the reserved IP number for the loopback:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">InetAddress.getByName("127.0.0.1");</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
three forms produce the same result.
</FONT><a name="_Toc375545494"></a><a name="_Toc408018767"></a><P></DIV>
<A NAME="Heading515"></A><H3 ALIGN=LEFT>
Port:
a unique place 
<P>within
the machine
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>ports</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and when you&#8217;re setting up a client or a server you must choose a <A NAME="Index2631"></A><A NAME="Index2632"></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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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 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. 
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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).
</FONT><a name="_Toc375545495"></a><a name="_Toc408018768"></a><P></DIV>
<HR><DIV ALIGN=LEFT><A NAME="fn63" HREF="#fnB63">[63]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
This means a maximum of just over four billion numbers, which is rapidly
running out. The new standard for IP addresses will use a 128-bit number, which
should produce enough unique IP addresses for the foreseeable future.
</FONT><P></DIV>


<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0162.html">Prev</a> | <a href="tij0164.html">Next</a>
</div>
</body></html>

⌨️ 快捷键说明

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