public MemoryMonitor() throws IOException {
    // 创建UDP数据包NIO
    NioDatagramAcceptor acceptor = new NioDatagramAcceptor();

    // 设置filter
    DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
    chain.addLast(
        "codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
    chain.addLast("logger", new LoggingFilter());

    // NIO设置底层IOHandler 把服务器的本身传入
    acceptor.setHandler(new MemoryMonitorHandler());

    // 设置是否重用地址? 也就是每个发过来的udp信息都是一个地址?
    DatagramSessionConfig dcfg = acceptor.getSessionConfig();
    dcfg.setReuseAddress(true);

    // 绑定端口地址
    acceptor.bind(new InetSocketAddress(PORT));
    logger.debug("UDPServer listening on port " + PORT);
  }
Exemple #2
0
  public MemoryMonitor() throws IOException {

    NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
    acceptor.setHandler(new MemoryMonitorHandler(this));

    DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
    chain.addLast("logger", new LoggingFilter());

    DatagramSessionConfig dcfg = acceptor.getSessionConfig();
    dcfg.setReuseAddress(true);

    frame = new JFrame("Memory monitor");
    tabbedPane = new JTabbedPane();
    tabbedPane.add("Welcome", createWelcomePanel());
    frame.add(tabbedPane, BorderLayout.CENTER);
    clients = new ConcurrentHashMap<SocketAddress, ClientPanel>();
    frame.pack();
    frame.setLocation(300, 300);
    frame.setVisible(true);

    acceptor.bind(new InetSocketAddress(PORT));
    System.out.println("UDPServer listening on port " + PORT);
  }