One of the Netty game servers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.