<html>
<!-- This panel generated from the developerWorks XML tag set for tutorials, Version 2.0.0. (C) Copyright 2000, 2001 IBM Corporation. All rights reserved. -->
<head><META http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>一个简单示例</title>
<style type="text/css"><!--
body { color: #000000; background-color: #ffffff; }
dt { font-weight: bold; }
code { font-family: Courier, monospace; font-size: +2; }
titleCode { font-family: Courier, monospace; font-size: +4; }
ol.alpha { list-style-type: upper-alpha; }
span.center { text-align: center; }
span.right { text-align: right; }
span.constant { color: #990099; }
span.keyword { color: #993333; }
span.function-name { color: #666633; }
span.comment { color: #666666; }
span.string { color: #0000ff; }
span.variable-name { color: #000066; }
span.type { color: #006600; }
span.spacer { font-size: 1pt; }
.small { font-family: Verdana, sans-serif; font-size: 8pt }
.divider { color: #999999; font-family: Verdana, Arial, sans-serif; font-weight: normal }
a.mainlink:link { text-decoration: none; color: #ffffff; font-family: Verdana, Arial, sans-serif; font-weight: bold; font-size: 8pt }
a.mainlink:visited { text-decoration: none; color: #ffffff; font-family: Verdana, Arial, sans-serif; font-weight: bold; font-size: 8pt }
a.mainlink:hover { text-decoration: underline; color: #ffffff; font-family: Verdana, Arial, sans-serif; font-weight: bold; font-size: 8pt }
.hbg { background-color : #006699; background-image: url(../i/lines.gif) ;background-repeat: repeat; }
.tbg { background-color : #006699; background-image: url(../i/gr760.gif); }
.tbgc { background-color : #006699 }
.bbg { background-color : #000000; vertical-align: middle }
.dbg { background-color : #6699cc }
.mbg { background-color : #99ccff }
.lbg { background-color : #cce5ff }
-->
</style>
</head>
<body bgcolor="ffffff">
<script language="javascript">
<!-- var emailAbstract="本教程将告诉您什么是套接字(socket)并教您如何在 Java 程序中使用它。通过实际分析几个示例,从单个客户机/服务器通讯到池中的一组客户机访问服务器,您将学到如何用套接字处理现实世界中碰到的典型情形。";
var justTitle="Java 套接字(socket)101";
var tutorialPrereqs="http://www-106.ibm.com/developerWorks/education/r-jsock.html";
var menu1blurb="教程提示";
var menu2blurb="套接字基础";
var menu3blurb="一个秘密的套接字";
var menu4blurb="一个简单示例";
var menu5blurb="一个多线程的示例";
var menu6blurb="一个带有连接池的示例";
var menu7blurb="现实生活中的套接字";
var menu8blurb="总结";
var menu9blurb="附录";
var browser = "x";
if (navigator.userAgent.indexOf("Mozilla/4") != -1) browser = "N3";
else if (navigator.userAgent.indexOf("Mozilla/3") != -1) browser = "N3";
else browser = "x";
if (browser=="N3")
{
var menu1over=new Image(108,68);
var menu1out=new Image(108,68);
var menu2over=new Image(108,68);
var menu2out=new Image(108,68);
var menu3over=new Image(108,68);
var menu3out=new Image(108,68);
var menu4over=new Image(108,68);
var menu4out=new Image(108,68);
var menu5over=new Image(108,68);
var menu5out=new Image(108,68);
var menu6over=new Image(108,68);
var menu6out=new Image(108,68);
var menu7over=new Image(108,68);
var menu7out=new Image(108,68);
var menu8over=new Image(108,68);
var menu8out=new Image(108,68);
var menu9over=new Image(108,68);
var menu9out=new Image(108,68);
var topmainover=new Image(77,15);
var topmainout=new Image(77,15);
var bottommainover=new Image(77,15);
var bottommainout=new Image(77,15);
var topsectionover=new Image(98,15);
var topsectionout=new Image(98,15);
var bottomsectionover=new Image(98,15);
var bottomsectionout=new Image(98,15);
var topfeedbackover=new Image(80,15);
var topfeedbackout=new Image(80,15);
var bottomfeedbackover=new Image(80,15);
var bottomfeedbackout=new Image(80,15);
var toppreviousover=new Image(77,15);
var toppreviousout=new Image(77,15);
var bottompreviousover=new Image(77,15);
var bottompreviousout=new Image(77,15);
var topnextover=new Image(60,15);
var topnextout=new Image(60,15);
var bottomnextover=new Image(60,15);
var bottomnextout=new Image(60,15);
var topnextsectionover=new Image(108,15);
var topnextsectionout=new Image(108,15);
var bottomnextsectionover=new Image(108,15);
var bottomnextsectionout=new Image(108,15);
menu1over.src="imagemaster/himenu1.jpg";
menu1out.src="imagemaster/menu1.jpg";
menu2over.src="imagemaster/himenu2.jpg";
menu2out.src="imagemaster/menu2.jpg";
menu3over.src="imagemaster/himenu3.jpg";
menu3out.src="imagemaster/menu3.jpg";
menu4over.src="imagemaster/himenu4.jpg";
menu4out.src="imagemaster/menu4.jpg";
menu5over.src="imagemaster/himenu5.jpg";
menu5out.src="imagemaster/menu5.jpg";
menu6over.src="imagemaster/himenu6.jpg";
menu6out.src="imagemaster/menu6.jpg";
menu7over.src="imagemaster/himenu7.jpg";
menu7out.src="imagemaster/menu7.jpg";
menu8over.src="imagemaster/himenu8.jpg";
menu8out.src="imagemaster/menu8.jpg";
menu9over.src="imagemaster/himenu9.jpg";
menu9out.src="imagemaster/menu9.jpg";
var mainblurb="主菜单";
var sectionblurb="章节菜单";
var feedbackblurb="提交反馈意见";
var previousblurb="上一屏";
var nextblurb="下一屏";
var nextsectionblurb="下一章";
topmainover.src="../i/h-main.gif";
topmainout.src="../i/main.gif";
bottommainover.src="../i/h-main.gif";
bottommainout.src="../i/main.gif";
topsectionover.src="../i/h-section.gif";
topsectionout.src="../i/section.gif";
bottomsectionover.src="../i/h-section.gif";
bottomsectionout.src="../i/section.gif";
topfeedbackover.src="../i/h-feedback.gif";
topfeedbackout.src="../i/feedback.gif";
bottomfeedbackover.src="../i/h-feedback.gif";
bottomfeedbackout.src="../i/feedback.gif";
toppreviousover.src="../i/h-previous.gif";
toppreviousout.src="../i/previous.gif";
bottompreviousover.src="../i/h-previous.gif";
bottompreviousout.src="../i/previous.gif";
topnextover.src="../i/h-next.gif";
topnextout.src="../i/next.gif";
bottomnextover.src="../i/h-next.gif";
bottomnextout.src="../i/next.gif";
topnextsectionout.src="../i/nextsection.gif";
topnextsectionover.src="../i/h-nextsection.gif";
bottomnextsectionout.src="../i/nextsection.gif";
bottomnextsectionover.src="../i/h-nextsection.gif";
}
function iOut(image)
{
if (browser=="N3")document[image].src=eval(image + "out.src");
}
function iOver(image)
{
if (browser=="N3")document[image].src=eval(image + "over.src");
}
// -->
</script><script src="../i/emailfriend.js" language="javascript"></script><a href="#navskip"><img border="0" alt="Skip to main content" src="../i/c.gif"></a>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td class="tbgc" width="160"><a href="/developerWorks/cgi-bin/click.cgi?url=http://www.ibm.com/cn"><img height="47" width="160" alt="IBM" border="0" src="../i/ibm-logo.gif"></a></td><td class="tbg" width="195"><font color="#99ccff" size="2"><b>China</b></font><a href="#main"><img alt="Skip to main content" height="1" width="195" border="0" src="../i/c.gif"></a></td><td valign="top" align="right" class="t