北國狼人的部落格
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
隨筆-159 評論-114 文章-7 trackbacks-0
用戶NIO寫服務(wù)器,必須多加處理OP_WRITE,不會丟包
服務(wù)器往外發(fā)數(shù)據(jù)包,不是光一個socketChannel.write(bb); 就完事了
那是理想狀態(tài)。。
用我方法才能保證發(fā)出,即使客戶端連接很濫
public
static
long
flushChannel(SocketChannel socketChannel,ByteBuffer bb,
long
writeTimeout)
throws
IOException
{
SelectionKey key
=
null
;
Selector writeSelector
=
null
;
int
attempts
=
0
;
int
bytesProduced
=
0
;
try
{
while
(bb.hasRemaining())
{
int
len
=
socketChannel.write(bb);
attempts
++
;
if
(len
<
0
)
{
throw
new
EOFException();
}
bytesProduced
+=
len;
if
(len
==
0
)
{
if
(writeSelector
==
null
)
{
writeSelector
=
Selector.open();
if
(writeSelector
==
null
)
{
//
Continue using the main one
continue
;
}
}
key
=
socketChannel.register(writeSelector, key.OP_WRITE);
if
(writeSelector.select(writeTimeout)
==
0
)
{
if
(attempts
>
2
)
throw
new
IOException(
"
Client disconnected
"
);
}
else
{
attempts
--
;
}
}
else
{
attempts
=
0
;
}
}
}
finally
{
if
(key
!=
null
)
{
key.cancel();
key
=
null
;
}
if
(writeSelector
!=
null
)
{
//
Cancel the key.
writeSelector.selectNow();
writeSelector.close();
}
}
return
bytesProduced;
}
posted on 2008-11-19 12:08
北國狼人的BloG
閱讀(1450)
評論(1)
編輯
收藏
評論:
#
re: 用戶NIO寫服務(wù)器,必須多加處理OP_WRITE,不會丟包
2010-11-09 16:14 |
raymond
好用,頂一個!
回復(fù)
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
<
2008年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
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(33)
給我留言
查看公開留言
查看私人留言
隨筆分類
3D至尊寶(1)
人生各層面經(jīng)驗積累(15)
關(guān)愛IT人士健康(15)
達(dá)內(nèi)學(xué)習(xí)總結(jié)(42)
隨筆檔案
2018年11月 (2)
2012年3月 (1)
2011年8月 (1)
2011年1月 (1)
2010年12月 (2)
2010年8月 (1)
2010年6月 (3)
2010年5月 (3)
2010年4月 (11)
2010年3月 (8)
2010年2月 (4)
2010年1月 (4)
2009年12月 (6)
2009年11月 (1)
2009年10月 (3)
2009年9月 (3)
2009年8月 (3)
2009年6月 (1)
2009年5月 (1)
2009年4月 (1)
2009年1月 (1)
2008年12月 (1)
2008年11月 (3)
2008年10月 (1)
2008年8月 (4)
2008年6月 (2)
2008年5月 (3)
2008年3月 (1)
2008年2月 (1)
2008年1月 (2)
2007年12月 (2)
2007年10月 (3)
2007年9月 (1)
2006年11月 (1)
2006年9月 (1)
2006年8月 (3)
2006年7月 (2)
2006年6月 (1)
2006年5月 (1)
2006年4月 (5)
2006年3月 (7)
2006年2月 (7)
2006年1月 (12)
2005年12月 (8)
2005年11月 (19)
2005年10月 (9)
文章分類
3D至尊寶
Web技術(shù)和趨勢(1)
原創(chuàng)(1)
翻譯Java文章(2)
文章檔案
2006年10月 (1)
2005年10月 (3)
Java學(xué)習(xí)論壇
3D至尊寶
Java最牛網(wǎng)站
搜索
最新評論
1.?re: xsl:value-of select="." 什么意思?[未登錄]
ffff
--ff
2.?re: 理解Java ClassLoader機制 |用Java說話,人氣戰(zhàn)勝時間!Come On
好文章
--godtree
3.?re: 理解Java ClassLoader機制 |用Java說話,人氣戰(zhàn)勝時間!Come On[未登錄]
好文章,收了,謝謝博主
--thinker
4.?re: 高效產(chǎn)生一組不重復(fù)的隨機數(shù)
要是要求產(chǎn)生的隨機數(shù)量特別大怎么辦啊
--ll
5.?re: AS3 位操作比較快
“看完還不明白,就不要搞計算機了。”
就沖著此話,為樓主的優(yōu)越感深表擔(dān)憂
每個人都是從新手過來的,樓主發(fā)帖如果是為了分享,我很敬佩,如果是為了和裝13,那我只能笑笑
--調(diào)整心態(tài)啊樓主
閱讀排行榜
1.?RCP開發(fā),如何解決 org.eclipse.core.runtime.CoreException: Plug-in TD was unable to load class td.app.Application.(14365)
2.?mysql_install_db --defaults-file=/etc/my.cnf --user=mysql(8197)
3.?IWAB0014E Unexpected exception occured 該死的問題,就是因為Eclipse + WTP 需要先配置Server,再生成webservice(6963)
4.?理解Java ClassLoader機制 |用Java說話,人氣戰(zhàn)勝時間!Come On(6509)
5.?J2ME 網(wǎng)絡(luò)連接(HTTP) 模擬器 WTK(4989)
評論排行榜
1.?RCP開發(fā),如何解決 org.eclipse.core.runtime.CoreException: Plug-in TD was unable to load class td.app.Application.(9)
2.?給自己的軟件加保險,用java獲取硬盤序列號(8)
3.?不要寫垃圾代碼?。?!(8)
4.?反日行動從現(xiàn)在開始,從每個中國人,每個程序員做起!不用日本東西,不給日本人打工!??!(8)
5.?IWAB0014E Unexpected exception occured 該死的問題,就是因為Eclipse + WTP 需要先配置Server,再生成webservice(7)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 北國狼人的BloG
主站蜘蛛池模板:
井研县
|
西贡区
|
兰考县
|
沭阳县
|
兴化市
|
秦安县
|
南江县
|
乌拉特前旗
|
读书
|
达孜县
|
珲春市
|
黔西县
|
健康
|
襄汾县
|
广饶县
|
新泰市
|
修文县
|
祁东县
|
蒲城县
|
思南县
|
尉氏县
|
法库县
|
淮北市
|
凤台县
|
平谷区
|
贵定县
|
修水县
|
凌源市
|
丽江市
|
丽水市
|
琼中
|
会理县
|
锡林郭勒盟
|
邢台市
|
呈贡县
|
大港区
|
石楼县
|
山东省
|
得荣县
|
邛崃市
|
沙雅县
|