Apache Mina 基礎入門

2021-07-15 02:33:43 字數 4259 閱讀 9740

1.需要niosocketacceptor

2.設定編碼解碼過濾器

3.設定一些session的屬性

4.繫結乙個埠

public

class

minaserver catch (ioexception e)

}}public

class

myserverhandler

extends

iohandleradapter

@override

public

void

sessionopened(iosession session) throws exception

@override

public

void

sessionclosed(iosession session) throws exception

@override

public

void

sessionidle(iosession session, idlestatus status) throws exception

@override

public

void

exceptioncaught(iosession session, throwable cause) throws exception

@override

public

void

messagereceived(iosession session, object message) throws exception

date date = new date();

session.write(date);

}@override

public

void

messagesent(iosession session, object message) throws exception

}

1.需要niosocketconnector

2.設定編碼解碼過濾器

3.連線乙個遠端的伺服器端程式,並傳送資料

public

class

minaclient

}public

class

myclienthandler

extends

iohandleradapter

@override

public

void

messagereceived(iosession session, object message) throws exception

}

public

class myprotocolpack

public

intgetlength()

public

void

setlength(int length)

public

byte

getflag()

public

void

setflag(byte flag)

public string getcontent()

public

void

setcontent(string content)

@override

public string tostring()

}

public

class

myprotocolencoder

extends

protocolencoderadapter

public

void

encode(iosession session, object message, protocolencoderoutput out) throws exception

buffer.flip();

out.write(buffer);

}}public

class

myprotocoldecoder

implements

protocoldecoder

public

myprotocoldecoder(charset charset)

public

intgetmaxpacklength()

public

void

setmaxpacklength(int maxpacklength)

this.maxpacklength = maxpacklength;

}public context getcontext(iosession session)

return ctx;

}public

void

decode(iosession session, iobuffer in, protocoldecoderoutput out) throws exception else

if (length >= packheadlength && length - packheadlength <= buffer.remaining()) else

}if (buffer.hasremaining()) else

}public

void

finishdecode(iosession session, protocoldecoderoutput out) throws exception

public

void

dispose(iosession session) throws exception

}/**

* 這個類出現是為了可以檢查到半包的情況

*/private

class

context

public

void

this.getbuffer().put(buffer);

}public

void

reset()

public iobuffer getbuffer()

public

void

setbuffer(iobuffer buffer)

public charsetdecoder getdecoder()

}}public

class

myptotocolfactory

implements

protocolcodecfactory

public protocolencoder getencoder(iosession session) throws exception

public protocoldecoder getdecoder(iosession session) throws exception

}

public

class

minaprotocolserver

}class myhandler extends iohandleradapter

@override

public

void

exceptioncaught(iosession session, throwable cause) throws exception

@override

public

void

messagereceived(iosession session, object message) throws exception

}

public

class

minaprotocolclient

}});

}private

static

void

senddata(iosession session)

}}class myhandler extends iohandleradapter

}@override

public

void

messagereceived(iosession session, object message) throws exception

}

Apache MINA 應用高階

li.zhongnan hotmail.com 1。mina 框架簡介 下圖為本人根據對mina的簡要理解,所畫出來的框架簡圖 session可以理解為伺服器與客戶端的特定連線,該連線由伺服器位址 埠以及客戶端位址 埠來決定。客戶端發起請求時,指定伺服器位址和埠,客戶端也會指定或者根據網路路由資訊自...

Apache MINA 應用高階

1。mina 框架簡介 下圖為本人根據對mina的簡要理解,所畫出來的框架簡圖 session可以理解為伺服器與客戶端的特定連線,該連線由伺服器位址 埠以及客戶端位址 埠來決定。客戶端發起請求時,指定伺服器位址和埠,客戶端也會指定或者根據網路路由資訊自動指定乙個位址 自動分配乙個埠。這個位址 埠對構...

Apache MINA框架簡述

1。mina 框架簡介 下圖為本人根據對mina的簡要理解,所畫出來的框架簡圖 session可以理解為伺服器與客戶端的特定連線,該連線由伺服器位址 埠以及客戶端位址 埠來決定。客戶端發起請求時,指定伺服器位址和埠,客戶端也會指定或者根據網路路由資訊自動指定乙個位址 自動分配乙個埠。這個位址 埠對構...