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

📄 运用异步输入输出流编写socket进程通信3.htm

📁 java非阻塞io编程
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0050)http://www.5xsoft.com/data/200112/0310225001_2.htm -->
<HTML><HEAD><TITLE>化境编程界</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<STYLE type=text/css>TD {
	COLOR: #222222; FONT-SIZE: 9pt
}
.tx1 {
	BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; FONT-SIZE: 9pt; WIDTH: 80px
}
.bt {
	BACKGROUND-COLOR: #f3eae9; BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; CURSOR: hand; FONT-SIZE: 9pt; HEIGHT: 18px; WIDTH: 40px
}
</STYLE>

<STYLE type=text/css>.td1 {
	BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: 0px; BORDER-RIGHT: 0px; BORDER-TOP: 0px; LINE-HEIGHT: 18px
}
.td2 {
	BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; BORDER-RIGHT: #cccccc 1px solid; BORDER-TOP: 0px; COLOR: #333333; LINE-HEIGHT: 18px
}
.td3 {
	BORDER-BOTTOM: 0px; BORDER-LEFT: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; BORDER-TOP: 0px; COLOR: #333333; LINE-HEIGHT: 16px
}
</STYLE>

<STYLE type=text/css>A {
	COLOR: #000000; TEXT-DECORATION: none
}
A:hover {
	COLOR: #ff3300; TEXT-DECORATION: underline
}
</STYLE>

<SCRIPT language=javascript src="运用异步输入输出流编写Socket进程通信3.files/new.js"></SCRIPT>

<SCRIPT language=javascript>
function proc_search()
{
var obj;
if(!window.search.value)
{
alert('请输入要搜索的内容');
return;
}
location.href='http://www.5xsoft.com/search.asp?type=0&search='+window.search.value;
}
</SCRIPT>

<STYLE type=text/css>A.a1 {
	COLOR: #ffffff; TEXT-DECORATION: none
}
A.a1:hover {
	COLOR: #ff9900; TEXT-DECORATION: underline
}
</STYLE>

<STYLE type=text/css>.td4 {
	BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; BORDER-RIGHT: medium none; BORDER-TOP: medium none; LINE-HEIGHT: 16px
}
</STYLE>

<META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
<BODY bgColor=#b3befd bottomMargin=0 leftMargin=0 rightMargin=0 topMargin=0>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 height=16 width=778>
  <TBODY>
  <TR>
    <TD align=left height=16 width=415>&nbsp;</TD>
    <TD align=left bgColor=#000000 width=46><IMG height=16 
      src="运用异步输入输出流编写Socket进程通信3.files/angle.gif" width=40></TD>
    <TD align=middle bgColor=#000000 width=308><FONT color=#ffffff><A class=a1 
      href="http://www.5xsoft.com/menu/index2.htm">化境作品</A> | <A class=a1 
      href="http://www.5xsoft.com/about.htm">关于作者</A> | <A class=a1 
      href="http://www.5xsoft.com/menu/index0.htm">技术文章</A> | <A class=a1 
      href="http://bbs.5xsoft.com/">留言板</A></FONT></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=778>
  <TBODY>
  <TR align=middle>
    <TD bgColor=#b3befd height=60 vAlign=bottom width=110><IMG height=60 
      src="运用异步输入输出流编写Socket进程通信3.files/5xsoft.gif" width=110></TD>
    <TD width=468>&nbsp;</TD>
    <TD bgColor=#b3befd vAlign=bottom width=191>&nbsp;</TD></TR>
  <TR align=left>
    <TD colSpan=3 height=5></TD></TR></TBODY></TABLE>
