📄 运用异步输入输出流编写socket进程通信3.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> </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> </TD>
<TD bgColor=#b3befd vAlign=bottom width=191> </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> 站内文章搜索:<BR> <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%"> </TD>
<TD width="91%"> </TD></TR>
<TR>
<TD bgColor=#eeeeee height=257> </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%"> </TD>
<TD height=19 width="95%"> </TD>
<TD rowSpan=5 width=0%> </TD></TR>
<TR>
<TD align=left class=td1 height=33 vAlign=center><A
href="http://www.5xsoft.com/">化境编程界</A> -> <A
href="http://www.5xsoft.com/menu/index0.htm">技术文章 </A>-> <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>[ 作者: 杨健
添加时间: 2001-12-3 10:26:31 ]<BR></TD></TR>
<TR>
<TD align=left class=td1 height=151
vAlign=top><BR>byteBuffer.flip();<BR>
String result = this.decode( byteBuffer );<BR>
//当客户端发出”@exit”退出命令时,关闭其通道<BR>
if ( result.indexOf( "@exit" ) >= 0 ) {<BR>
channel.close();<BR>
}<BR> else {<BR>
clientInstance.append(
result.toString() );<BR>
//读入一行完毕,执行相应操作<BR>
if ( result.indexOf( "\n" ) >= 0 ){<BR>
System.out.println("client input"+result);<BR>
clientInstance.execute();<BR>
}<BR>
}<BR> }<BR>
//该类封装了怎样对客户端的通道进行操作,具体实现可以通过重载execute()方法<BR>
public class ClientChInstance {<BR>
SocketChannel channel;<BR>
StringBuffer buffer=new StringBuffer();<BR>
public ClientChInstance( SocketChannel channel )
{<BR> this.channel =
channel;<BR> }<BR>
public void execute() throws IOException {<BR>
String message = "This is response after
reading from channel!";<BR>
writeToChannel( this.channel, message );<BR>
buffer = new StringBuffer();<BR>
}<BR>
//当一行没有结束时,将当前字窜置于缓冲尾<BR> public
void append( String values ) {<BR>
buffer.append( values );<BR>
}<BR> }<BR><BR><BR> //主程序<BR>
public static void main( String[] args ) {<BR>
NBlockingServer nbServer = new
NBlockingServer(8000);<BR> try {<BR>
nbServer.initialize();<BR>
} catch ( Exception e ) {<BR>
e.printStackTrace();<BR>
System.exit( -1 );<BR>
}<BR> try {<BR>
nbServer.portListening();<BR>
}<BR> catch (
Exception e ) {<BR>
e.printStackTrace();<BR> }<BR>
}<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>
<!--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>© 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 + -