Mina Server執行個體

來源:互聯網
上載者:User

在公司寫了個Mina 伺服器端的執行個體,供大家學習參考:

package com.cemso.mina.server;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.charset.Charset;import org.apache.mina.core.service.IoAcceptor;import org.apache.mina.core.session.IdleStatus;import org.apache.mina.filter.codec.ProtocolCodecFilter;import org.apache.mina.filter.codec.textline.TextLineCodecFactory;import org.apache.mina.filter.logging.LoggingFilter;import org.apache.mina.transport.socket.nio.NioSocketAcceptor;/** * @author gl65293 */public class MinaTimeServer {    private static final int PORT = 9123;    public static void main(String[] args) throws IOException{        IoAcceptor acceptor = new NioSocketAcceptor();        acceptor.getFilterChain().addLast("logger", new LoggingFilter());        acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));        acceptor.setHandler(new TimeServerHandler());        acceptor.getSessionConfig().setReadBufferSize(2048);        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);        acceptor.bind(new InetSocketAddress(PORT));    }}

package com.cemso.mina.server;/** * @author gl65293 * */import java.util.Date;import org.apache.mina.core.service.IoHandlerAdapter;import org.apache.mina.core.session.IdleStatus;import org.apache.mina.core.session.IoSession;public class TimeServerHandler extends IoHandlerAdapter {    public TimeServerHandler() {    }    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {        cause.printStackTrace();    }    public void messageReceived(IoSession session, Object message) throws Exception {        String str = message.toString();        if (str.trim().equalsIgnoreCase("quit")) {            session.close(true);            return;        } else {            Date date = new Date();            session.write(date.toString());            System.out.println("Message written...");            return;        }    }    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {        System.out.println((new StringBuilder()).append("IDLE ").append(session.getIdleCount(status)).toString());    }}

C:\Documents and Settings\gl65293\Desktop\Mina\examplejars\log4j-1.2.15.jar
C:\Documents and Settings\gl65293\Desktop\Mina\examplejars\mina-core-2.0.4.jar
C:\Documents and Settings\gl65293\Desktop\Mina\examplejars\mina-example-2.0.4.jar
C:\Documents and Settings\gl65293\Desktop\Mina\examplejars\slf4j-api-1.5.11.jar
C:\Documents and Settings\gl65293\Desktop\Mina\examplejars\slf4j-log4j12-1.5.0.jar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.