一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Apache Mina的學(xué)習(xí)應(yīng)用(四)

 集微筆記 2014-02-27

在客戶端和服務(wù)端的調(diào)用如下:

 

客戶端;

package cn.com.unutrip.java.opensource.mina.sumup;

import java.net.InetSocketAddress;

import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import cn.com.unutrip.java.opensource.mina.sumup.codec.SumUpProtocolCodecFactory;
/**
 * TCP/ID 客戶端的應(yīng)用
 * @author longgangbai
 *
 */
public class Client {
          private static final String HOSTNAME = "localhost";
          private static final int PORT = 8123;
     
          private static final long CONNECT_TIMEOUT = 30*1000L; // 30 seconds
     
          // Set this to false to use object serialization instead of custom codec.
          private static final boolean USE_CUSTOM_CODEC = true;
     
          public static void main(String[] args) throws Throwable {
              // prepare values to sum up
              int[] values = new int[10];
              for (int i = 0; i < args.length; i++) {
                  values[i] = Integer.parseInt(args[i]);
              }
              //創(chuàng)建一個(gè)客戶端對(duì)象
              NioSocketConnector connector = new NioSocketConnector();
     
              // Configure the service.
              //設(shè)置連接超時(shí)時(shí)間
              connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
              //獲取過(guò)濾器鏈工廠
              DefaultIoFilterChainBuilder filterchain=connector.getFilterChain();
              //添加相應(yīng)的過(guò)濾鏈
              if (USE_CUSTOM_CODEC) {
           //采用自定義的協(xié)議編碼工廠類過(guò)濾器連
           filterchain.addLast("codec",new ProtocolCodecFilter(new SumUpProtocolCodecFactory(false)));
              } else {
           //采用對(duì)象序列化編碼工廠類過(guò)濾器連
           filterchain.addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
              }
              //添加日志過(guò)濾器連
              filterchain.addLast("logger", new LoggingFilter());
              //進(jìn)行業(yè)務(wù)處理邏輯
              connector.setHandler(new ClientSessionHandler(values));
     
              IoSession session=null;
              for (;;) {
                  try {
                     ConnectFuture future = connector.connect(new InetSocketAddress(
                              HOSTNAME, PORT));
                      future.awaitUninterruptibly();
                      session = future.getSession();
                      break;
                  } catch (RuntimeIoException e) {
                      System.err.println("Failed to connect.");
                      e.printStackTrace();
                      Thread.sleep(5000);
                  }
              }
              // wait until the summation is done
              session.getCloseFuture().awaitUninterruptibly();
              connector.dispose();
          }
      }

 

服務(wù)端使用如下:

 

package cn.com.unutrip.java.opensource.mina.sumup;

import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import cn.com.unutrip.java.opensource.mina.sumup.codec.SumUpProtocolCodecFactory;
/**
 *TCP /IP 服務(wù)端的代碼的應(yīng)用
 * @author longgangbai
 *
 */
public class Server {
    private static final int SERVER_PORT = 8123;
          // Set this to false to use object serialization instead of custom codec.
          private static final boolean USE_CUSTOM_CODEC = true;
     
          public static void main(String[] args) throws Throwable {
              //創(chuàng)建服務(wù)端
              NioSocketAcceptor acceptor = new NioSocketAcceptor();
              //獲取過(guò)濾鏈的信息
              DefaultIoFilterChainBuilder filterchain=acceptor.getFilterChain();
              // Prepare the service configuration.
              if (USE_CUSTOM_CODEC) {
           filterchain.addLast("codec",new ProtocolCodecFilter(new SumUpProtocolCodecFactory(true)));
              } else {
           filterchain.addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
              }
              filterchain.addLast("logger", new LoggingFilter());
              //業(yè)務(wù)處理
              acceptor.setHandler(new ServerSessionHandler());
              //綁定服務(wù)端口
              acceptor.bind(new InetSocketAddress(SERVER_PORT));
              System.out.println("Listening on port " + SERVER_PORT);
          }

}

 

希望大家交流。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    国产老熟女超碰一区二区三区| 欧美尤物在线视频91| 精产国品一二三区麻豆| 中文字幕熟女人妻视频| 在线观看视频国产你懂的| 五月综合婷婷在线伊人| 亚洲国产精品久久琪琪| 国产精品午夜福利免费在线| 欧美日韩免费观看视频| 天堂网中文字幕在线观看| 国产又长又粗又爽免费视频| 日韩人妻少妇一区二区| 激情亚洲一区国产精品久久| 99久久婷婷国产亚洲综合精品| 91在线国内在线中文字幕| 亚洲一区二区三区熟女少妇| 国产精品久久精品国产| 少妇被粗大进猛进出处故事| 欧美乱码精品一区二区三| 亚洲午夜av一区二区| 91人妻人人做人碰人人九色| 日本乱论一区二区三区| a久久天堂国产毛片精品| 日本一品道在线免费观看| 日韩欧美好看的剧情片免费| 欧美日韩国内一区二区| 国产日韩欧美综合视频| 欧美午夜一级特黄大片| 欧美日韩精品久久亚洲区熟妇人| 国产av乱了乱了一区二区三区| 日韩性生活片免费观看| 久久re6热在线视频| 日韩不卡一区二区视频| 果冻传媒精选麻豆白晶晶| 青青久久亚洲婷婷中文网| 免费特黄欧美亚洲黄片| 成人精品国产亚洲av久久| 亚洲最大的中文字幕在线视频| 国产欧美亚洲精品自拍| 色婷婷中文字幕在线视频| 欧美午夜视频免费观看|