Mina使用Demo以及最少jar

2021-08-30 23:27:05 字數 1810 閱讀 1406

伺服器端啟動**:

//建立乙個非阻塞的server端的socket,用nio

socketacceptor acceptor = new niosocketacceptor();

//建立資料過濾器

defaultiofilterchainbuilder chain = acceptor.getfilterchain();

//設定這個過濾器將 一行一行 的讀取資料

chain.addlast("mychain", new protocolcodecfilter(new textlinecodecfactory()));

//設定伺服器端的訊息處理器

acceptor.sethandler(new samplmainserverhandle());

//伺服器端 埠

int bindport = 9988;

//繫結埠,啟動伺服器

acceptor.bind(new inetsocketaddress(bindport));

system.out.println("mina server is listing on:= " + bindport);

客戶端連線**:

//create tcp/ip connector

niosocketconnector client = new niosocketconnector();

//建立接受資料的過濾器

defaultiofilterchainbuilder chain = client.getfilterchain();

//設定這個過濾器將一行一行(/r/n)的讀取資料

chain.addlast("mychin", new protocolcodecfilter(new textlinecodecfactory()));

//設定客戶端的訊息處理器

client.sethandler(new samplmianclienthandle());

//set connect timeout

client.setconnecttimeoutmillis(30000);

//連線伺服器

connectfuture cf = client.connect(new inetsocketaddress("localhost",9988));

// wait for the connection attempt to be finished.

cf.awaituninterruptibly();

cf.getsession().getclosefuture().awaituninterruptibly();

client.dispose();

客戶端和伺服器端的處理器都是繼承自iohandleradapter類所以只列出伺服器端

public class samplmainserverhandle extends iohandleradapter

}//當乙個客戶端連線關閉時

@override

public void sessionclosed(iosession session) throws exception

//當乙個客戶端連線進入時

@override

public void sessionopened(iosession session) throws exception

}

以下為mina執行所需最少的jar包

MINA入門使用

mina作為高效能的可應對高併發訪問的nio框架,特性就不多介紹了。下面提供一些使用的例項。伺服器端 description 用來啟動mina服務端 author administrator 2012 10 21 public class minatimeserver 伺服器端程式的業務處理器 de...

Xsocket與Mina使用感受

mina使用了一次,有一段時間沒關注了。不過其的用法和功能還記得。mina是乙個大型一點的網路框架,支援多種協議,使用起來也較簡單,其 中提供的原始碼中含有example,不過有一些情況下需要擴充套件其過濾器類。支援非同步。其主要方法為 onmessage xsocket剛使用,現在正在使用中。xs...

MINA2 0簡單使用

一直知道mina是apache 開發的乙個開發socket程式設計框架,但一直沒弄清楚mina2.0裡的多執行緒該如何處理,根據apache提供的文件,使用mina2.0開發多執行緒的程式變的非常簡單,只用在其filter中加入執行緒池就可以了,感覺挺神奇,於是參考apache的其他專案,寫了個小例...