Java tutorial
/* * PoweredCube3 * Copyright (C) 2014 James * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.jselby.pc.network; 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; /** * Adapted from Netty documentation */ public class NetworkServer extends Thread { public final int port; private ChannelFuture f = null; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; public NetworkServer(int port) { this.port = port; } public void run() { bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new PacketDecoder(), new ClientConnectionHandler()); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); System.out.println("Binding to port " + port + "..."); ChannelFuture fut = b.bind("0.0.0.0", port).sync(); this.f = fut.channel().closeFuture(); } catch (InterruptedException e) { e.printStackTrace(); } finally { } } public void waitForClose() { try { f.sync(); workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } catch (InterruptedException e) { e.printStackTrace(); } } public boolean isBound() { return f != null; } }