歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Socket網絡編程-Mina 實例

第一步: Mina服務端 Server 的創建

public class MinaServer {
 public static final int PORT = 1989;
 
 private static final Logger logger = Logger.getLogger(MinaServer.class);
 public static void main(String[] args) {
  IoAcceptor ioAcceptor = null;
  try {
  //創建一個非阻塞 的Server端Socket
  ioAcceptor = new NioSocketAcceptor();
  //
  ioAcceptor.getFilterChain().addLast(
    "codec",
    new ProtocolCodecFilter(new TextLineCodecFactory(Charset
      .forName("UTF-8"),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
 
  //設置讀取數據的緩沖區大小
  ioAcceptor.getSessionConfig().setReadBufferSize(4096);
  //設置10秒沒有任何操作進入空閒狀態
 
  ioAcceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
 
  //設置業務邏輯處理程序
  ioAcceptor.setHandler(new DefaultServerHander());
 
  ioAcceptor.bind(new InetSocketAddress(PORT));
 
  logger.info("在端口" + PORT + "啟動Mina監聽程序成功");
 
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
 
 
 }
}

第二部: 創建Mina服務端處理程序

public class MinaServer {
 public static final int PORT = 1989;
 
 private static final Logger logger = Logger.getLogger(MinaServer.class);
 public static void main(String[] args) {
  IoAcceptor ioAcceptor = null;
  try {
  //創建一個非阻塞 的Server端Socket
  ioAcceptor = new NioSocketAcceptor();
  //
  ioAcceptor.getFilterChain().addLast(
    "codec",
    new ProtocolCodecFilter(new TextLineCodecFactory(Charset
      .forName("UTF-8"),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
 
  //設置讀取數據的緩沖區大小
  ioAcceptor.getSessionConfig().setReadBufferSize(4096);
  //設置10秒沒有任何操作進入空閒狀態
 
  ioAcceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
 
  //設置業務邏輯處理程序
  ioAcceptor.setHandler(new DefaultServerHander());
 
  ioAcceptor.bind(new InetSocketAddress(PORT));
 
  logger.info("在端口" + PORT + "啟動Mina監聽程序成功");
 
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
 }
}

第三部:客戶端連接程序代碼

public class MinaClient {
 private static final int PORT = 1989;
 private static final String Host = "localhost";
 public static void main(String[] args) {
 
  // 創建一個非阻塞的客戶端連接
  IoConnector connector = new  NioSocketConnector();
 
  connector.getFilterChain().addLast(
    "codec",
    new ProtocolCodecFilter(new TextLineCodecFactory(Charset
      .forName("UTF-8"),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
 
 
  connector.setHandler(new MinaClientHander());
 
  ConnectFuture connectFuture = connector.connect(new InetSocketAddress(Host, PORT));
 
  connectFuture.awaitUninterruptibly(); //等待連接創建完成
 
  IoSession session  = connectFuture.getSession();
 
  session.write("高江濤");
 
  session.getCloseFuture().awaitUninterruptibly(); //等待連接斷開
  connector.dispose();
 
 }
}

第四步: 客戶端處理程序

public class MinaClientHander extends IoHandlerAdapter {
 @Override
 public void exceptionCaught(IoSession session, Throwable cause)
  throws Exception {
  // TODO Auto-generated method stub
  super.exceptionCaught(session, cause);
 }
 @Override
 public void messageReceived(IoSession session, Object message)
  throws Exception {
  // TODO Auto-generated method stub
  super.messageReceived(session, message);
 }
 @Override
 public void messageSent(IoSession session, Object message) throws Exception {
  // TODO Auto-generated method stub
  super.messageSent(session, message);
 }
 @Override
 public void sessionClosed(IoSession session) throws Exception {
  // TODO Auto-generated method stub
  super.sessionClosed(session);
 }
 @Override
 public void sessionCreated(IoSession session) throws Exception {
  // TODO Auto-generated method stub
  super.sessionCreated(session);
 }
 @Override
 public void sessionIdle(IoSession session, IdleStatus status)
  throws Exception {
  // TODO Auto-generated method stub
  super.sessionIdle(session, status);
 }
 @Override
 public void sessionOpened(IoSession session) throws Exception {
  // TODO Auto-generated method stub
  super.sessionOpened(session);
 }
}

Copyright © Linux教程網 All Rights Reserved