📄 ch18.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 18 -- Networking with Java</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 18</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Networking with Java</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#NetworkBasics" >Network Basics</A>
<UL>
<LI><A HREF="#Addresses" >Addresses</A>
<LI><A HREF="#Protocols" >Protocols</A>
<LI><A HREF="#Ports" >Ports</A>
</UL>
<LI><A HREF="#TheClientServerParadigm" >The Client/Server Paradigm</A>
<LI><A HREF="#Sockets" >Sockets</A>
<UL>
<LI><A HREF="#StreamSockets" >Stream Sockets</A>
<LI><A HREF="#DatagramSockets" >Datagram Sockets</A>
</UL>
<LI><A HREF="#AReusableSocketClass" >A Reusable Socket Class</A>
<LI><A HREF="#Summary" >Summary</A>
<LI><A HREF="#QA" >Q&A</A>
<LI><A HREF="#Workshop" >Workshop</A>
<UL>
<LI><A HREF="#Quiz" >Quiz</A>
<LI><A HREF="#Exercises" >Exercises</A>
</UL>
</UL>
<HR>
<P>
Yesterday you learned what multiplayer games are and why they
are so important. Today you take one more step toward creating
a networked multiplayer game that can be played over the Internet.
I know, you're probably itching to move on and see a full-blown
network game. However, you still need to cover some ground in
regard to what is required behind the scenes to facilitate network
communication in a real game. Trust me, you'll be dealing with
the intricacies of network game programming soon enough.
<P>
In today's lesson, you learn specifically what Java has to offer
in regard to communicating over an Internet network connection.
You begin the lesson by taking a look at some basic concepts surrounding
the structure of the Internet itself. You then move on to what
specific support is provided by the standard Java networking API.
Finally, you conclude the lesson by writing a reusable socket
class that will prove invaluable tomorrow when you write a complete
network game.
<P>
The following topics are covered in today's lesson:
<UL>
<LI>Network basics
<LI>The client/server paradigm
<LI>Sockets
<LI>A reusable socket class
</UL>
<H2><A NAME="NetworkBasics"><B><FONT SIZE=5 COLOR=#FF0000>Network
Basics</FONT></B></A></H2>
<P>
Before you look at what type of network support Java provides,
it's important that you understand some fundamentals about the
structure of the Internet as a network. As you are no doubt already
aware, the Internet is itself a global network of many different
types of computers connected in various ways. With this wide diversity
of both hardware and software all connected together, it's pretty
amazing that the Internet is even functional. The functionality
of the Internet is no accident and has come at no small cost in
terms of planning.
<P>
The only way to guarantee compatibility and reliable communication
across a wide range of different computer systems is to lay out
very strict standards that must be conformed to rigorously. Now,
please understand that I'm not the type of person who typically
preaches conformity, but conformity in one's personal life is
very different from conformity in complex computer networks. When
it comes to computers and communication, I make a pretty big exception
and embrace conformity for all its worth!
<P>
The point is that the only way to allow a wide range of computer
systems to coexist and communicate with each other effectively
is to hammer out some standards. Fortunately, plenty of standards
abound for the Internet, and they share wide support across many
different computer systems. Now that I have (I hope) sold you
on the importance of communication standards, let's take a look
at a few of them.
<H3><A NAME="Addresses"><B>Addresses</B></A></H3>
<P>
One of the first areas of standardization on the Internet was
in establishing a means to uniquely identify each connected computer.
It's not surprising that a technique logically equivalent to traditional
mailing addresses is the one that was adopted; each computer physically
connected to the Internet is assigned an <I>address</I> that uniquely
identifies it. These addresses, also referred to as <I>IP addresses,</I>
come in the form of a 32-bit number that looks like this: <TT><FONT FACE="Courier">243.37.126.82</FONT></TT>.
You're probably more familiar with the symbolic form of IP addresses,
which looks like this: <TT><FONT FACE="Courier">sincity.com</FONT></TT>.
<P>
An <I>IP address</I> is a 32-bit number that uniquely identifies
each computer physically attached to the Internet.
<P>
Addresses provide a unique identifier for each computer connected
to the Internet. Each Internet computer has an address for the
same reason you have a mailing address and a phone number at your
home: to facilitate communication. It might sound simple, and
that's because conceptually it is. As long as you can guarantee
that each computer is uniquely identifiable, you can easily communicate
with any computer without worry. Well, almost. The truth is that
addresses are only a small part of the Internet communication
equation, but an important part nevertheless. Without addresses,
there would be no way to distinguish among different computers.
<H3><A NAME="Protocols"><B>Protocols</B></A></H3>
<P>
The idea of communicating among different computers on the Internet
might not sound like a big deal now that you understand that they
use addresses similar to mailing addresses. The problem is that
there are many different types of communication that can take
place on the Internet, meaning that there must be an equal number
of mechanisms for facilitating them. It's at this point that the
mailing-address comparison to Internet addressing breaks down.
The reason for this is that each type of communication taking
place on the Internet requires a unique protocol. Your mail address
essentially revolves around one type of communication: the mailman
driving up to your mailbox and placing the mail inside.
<P>
A <I>protocol</I> is a set of rules and standards defining a certain
type of Internet communication.
<P>
A protocol specifies the format of data being sent over the Internet,
along with how and when it is sent. On the other end of the communication,
the protocol also defines how the data is received along with
its structure and what it means. You've probably heard mention
of the Internet just being a bunch of bits flying back and forth
in cyberspace. That's a very true statement, and without protocols
those bits wouldn't mean anything.
<P>
The concept of a protocol is not groundbreaking or even new; you
use protocols all the time in everyday situations, but you just
don't call them protocols. Think about how many times you've been
involved in this type of dialog:
<P>
"Hi, may I take your order?"
<P>
"Yes, I'd like the shrimp special and a soda."
<P>
"Thanks, I'll put your order in and bring you your drink."
<P>
"Thank you, I sure am hungry."
<P>
Although this conversation might not look like anything special,
it is a very definite social protocol used to place orders for
food at a restaurant. Conversational protocol is important because
it gives us familiarity and confidence in knowing what to do in
certain situations. Haven't you ever been nervous when entering
a new social situation in which you don't quite know how to act?
In these cases, you don't really have confidence in the protocol,
so you worry about a communication problem that could easily result
in embarrassment. For computers and networks, protocol breakdown
translates into errors and information transfer failure rather
than embarrassment.
<P>
Now that you understand the importance of protocols, let's take
a look at a couple of the more important ones used on the Internet.
Without a doubt, the protocol getting the most attention these
days is HTTP, which stands for hypertext transfer protocol. HTTP
is the protocol used to transfer HTML documents on the Web. Another
important protocol is FTP, which stands for file transfer protocol.
FTP is a more general protocol used to transfer binary files over
the Internet. These two protocols both have their own unique set
of rules and standards defining how information is transferred,
and Java provides support for both of them.
<P>
<I>HTTP</I>, which stands for Hypertext Transfer Protocol, is
the protocol used to transfer HTML documents on the Web.
<H3><A NAME="Ports"><B>Ports</B></A></H3>
<P>
Internet protocols make sense only in the context of a service.
For example, the HTTP protocol comes into play when you are providing
Web content (HTML pages) through an HTTP service. Each computer
on the Internet has the capability to provide a variety of services
through the various protocols supported. There is a problem, however,
in that the type of service must be known before information can
be transferred. This is where ports come in. A <I>port</I> is
a software abstraction that provides a means to differentiate
between different services. More specifically, a port is a 16-bit
number identifying the different services offered by a network
server.
<P>
A <I>port</I> is a 16-bit number that identifies each service
offered by a network server.
<P>
Each computer on the Internet has a bunch of ports that can be
assigned different services. To use a particular service and therefore
establish a line of communication via a particular protocol, you
must connect to the correct port. Ports are numbered, and some
of the numbers are specifically associated with a type of service.
Ports with specific service assignments are known as standard
ports, meaning that you can always count on a particular port
corresponding to a certain service. For example, the FTP service
is located on port 21, so any other computer wanting to perform
an FTP file transfer would connect to port 21 of the host computer.
Likewise, the HTTP service is located on port 80, so any time
you access a Web site, you are really connecting to port 80 of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -