1 20 package org.apache.mina.example.netcat; 21 22 import org.apache.mina.common.ByteBuffer; 23 import org.apache.mina.common.IdleStatus; 24 import org.apache.mina.common.IoHandler; 25 import org.apache.mina.common.IoHandlerAdapter; 26 import org.apache.mina.common.IoSession; 27 28 35 public class NetCatProtocolHandler extends IoHandlerAdapter { 36 public void sessionOpened(IoSession session) { 37 session.setIdleTime(IdleStatus.READER_IDLE, 10); 41 } 42 43 public void sessionClosed(IoSession session) { 44 System.err.println("Total " + session.getReadBytes() + " byte(s)"); 46 } 47 48 public void sessionIdle(IoSession session, IdleStatus status) { 49 if (status == IdleStatus.READER_IDLE) 51 session.close(); 52 } 53 54 public void messageReceived(IoSession session, Object message) { 55 ByteBuffer buf = (ByteBuffer) message; 56 while (buf.hasRemaining()) { 58 System.out.print((char) buf.get()); 59 } 60 System.out.flush(); 61 } 62 } | Popular Tags |