轻轻松松写NIO
作者:older 日期:2008-03-18 10:34:38
近期在Jmin Kernel中加入NIO通讯块. 由于该块的加入可使Socket通讯编写更加简单容易. 下面就三个编写步骤介绍它们的使用.
第一步: 编写消息处理器.
我们都知道使用Socket最大的特点无非是用它来发送/接受一些网络数据. 一般接收端是需要处理这些数据的,正因为如此我们编写了消息数据处理器,具体如下:
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
import org.jmin.j2ee.kernel.nio.Listener;
/**
* A sample class to receive byte message and return it to sender
*
* @author Linda
*/
public class NIOFeedBackListener extends Listener{
/**
* Handle bytes message,and write them back to sender
*
* @param data from sender
*/
public void onData(byte[] data){
try{
System.out.println("Reveive data: " + new String(data));
this.connection.write(data);
}catch(Exception e){
}
}
}
该类由org.jmin.j2ee.kernel.nio.Listener扩展而来并实现其抽象方法: onData(byte[] data),
我们可以在该方法中加入我们的处理逻辑. 目前例子类是收到消息并反馈回发送者.
第二步:创建Server
我们已经对NIO做了一些包装类,使其更为简单使用,具体例子如下:
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
import java.io.IOException;
import org.jmin.j2ee.kernel.nio.ServerCluster;
/**
* Test sample to build a socket server based on NIO and don't care more detail.
*
* @author chris
*/
public class NIOServerTestor {
/**
* Entrance method to build a server.If get familiar with nio, it is easy to be
* catched.
*/
public static void main(String args[]) throws IOException {
/**
* Cluster object works as a server collection manager,some servers can be open
* in it.Before useful operation,<method>open</method>must be invoked to create
* a cluster instance.
*/
ServerCluster cluster = ServerCluster.open();
/**
* Call<method>openServer</method>to create a socket server,two necessary
* arguments are applied here: server port and message lister class.
*
* We can assume that every server can handle matched messages from clients,
* this listener class is extended from super <class>org.jmin.j2ee.kernel.Listener</class>,
* of course,you can define sub implementation based on the class.
*/
cluster.openServer(9988, NIOFeedBackListener.class);
System.out.println("A nio server run on port: "+ 9988);
/**
* Create a synchronized object to block the main thread and make the server kept running.
*/
Object obj = new Object();
try {
synchronized (obj) {
obj.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
第三步:建立连接到Server的Client.代码如下:
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
import java.io.IOException;
import org.jmin.j2ee.kernel.nio.Connection;
import org.jmin.j2ee.kernel.nio.ConnectionCluster;
/**
* Test sample to show that how to build a nio client.
*
* @author Chris Liao
*/
public class NIOClient {
/**
* Main method to run a nio client.
*
*/
public static void main(String args[]) throws IOException {
/**
* Call static method from ConnectionCluster class to build its a instance,
* this action is similar with nio.
*/
ConnectionCluster cluster = ConnectionCluster.open();
/**
* Build a connection to remote host with threa parameters
*
* @arg remote host
* @arg remote port
* @arg message listener
*
* Message listener is extended from<class>org.jmin.j2ee.kernel.Listener</class>
* You can define yourself message listeners for it.
*/
Connection con = cluster.connect("localhost", 9988,new NIOFeedBackListener());
/**
* Send a message to the remote host.
*/
con.write("I am a Chinese".getBytes());
/**
* synchronized objec to block main thread.
*
*
*/
Object obj = new Object();
try {
synchronized (obj) {
obj.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
经过上面三个步骤,我们就已经完成建立NIO通讯的全部过程,至此我们可以编译他们,并分别运行Server 与Client来看运行效果吧. 正确运行后将会在Console频繁看到 "Receive data: I am a Chinese"
如果对该通讯块细节感兴趣的话,请联系: jmin520@21cn.com.
第一步: 编写消息处理器.
我们都知道使用Socket最大的特点无非是用它来发送/接受一些网络数据. 一般接收端是需要处理这些数据的,正因为如此我们编写了消息数据处理器,具体如下:
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
import org.jmin.j2ee.kernel.nio.Listener;
/**
* A sample class to receive byte message and return it to sender
*
* @author Linda
*/
public class NIOFeedBackListener extends Listener{
/**
* Handle bytes message,and write them back to sender
*
* @param data from sender
*/
public void onData(byte[] data){
try{
System.out.println("Reveive data: " + new String(data));
this.connection.write(data);
}catch(Exception e){
}
}
}
该类由org.jmin.j2ee.kernel.nio.Listener扩展而来并实现其抽象方法: onData(byte[] data),
我们可以在该方法中加入我们的处理逻辑. 目前例子类是收到消息并反馈回发送者.
第二步:创建Server
我们已经对NIO做了一些包装类,使其更为简单使用,具体例子如下:
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
import java.io.IOException;
import org.jmin.j2ee.kernel.nio.ServerCluster;
/**
* Test sample to build a socket server based on NIO and don't care more detail.
*
* @author chris
*/
public class NIOServerTestor {
/**
* Entrance method to build a server.If get familiar with nio, it is easy to be
* catched.
*/
public static void main(String args[]) throws IOException {
/**
* Cluster object works as a server collection manager,some servers can be open
* in it.Before useful operation,<method>open</method>must be invoked to create
* a cluster instance.
*/
ServerCluster cluster = ServerCluster.open();
/**
* Call<method>openServer</method>to create a socket server,two necessary
* arguments are applied here: server port and message lister class.
*
* We can assume that every server can handle matched messages from clients,
* this listener class is extended from super <class>org.jmin.j2ee.kernel.Listener</class>,
* of course,you can define sub implementation based on the class.
*/
cluster.openServer(9988, NIOFeedBackListener.class);
System.out.println("A nio server run on port: "+ 9988);
/**
* Create a synchronized object to block the main thread and make the server kept running.
*/
Object obj = new Object();
try {
synchronized (obj) {
obj.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
第三步:建立连接到Server的Client.代码如下:
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
import java.io.IOException;
import org.jmin.j2ee.kernel.nio.Connection;
import org.jmin.j2ee.kernel.nio.ConnectionCluster;
/**
* Test sample to show that how to build a nio client.
*
* @author Chris Liao
*/
public class NIOClient {
/**
* Main method to run a nio client.
*
*/
public static void main(String args[]) throws IOException {
/**
* Call static method from ConnectionCluster class to build its a instance,
* this action is similar with nio.
*/
ConnectionCluster cluster = ConnectionCluster.open();
/**
* Build a connection to remote host with threa parameters
*
* @arg remote host
* @arg remote port
* @arg message listener
*
* Message listener is extended from<class>org.jmin.j2ee.kernel.Listener</class>
* You can define yourself message listeners for it.
*/
Connection con = cluster.connect("localhost", 9988,new NIOFeedBackListener());
/**
* Send a message to the remote host.
*/
con.write("I am a Chinese".getBytes());
/**
* synchronized objec to block main thread.
*
*
*/
Object obj = new Object();
try {
synchronized (obj) {
obj.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
经过上面三个步骤,我们就已经完成建立NIO通讯的全部过程,至此我们可以编译他们,并分别运行Server 与Client来看运行效果吧. 正确运行后将会在Console频繁看到 "Receive data: I am a Chinese"
如果对该通讯块细节感兴趣的话,请联系: jmin520@21cn.com.
平均得分
(0 次评分)
评论: 9 | 查看次数: 2227
- 共有 9 条评论
- 共有 9 条评论
发表评论
订阅
上一篇
|

文章来自:
标签: 





Acheter carte world of warcraft , 10 min de livraison PayPal Top10 partenaire, Acheter Maintenant!>
婚活パーティー
ふじみ野市 一戸建て
松戸市 一戸建て
三郷市 一戸建て
上尾市 一戸建て
浦和 不動産
イソギンチャク 通販
目黒線 不動産
不動産投資
空調機 修理
エルメス バッグ
家具店
相続相談
投資情報
J-Payment
異業種交流
任意整理
ハレクラニ
間仕切り
福祉用具専門相談員
レンタルサーバー
映像編集
過払い金変換請求
ダビング サービス
レーティング
時計の修理
USB
新聞折込チラシ
有料老人ホーム 紹介
三軒茶屋 賃貸マンション
カイロプラクター
中古 冷蔵ショーケース
新幹線便
賃貸伊丹
ネットスーパー システム
楽器レンタル
私学
相続登記
大宮 一戸建
モデルサイズ
ドリッパー
先物取引
福生市 不動産
募金
桜新町 マンション
声楽教室
RMT
リロケーション
過払い
フランチャイズ 募集
人間関係 悩み
ショッピング枠 現金化
建築金物
調理 求人
オーガニックコットン
石垣島 ダイビングツアー
リノベーションマンション
卵巣チョコレート嚢胞 漢方
グッチ バッグ
原状回復
ホクロ除去
しわ取り
簿記検定
和光市 不動産
胃がん
子宮がん
東京 事務所
社会人入試
英語 転職
wow gold
wow power leveling
oofay.us
wowmine
brogame
brogame
隔膜泵
隔膜泵
隔膜泵
磁力泵
磁力泵
磁力泵
油泵
油泵
油泵
上海展览公司
上海展览公司
上海展览公司
阀门
阀门
阀门
空调
空调
空调
胶体磨
胶体磨
胶体磨
Cosmetic surgery
Cosmetic surgery
菲律宾留学
菲律宾留学
菲律宾留学
东方医院
东方医院
东方医院
隔膜泵
油泵
上海展览公司
阀门
磁力泵
qswd00
wow gold
wow gold
澳洲留学费用
澳洲留学签证
德国签证
澳洲留学论坛
澳洲留学申请
澳洲大学排名
澳洲留学中介
澳洲留学网
英国商学院
美国签证
伦敦摄政学院
韩国签证
澳洲大学排名
伦敦大学
buy worhammer gold
buy worhammer gold
wow account
wow accounts
buy wow account
buy wow accounts
world of warcraft account
wow account
wow accounts
buy wow account
buy wow accounts
world of warcraft account
新西兰签证
爱尔兰签证
马来西亚签证
出国留学
英国签证
新加坡签证
澳洲留学费用
澳洲留学签证
澳洲留学论坛
澳洲留学申请
澳洲留学网
韩国签证
美国签证
德国签证
wow account
wow accounts
buy wow account
buy wow accounts
world of warcraft account
环境:
PC:普通PC
内存:1G
CPU: 2.8G
开一个Server,连接数到1000并发,没有发现任何问题,而且还可以同时上网,感觉不错!
如果机器性能不错的话,更大的并发数也应该没问题,支持!