controlspy3.ControlSpy3.java Source code

Java tutorial

Introduction

Here is the source code for controlspy3.ControlSpy3.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controlspy3;
//package io.netty.example.discard;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 *
 * @author R&D Engineer Junior
 *
 */
public class ControlSpy3 {

    private int port;
    public static boolean ALGO = true;

    public ControlSpy3(int port) {
        this.port = port;
    }

    public void run() {

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            // Server
            ServerBootstrap boots = new ServerBootstrap();
            boots.group(bossGroup, workerGroup);
            boots.channel(NioServerSocketChannel.class);
            boots.childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new TimeEncoder(), new SpyAsServer());
                    if (ALGO == false) {
                        System.out.println("se salio ");
                    }
                    System.out.println("Cliente conectado!");
                }
            }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

            //SERVER
            // Bind and start to accept incoming connections.
            ChannelFuture future_ch = boots.bind(port).sync();

            //SERVER
            // Wait until the server socket is closed. Server
            future_ch.channel().closeFuture().sync();

        } catch (InterruptedException ex) {

        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        int port;

        System.out.println("Pontichat Server v0.2\n");

        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        } else {
            // Default port
            port = 20500;
        }

        new ControlSpy3(port).run();
    }
}