So-called Ax, so before we build the Netty game server, we need to prepare the things well.
First go to Netty's official website to download the latest version of the Netty jar package, http://netty.io/downloads.html, here I downloaded the netty-5.0.0.alpha2.tar.bz2 version.
Open the ZIP package and locate the Netty-5.0.0.alpha2.jar package under All-in-one.
Open our Eclipse our sacred editor, then create a new file, named Lib, dedicated to the third-party jar.
Then drag the jar from the previous Netty to the inside, then configure the path.
Add jars This package is added to the path.
OK, the past has. Then we started Netty the programming journey. First of all, the reader needs to know something about Netty, or he may not be able to read the code I wrote.
First of all, we first make a package, named Com.netty.server, in the bag we create a new class Gameserver, is the main portal of our server.
public class Gameserver {public void bind (int port) throws Exception{eventloopgroup Bossgroup = new Nioeventloopgroup ();// Thread Group Eventloopgroup workGroup = new Nioeventloopgroup (); try {serverbootstrap b = new Serverbootstrap ();// Server Startup Management configuration B.group (Bossgroup, WorkGroup). Channel (nioserversocketchannel.class). Option (Channeloption.so_backlog , 1024)//maximum number of client connections is 1024.childHandler (new channelinitializer<socketchannel> () {@Overrideprotected void Initchannel (Socketchannel ch) throws Exception {//Here we first do not write code, here is mainly to add business processing handler}}); Channelfuture f = b.bind (port). Sync (), if (F.issuccess ()) {System.out.println ("Server starts success at Port:" + port);} F.channel (). Closefuture (). sync (); catch (Exception e) {e.printstacktrace ();} Finally{bossgroup.shutdowngracefully (); workgroup.shutdowngracefully ();}} public static void Main (string[] args) throws Exception{int port = 8080;new gameserver (). bind (port);}}
Run, you can discover that the service is started.
One of the Netty game servers