zhangxl_blog
路漫漫其修遠兮,吾將上下而求索!
BlogJava
首頁
新隨筆
新文章
聯(lián)系
聚合
管理
posts - 28, comments - 15, trackbacks - 0
Java NIO Demo
在讀Amoeba源碼的時候,里面采用java NIO進行通信管理,以前也了解過一些關于這方面的知識但是都不太系統(tǒng),最近兩天抽時間對這塊進行一下掃盲。我主要參考以下兩篇文章,個人認為這兩篇文章還是不錯的入門級文章,講的比較通俗易懂。
1.
http://www.ibm.com/developerworks/cn/education/java/j-nio/section11.html
比較系統(tǒng)的講述了Channel(通道)、Buffer(緩沖區(qū))、position,limit,
capacity
in buffer等;其示例代碼在:
http://code.google.com/p/astudy/source/browse/trunk/applications/astudy/nio/MultiPortEcho.java?spec=svn141&r=141
下
2.
http://tutorials.jenkov.com/java-nio/index.html
這個站點也是一個不錯的入門級別介紹,雖然是e文,但講解的比較細致。
3.我的demo
這個小例子,模擬了一個echo服務,客戶端向echo服務器發(fā)送一段信息,echo收到信息后,返回給客戶端,然后,連接關閉。代碼如下:
/** */
/**
************************************
*/
客戶端代碼:
package
com.zxl.channel;
import
java.io.IOException;
import
java.net.InetSocketAddress;
import
java.nio.ByteBuffer;
import
java.nio.channels.SelectionKey;
import
java.nio.channels.Selector;
import
java.nio.channels.SocketChannel;
import
java.nio.charset.Charset;
import
java.util.Set;
public
class
EchoClient
{
/** */
/**
*
@param
args
*
@throws
IOException
*/
public
static
void
main(String[] args)
throws
IOException
{
SocketChannel channel
=
SocketChannel.open();
channel.configureBlocking(
false
);
InetSocketAddress s
=
new
InetSocketAddress(
"
localhost
"
,
2000
);
channel.connect(s);
Selector selector
=
Selector.open();
channel.register(selector, SelectionKey.OP_CONNECT
|
SelectionKey.OP_READ);
Charset charset
=
Charset.forName(
"
GBK
"
);
boolean
isFinished
=
false
;
while
(
!
isFinished)
{
int
num
=
selector.select();
if
(num
>
0
)
{
Set
<
SelectionKey
>
keys
=
selector.selectedKeys();
for
(SelectionKey k:keys)
{
if
(k.isConnectable())
{
SocketChannel sc
=
(SocketChannel) k.channel();
sc.configureBlocking(
false
);
sc.finishConnect();
sc.register(selector, SelectionKey.OP_READ);
ByteBuffer echoBuffer
=
ByteBuffer.allocate(
1024
);
ByteBuffer info
=
charset.encode(
"
好了克隆技術杜洛克防水堵漏開發(fā)!
"
);
echoBuffer.put(info);
echoBuffer.flip();
sc.write(echoBuffer);
echoBuffer.clear();
}
else
if
(k.isValid()
&&
k.isReadable())
{
ByteBuffer echoBuffer
=
ByteBuffer.allocate(
1024
);
SocketChannel sc
=
(SocketChannel) k.channel();
sc.read(echoBuffer);
echoBuffer.flip();
System.out.println(
"
echo server return:
"
+
charset.decode(echoBuffer).toString());
echoBuffer.clear();
isFinished
=
true
;
k.cancel();
sc.close();
selector.close();
}
}
}
}
}
}
/** */
/**
******************************************
*/
服務端代碼:
package
com.zxl.channel;
import
java.io.IOException;
import
java.net.InetSocketAddress;
import
java.net.ServerSocket;
import
java.nio.ByteBuffer;
import
java.nio.channels.SelectionKey;
import
java.nio.channels.Selector;
import
java.nio.channels.ServerSocketChannel;
import
java.nio.channels.SocketChannel;
import
java.nio.charset.Charset;
import
java.util.Iterator;
import
java.util.Set;
public
class
MultiPortEchoServer
{
private
Charset charset
=
Charset.forName(
"
GBK
"
);
private
int
[] ports;
/** */
/**
*
@param
args
*/
public
static
void
main(String[] args)
{
int
[] ps
=
{
2000
,
2001
}
;
//
默認監(jiān)聽2000,2001端口
new
MultiPortEchoServer(ps);
}
public
MultiPortEchoServer(
int
[] ports)
{
this
.ports
=
ports;
try
{
go();
}
catch
(IOException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
public
void
go()
throws
IOException
{
Selector selector
=
Selector.open();
for
(
int
i
=
0
;i
<
ports.length;i
++
)
{
ServerSocketChannel channel
=
ServerSocketChannel.open();
channel.configureBlocking(
false
);
ServerSocket socket
=
channel.socket();
InetSocketAddress address
=
new
InetSocketAddress(
"
localhost
"
,ports[i]);
socket.bind(address);
//
注冊接受連接事件
channel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println(
"
Going to listen on
"
+
ports[i] );
}
while
(
true
)
{
int
num
=
selector.select();
Set
<
SelectionKey
>
keys
=
selector.selectedKeys();
Iterator
<
SelectionKey
>
iter
=
keys.iterator();
while
(iter.hasNext())
{
SelectionKey key
=
iter.next();
if
((key.readyOps()
&
SelectionKey.OP_ACCEPT)
==
SelectionKey.OP_ACCEPT)
{
ServerSocketChannel ssc
=
(ServerSocketChannel) key.channel();
SocketChannel sc
=
ssc.accept();
sc.configureBlocking(
false
);
sc.register(selector, SelectionKey.OP_READ);
iter.remove();
}
else
if
((key.readyOps()
&
SelectionKey.OP_READ)
==
SelectionKey.OP_READ)
{
SocketChannel sc
=
(SocketChannel) key.channel();
if
(
!
sc.isOpen())
{
selector
=
Selector.open();
}
else
{
ByteBuffer echoBuffer
=
ByteBuffer.allocate(
1024
);
//
int x = sc.read(echoBuffer);
while
(sc.read(echoBuffer)
>
0
)
{
System.out.println(
"
Echoed
"
+
charset.decode(echoBuffer).toString()
+
"
from
"
+
sc.socket().getInetAddress().getHostAddress() );
echoBuffer.flip();
sc.write(echoBuffer);
echoBuffer.clear();
}
iter.remove();
/**/
/*
返回信息后關閉連接
*/
key.cancel();
sc.close();
}
}
}
keys.clear();
}
}
}
posted on 2011-06-30 16:24
zhangxl
閱讀(2803)
評論(1)
編輯
收藏
所屬分類:
java concurrency
FeedBack:
#
re: Java NIO Demo
2014-11-24 09:47 |
zuidaima
java demo學習實例教程源代碼下載:
http://zuidaima.com/share/kjava-p1-s1.htm
回復
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
使用java nio 實現(xiàn) Ping
Java NIO Demo
Copyright ©2025 zhangxl Powered by:
博客園
模板提供:
滬江博客
<
2014年11月
>
日
一
二
三
四
五
六
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(1)
給我留言
查看公開留言
查看私人留言
隨筆分類
(17)
arithmetics(3)
C/C++(1)
Cache
DB(1)
IOC/AOP(2)
java concurrency(2)
java 多線程
JDK(1)
JVM(1)
Linux(1)
nosql(5)
Performance
隨筆檔案
(28)
2014年7月 (3)
2014年5月 (1)
2014年4月 (1)
2013年6月 (1)
2013年4月 (2)
2013年3月 (1)
2012年8月 (1)
2012年5月 (1)
2012年2月 (6)
2012年1月 (1)
2011年10月 (1)
2011年9月 (1)
2011年6月 (2)
2009年8月 (3)
2008年5月 (1)
2006年4月 (2)
文章分類
(30)
AJAX
common(3)
DB(3)
java tools(1)
JAVA 基礎文章(1)
java 并發(fā)(3)
JDBC(1)
linux(3)
ORM(包括hibernate等)(2)
Spring(6)
SWT、SWING、AWT(2)
web(2)
web service
優(yōu)化(2)
版本控制(1)
文章檔案
(30)
2013年4月 (1)
2013年3月 (1)
2012年1月 (2)
2011年12月 (1)
2011年11月 (1)
2011年8月 (2)
2011年7月 (1)
2011年6月 (4)
2011年4月 (3)
2009年8月 (4)
2008年6月 (1)
2008年4月 (1)
2007年3月 (2)
2006年12月 (1)
2006年10月 (2)
2006年8月 (1)
2006年6月 (1)
2006年4月 (1)
相冊
my picture
收藏夾
(2)
我的關注(2)
hibernate
java基礎
serialization
mysql
mysql debin
xml
IBM XSL
w3c標準的xpath說明
web service ibm
XPath 示例
XPath 簡單語法
XQuery 1.0 and XPath 2.0 Full-Text Use Cases
關注
Doug Lea's Home Page
壓力測試
JMeter使用技巧
算法
排列組合算法
最新隨筆
1.?解決Redis數(shù)據(jù)庫響應延遲問題(轉載)
2.?理想化的 Redis 集群 (轉載)
3.?Redis 分區(qū)(翻譯)
4.?Mysql索引相關知識分享
5.?數(shù)據(jù)結構-BinaryTree
6.?深入學習Linux之命令篇-find
7.?什么情況下應該使用GridFS(翻譯)
8.?Mongodb主從復制實踐
9.?jmap使用
10.?為什么實現(xiàn)了equal方法,一定需要實現(xiàn)hashCode方法呢?
11.?MongoDB學習—MongoDB安裝
12.?京東碰到的一道面試題
13.?HashMap分析
14.?12個小球其中有一個是次品,不過不知道輕重,請問用天平能用三次測量的機會找出那個次品嗎?
15.?使用java nio 實現(xiàn) Ping
16.?Spring 源碼閱讀(IOC容器)-容器啟動2
17.?Spring 源碼閱讀(IOC容器)-容器啟動1
18.?JDBC SavePoint淺析
19.?Linux下C訪問MySQL實踐
20.?Apache Benchmark(ab)使用
21.?Java NIO Demo
22.?Amoeba源碼解讀一
23.?編寫跨平臺代碼注意事項
24.?編寫跨平臺代碼注意事項
25.?JavaScript的toString()方法自動調用
搜索
積分與排名
積分 - 96772
排名 - 600
最新評論
1.?嗯嗯
阿斯達斯
--安德森
2.?re: tomcat 產(chǎn)生heapdump文件配置
如果不內存溢出,heapdump目錄是不是空的?
--小龍在線
3.?re: Java NIO Demo
評論內容較長,點擊標題查看
--zuidaima
4.?re: Redis 分區(qū)(翻譯)
手機賺錢軟件
http://www.szapk.cn
!!!
--手機賺錢軟件http://www.szapk.cn
5.?re: Mysql索引相關知識分享
很有價值的分享,值得學習
--任務大廳
閱讀排行榜
1.?Apache Benchmark(ab)使用(3232)
2.?Linux下C訪問MySQL實踐(3143)
3.?Java NIO Demo(2803)
4.?創(chuàng)建mysql innodb數(shù)據(jù)庫(2642)
5.?JDBC SavePoint淺析(2541)
評論排行榜
1.?Mysql索引相關知識分享(4)
2.?京東碰到的一道面試題(1)
3.?Linux下C訪問MySQL實踐(1)
4.?Apache Benchmark(ab)使用(1)
5.?Java NIO Demo(1)
6.?JavaScript的toString()方法自動調用(1)
7.?lucene 實踐(1)
8.?Redis 分區(qū)(翻譯)(1)
9.?解決Redis數(shù)據(jù)庫響應延遲問題(轉載)(0)
10.?理想化的 Redis 集群 (轉載)(0)
11.?dom4j學習筆記(0)
12.?創(chuàng)建mysql innodb數(shù)據(jù)庫(0)
13.?Amoeba源碼解讀一(0)
14.?編寫跨平臺代碼注意事項(0)
15.?編寫跨平臺代碼注意事項(0)
16.?HashMap分析(0)
17.?12個小球其中有一個是次品,不過不知道輕重,請問用天平能用三次測量的機會找出那個次品嗎?(0)
18.?使用java nio 實現(xiàn) Ping(0)
19.?Spring 源碼閱讀(IOC容器)-容器啟動2(0)
20.?Spring 源碼閱讀(IOC容器)-容器啟動1(0)
主站蜘蛛池模板:
无码国产精品一区二区免费式影视
|
国产99在线|亚洲
|
国产亚洲综合色就色
|
亚洲av片劲爆在线观看
|
亚洲中文字幕人成乱码
|
亚洲av中文无码乱人伦在线观看
|
亚洲日本中文字幕天天更新
|
久久99精品免费一区二区
|
免费精品无码AV片在线观看
|
好男人看视频免费2019中文
|
亚洲国产精品成人久久蜜臀
|
亚洲国产二区三区久久
|
粉色视频在线观看www免费
|
色猫咪免费人成网站在线观看
|
免费观看理论片毛片
|
亚洲视频欧洲视频
|
国产免费A∨在线播放
|
亚洲成亚洲乱码一二三四区软件
|
国产一区二区三区亚洲综合
|
全亚洲最新黄色特级网站
|
亚洲欧洲日产国码二区首页
|
久久久WWW免费人成精品
|
亚洲av无码不卡
|
a级毛片视频免费观看
|
亚洲欧洲中文日韩av乱码
|
亚洲精品国产第一综合99久久
|
国产精品久免费的黄网站
|
亚洲熟妇无码一区二区三区导航
|
免费萌白酱国产一区二区
|
四虎永久在线观看免费网站网址
|
国产99久久亚洲综合精品
|
免费人成在线观看网站视频
|
亚洲中文无码a∨在线观看
|
日韩免费高清播放器
|
亚洲国产日韩成人综合天堂
|
国产大片免费天天看
|
免费少妇a级毛片
|
国产精品免费无遮挡无码永久视频
|
亚洲热线99精品视频
|
久久久久久噜噜精品免费直播
|
97se亚洲综合在线
|