<TABLE bgColor=#ffffff border=0 cellPadding=0 cellSpacing=0 height=1 
width="100%">
  <TBODY>
  <TR>
    <TD></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 height=332 width=778>
  <TBODY>
  <TR>
    <TD bgColor=#eeeeee vAlign=top width=170>
      <TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
        <TBODY>
        <TR bgColor=#b3befd vAlign=top>
          <TD colSpan=2 height=65><BR>&nbsp;站内文章搜索:<BR>&nbsp; <INPUT class=tx1 
            id=search name=search> <INPUT class=bt name=button onclick=proc_search(); style="WIDTH: 40px" type=button value=搜索> 
          </TD></TR>
        <TR>
          <TD width="9%">&nbsp;</TD>
          <TD width="91%">&nbsp;</TD></TR>
        <TR>
          <TD bgColor=#eeeeee height=257>&nbsp;</TD>
          <TD class=td4 vAlign=top><STRONG>相关内容: </STRONG><BR>- <A 
            href="http://www.5xsoft.com/data/200203/2914384501.htm" 
            title="J2EE 组件开发:会话EJB">J2EE 组件开发:会话EJB</A><BR>- <A 
            href="http://www.5xsoft.com/data/200202/0508330901.htm" 
            title=面向对象编程:Java中的抽象数据类型>面向对象编程:Java中的抽象数据类型</A><BR>- <A 
            href="http://www.5xsoft.com/data/200202/0416042401.htm" 
            title="Sun为Web Service提供Java工具">Sun为Web Service提供Java工具</A><BR>- <A 
            href="http://www.5xsoft.com/data/200201/1808575201.htm" 
            title=关于EJB返回值的解决方案>关于EJB返回值的解决方案</A><BR>- <A 
            href="http://www.5xsoft.com/data/200201/1808413601.htm" 
            title=J2EE中的设计模式>J2EE中的设计模式</A><BR></TD></TR></TBODY></TABLE></TD>
    <TD align=left bgColor=#ffffff class=td3 vAlign=top width=608>
      <TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
        <TBODY>
        <TR>
          <TD rowSpan=5 width="5%">&nbsp;</TD>
          <TD height=19 width="95%">&nbsp;</TD>
          <TD rowSpan=5 width=0%>&nbsp;</TD></TR>
        <TR>
          <TD align=left class=td1 height=33 vAlign=center><A 
            href="http://www.5xsoft.com/">化境编程界</A> -&gt; <A 
            href="http://www.5xsoft.com/menu/index0.htm">技术文章 </A>-&gt; <A 
            href="http://www.5xsoft.com/menu/index0_4_1.htm">JAVA/JSP </A></TD></TR>
        <TR>
          <TD align=middle class=td1 height=33 vAlign=center><FONT 
            size=+1><B><BR>运用异步输入输出流编写Socket进程通信(3) </B></FONT><BR><BR>[ 作者: 杨健 
            &nbsp; 添加时间: 2001-12-3 10:26:31 ]<BR></TD></TR>
        <TR>
          <TD align=left class=td1 height=151 
            vAlign=top><BR>byteBuffer.flip();<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;String result = this.decode( byteBuffer );<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;//当客户端发出”@exit”退出命令时,关闭其通道<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;if ( result.indexOf( "@exit" ) &gt;= 0 ) {<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;channel.close();<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;else {<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;clientInstance.append( 
            result.toString() );<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;//读入一行完毕,执行相应操作<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp;if ( result.indexOf( "\n" ) &gt;= 0 ){<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;System.out.println("client input"+result);<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;clientInstance.execute();<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp;}<BR>&nbsp; 
            &nbsp;//该类封装了怎样对客户端的通道进行操作,具体实现可以通过重载execute()方法<BR>&nbsp; 
            &nbsp;public class ClientChInstance {<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;SocketChannel channel;<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;StringBuffer buffer=new StringBuffer();<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;public ClientChInstance( SocketChannel channel ) 
            {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.channel = 
            channel;<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;public void execute() throws IOException {<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;String message = "This is response after 
            reading from channel!";<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;writeToChannel( this.channel, message );<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;buffer = new StringBuffer();<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;//当一行没有结束时,将当前字窜置于缓冲尾<BR>&nbsp; &nbsp; &nbsp; &nbsp;public 
            void append( String values ) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;buffer.append( values );<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp;}<BR><BR><BR>&nbsp; &nbsp;//主程序<BR>&nbsp; 
            &nbsp;public static void main( String[] args ) {<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;NBlockingServer nbServer = new 
            NBlockingServer(8000);<BR>&nbsp; &nbsp; &nbsp; &nbsp;try {<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nbServer.initialize();<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;} catch ( Exception e ) {<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace();<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;System.exit( -1 );<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;try {<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;nbServer.portListening();<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;catch ( 
            Exception e ) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;e.printStackTrace();<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; 
            &nbsp;}<BR>}<BR><BR>程序清单1<BR><BR>小结: 
            <BR>从以上程序段可以看出,服务器端没有引入多余线程就完成了多客户的客户/服务器模式。该程序中使用了回调模式(CALLBACK),细心的读者应该早就看出来了。需要注意的是,请不要将原来的输入输出包与新加入的输入输出包混用,因为出于一些原因的考虑,这两个包并不兼容。即使用通道时请使用缓冲完成输入输出控制。该程序在Windows2000,J2SE1.4下,用telnet测试成功。<BR><BR>参考文献: 
            <BR><BR>《JavaTM 2 Platform, Standard Edition, v 1.4.0 API 
            Specification》<BR><BR>关于作者 
            <BR>杨健,中南工业大学计算机科学与技术专业硕士研究生,参与过大型MIS系统开发,网站建设等,现在正致力于Java技术的研究。Email:ytjcopy@china.com。 
            <BR><BR></TD></TR>
        <TR>
          <TD align=middle class=td4 height=48 vAlign=center><A 
            href="http://www.5xsoft.com/data/200112/0310225001_1.htm"><FONT 
            face=Webdings>7</FONT> 上一页</A> &nbsp; &nbsp; 
        <!--nextpage--></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 height=58 width=778>
  <TBODY>
  <TR>
    <TD align=middle height=58 width=784><BR>&copy; Copyright 化境编程界 稻香老农 2000.4 - 
      2002.12 | OICQ:593737 | Email: 5xsoft@21cn.com <IMG border=0 
      src="运用异步输入输出流编写Socket进程通信3.files/top.htm"></TD></TR></TBODY></TABLE></BODY></HTML>

⌨️ 快捷键说明

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