?? ch9.htm
字號:
<HTML><HEAD><TITLE>developer.com - Reference</TITLE><META NAME="Author" CONTENT="developer.com"><META NAME="Keywords" CONTENT="javascript, ActiveX, VRML, Push, channel,push channel, applet, directory, news, community, developer, internet,intranet, java, java applet, J++, javabeans, java products, tools, javatools, JDK, java development kit, java development environment, JIT,JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, API,browser, java documentation, CDF, Common Desktop Format, RDF, ResourceDescription Framework, Netcaster, Netcast, Castanet, meta-language, metadata,solaris, Solaris, SUN, CGI, Perl, Oracle, database, network, Netscape API,Apache, apache, SSL, mSQL, Netscape, Fasttrack, FastTrack,industry standard, ActiveX bridge, web, internet, site, sitedesign, microsoft, active server pages, asp, iis, nt, win95, 95, html, cgi,frontpage, business, corporate, vbscript, javascript, activex, isapi,database, access, sql, server, www, 3.0, 3.01, 3.02, 3.03, 3.04, 4.0,4.0b2, 4.01, 4.03, 4.04, connection, online, commerce, publisher,publishing, publish, webmaster, dynamic html, dhtml, windows 98, netscape,netscape software, shareware, magazines, jobs, japan, free, freeware, freestuff"><META NAME="description" CONTENT="Developer.com is the leading onlineservice for developers. It includes Gamelan, the official directory for Java, as wellas news, information, tutorials, and directories for other Internet technologiesincluding ActiveX, JavaScript, perl, VRML, Java Beans, push technologies, andother Internet and intranet technologies. Developer.com is also the home ofDeveloper Direct, the Professional Developer's Store, where developers canpurchase and download thousands of Web, authoring and other developmentproducts at some of today's lowest prices."><META NAME="distribution" CONTENT="global"><META NAME="resource-type" CONTENT="document"><META NAME="language" CONTENT="en"></HEAD><BODY BACKGROUND="/images/curve_r-wide.gif" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#DD0000" VLINK="#999999" ALINK="#FF0000"><TABLE><TR><TD VALIGN=TOP WIDTH=120><A HREF="/" ><IMG SRC="/images/masthead_r.gif" WIDTH="100" HEIGHT="78" BORDER="0"ALT="Developer.com Logo"></a></TD><TD><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCBAN.REF/DT=null"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCBAN.REF/DT=null" HEIGHT="60" WIDTH="468" ALT="Click here to support our advertisers"><BR><CENTER>Click here to support our advertisers</CENTER></a></TD></TR></TABLE><!--outer table--><TABLE><!--outer table--><TR><!--outer table--><TD VALIGN="TOP"><!--navbar info --><table><tr><td><A HREF="http://www.developerdirect.com/" ><IMG SRC="/images/button_s.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>SHOPPING</b></font></a></td></tr><tr><td><A HREF="/community/jobs/c_jobs.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>JOB BANK</B></font></a></td></tr><tr><td><A HREF="http://www.classifieds2000.com/cgi-cls/display.exe?Developer+computer+searchcomputercategories"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>CLASSIFIEDS</B></font></a></td></tr><tr><td><A HREF="/directories/directories.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>DIRECTORIES</B></font></a></td></tr><tr><td><A HREF="/reference/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>REFERENCE</B></font></a><br><a href="/reference/r_library.html"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">Online Library</a></font></td></tr><tr><td><A HREF="/classroom/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>LEARNING CENTER</B></font></a></td></tr><tr><td><A HREF="/journal/"><IMG SRC="/images/button_n.gif" width="15" height="11" BORDER="0"><B><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">JOURNAL</font></B></a></td></tr><tr><td><A HREF="/news/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>NEWS CENTRAL</b></font></a></td></tr><tr><td><A HREF="/downloads/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>DOWNLOADS</b></font></a></td></tr><tr><td><A HREF="/community/" ><IMG SRC="/images/button_c.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>COMMUNITY</B></font></a></td></tr><tr><td><A HREF="/calendar/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>CALENDAR</b></font></a></td></tr><tr><td><A HREF="/about/"><IMG SRC="/images/button_a.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>ABOUT US</b></a></font></td></tr></table><table><tr><td><form action="/subscribe/list.cgi" method=post ><img src="/images/rule.gif" width="100" height="2"><br><B><FONT FACE="Arial, Helvetica" SIZE="-1">Journal:</b></font><br><input name='email' value='your e-mail' size="11"><input type='hidden' name='state' value='Subscribe'></td></tr><tr><td><input type='submit' value='Subscribe'><br><font size="-1">Get the weekly email highlights from the most popular journal for developers!<br><a href="http://www.developer.com/subscribe.html">Current issue</a><img src="/images/rule.gif" width="100" height="2"><input type='hidden' name='name' value='New Subscriber'></FORM></td></tr></table><!--/navbar--><font size="-1"><A HREF="http://www.developer.com">developer.com</a><BR><A HREF="http://www.developerdirect.com">developerdirect.com</a><BR><A HREF="http://www.htmlgoodies.com">htmlgoodies.com</a><BR><A HREF="http://www.javagoodies.com">javagoodies.com</a><br><A HREF="http://www.jars.com">jars.com</a><BR><a href="http://www.intranetjournal.com">intranetjournal.com</a><br><a href="http://www.javascripts.com">javascripts.com</a><br></font><!--outer table--></TD><!--outer table--><TD VALIGN="TOP"><BLOCKQUOTE><P><img src="/images/header_r.gif" width="440" height="50"><P><MAP NAME ="header_r2.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/author.html" COORDS ="361,0,440,29"><AREA SHAPE ="RECT" HREF ="/reference/basic.html" COORDS ="254,0,360,29"><AREA SHAPE ="RECT" HREF ="/reference/help.html" COORDS ="183,0,251,29"><AREA SHAPE ="RECT" HREF ="/reference/cool.html" COORDS ="100,0,181,29"><AREA SHAPE ="RECT" HREF ="/reference/mybook.html" COORDS ="0,0,100,29"></MAP><MAP NAME ="header_r1.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/personalize.html" COORDS ="339,0,439,29"></MAP><p><font face="Arial,Helvetica" size="-1" color="#006666"><A HREF="/reference/r_library.html"><B>All Categories</A> :</B><b><A HREF="/reference/r_java.html">Java</A></b></font><p> <META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]"><H1><FONT COLOR=#FF0000>Chapter 9</FONT></H1><H1><B><FONT SIZE=5 COLOR=#FF0000>Java Socket Programming</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="#AnIntroductiontoSockets" >An Introduction to Sockets</A><UL><LI><A HREF="#SocketTransmissionModes" >Socket Transmission Modes</A><LI><A HREF="#JavaConnectionOrientedClasses" >Java Connection-Oriented Classes</A><LI><A HREF="#IterativeandConcurrentServers" >Iterative and Concurrent Servers</A><LI><A HREF="#JavaDatagramClasses" >Java Datagram Classes</A><LI><A HREF="#AppletSecurityandSockets" >Applet Security and Sockets</A></UL><LI><A HREF="#ChapterProjectHTTPServerApplication" >Chapter Project: HTTP Server Application and Client Applet</A><UL><LI><A HREF="#ChapterProjectHTTPServeApplication" >Chapter Project: HTTP Server Application and Client Applet</A><LI><A HREF="#BasicWebServer" >Basic Web Server</A><LI><A HREF="#ClientDatagramApplet" >Client Datagram Applet</A><LI><A HREF="#ClientApplet" >Client Applet</A></UL><LI><A HREF="#Summary" >Summary</A></UL><HR><P><FONT COLOR=#000000>To demonstrate full Java client/server appletconnectivity, an applet server is necessary. This chapter initiatesthe development of a Java HTTP server. Before beginning the server,however, you need some background knowledge of socket programming.This chapter begins with a socket overview and is followed byan exploration of Java's socket classes. The remainder of thechapter will delve into constructing a Java HTTP Web server anda client/server applet.</FONT><P>After reading this chapter, you should be able to do the following:<UL><LI><FONT COLOR=#000000>Understand the socket abstraction</FONT><LI><FONT COLOR=#000000>Know the different modes of socket operation</FONT><LI><FONT COLOR=#000000>Have a working knowledge of the HTTP protocol</FONT><LI><FONT COLOR=#000000>Be able to apply the Java socket classes</FONT><LI><FONT COLOR=#000000>Understand applet socket use and limitations</FONT><LI><FONT COLOR=#000000>Comprehend the HTTP Java server</FONT></UL><H2><A NAME="AnIntroductiontoSockets"><FONT SIZE=5 COLOR=#FF0000>AnIntroduction to Sockets</FONT></A></H2><P>The computers on the Internet are connected by the TCP/IP protocol.In the 1980s, the Advanced Research Projects Agency (ARPA) ofthe U.S. government funded the University of California at Berkeleyto provide a UNIX implementation of the TCP/IP protocol suite.What was developed was termed the <I>socket interface</I>, althoughyou might hear it called the Berkeley-socket interface or justBerkeley sockets. Today, the socket interface is the most widelyused method for accessing a TCP/IP network.<P>A socket is nothing more than a convenient abstraction. It representsa connection point into a TCP/IP network, much like the electricalsockets in your home provide a connection point for your appliances.When two computers want to converse, they each use a socket. Onecomputer is termed the server-it opens a socket and listens forconnections. The other computer is termed the client; it callsthe server socket to start the connection. To establish a connection,all that's needed is a destination address and a port number.<P>Each computer in a TCP/IP network has a unique address. <I>Ports</I>represent individual connections within that address. This isanalogous to corporate mail-each person within a company sharesthe same address, but a letter is routed within the company bythe person's name. Each port within a computer shares the sameaddress, but data is routed within each computer by the port number.When a socket is created, it must be associated with a specificport-this is known as binding to a port.<H3><A NAME="SocketTransmissionModes">Socket Transmission Modes</A></H3><P>Sockets have two major modes of operation: <I>connection-oriented</I>and <I>connectionless</I>. Connection-oriented sockets operatelike a telephone; they must establish a connection and a hangup. Everything that flows between these two events arrives inthe same order it was sent. Connectionless sockets operate likethe mail-delivery is not guaranteed, and multiple pieces of mailmay arrive in a different order than they were sent.<P>Which mode to use is determined by an application's needs. Ifreliability is important, then connection-oriented operation isbetter. File servers need to have all their data arrive correctlyand in sequence. If some data was lost, the server's usefulnesswould be invalidated. Some applications-a time server, for example-senddiscrete chunks of data at regular intervals. If the data becamelost, the server would not want the network to retry until thedata was sent. By the time the data arrived, it would be too oldto have any accuracy. When you need reliability, be aware thatit does come with a price. Ensuring data sequence and correctnessrequires extra processing and memory usage; this extra overheadcan slow down the response times of a server.<P>Connectionless operation uses the User Datagram Protocol (UDP).A datagram is a self- contained unit that has all the informationneeded to attempt its delivery. Think of it as an envelope-it
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -