1- // package netty;
2- //
3- // import io.netty.bootstrap.ServerBootstrap;
4- // import io.netty.channel.*;
5- // import io.netty.channel.nio.NioEventLoopGroup;
6- // import io.netty.channel.socket.SocketChannel;
7- // import io.netty.channel.socket.nio.NioServerSocketChannel;
8- // import io.netty.handler.codec.string.StringDecoder;
9- // import io.netty.handler.codec.string.StringEncoder;
10- // import io.netty.util.CharsetUtil;
11- //
12- /// **
13- // * Created with IntelliJ IDEA.
14- // * User: liukunyang
15- // * Date: 14-3-28
16- // * Time: 下午6:02
17- // * To change this template use File | Settings | File Templates.
18- // */
19- /// **
20- // * Discards any incoming data.
21- // */
22- // public class DiscardServer {
23- //
24- // private int port;
25- //
26- // public DiscardServer(int port) {
27- // this.port = port;
28- // }
29- //
30- // public void run() throws Exception {
31- //
32- // EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
33- // EventLoopGroup workerGroup = new NioEventLoopGroup();
34- // try {
35- // ServerBootstrap b = new ServerBootstrap(); // (2)
36- // b.group(bossGroup, workerGroup)
37- // .channel(NioServerSocketChannel.class) // (3)
38- // .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
39- // @Override
40- // public void initChannel(SocketChannel ch) throws Exception {
41- // ChannelPipeline pipeline = ch.pipeline();
42- // pipeline.addLast(new DiscardServerHandler());
43- // pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
44- // pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
45- // }
46- // })
47- // .option(ChannelOption.SO_BACKLOG, 128) // (5)
48- // .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
49- //
50- // // Bind and start to accept incoming connections.
51- // ChannelFuture f = b.bind(port).sync(); // (7)
52- //
53- // // Wait until the server socket is closed.
54- // // In this example, this does not happen, but you can do that to gracefully
55- // // shut down your server.
56- // f.channel().closeFuture().sync();
57- // } finally {
58- // workerGroup.shutdownGracefully();
59- // bossGroup.shutdownGracefully();
60- // }
61- // }
62- //
63- // public static void main(String[] args) throws Exception {
64- // int port;
65- // if (args.length > 0) {
66- // port = Integer.parseInt(args[0]);
67- // } else {
68- // port = 8080;
69- // }
70- // new DiscardServer(port).run();
71- // }
72- // }
1+ package netty ;
2+
3+ import io .netty .bootstrap .ServerBootstrap ;
4+ import io .netty .channel .*;
5+ import io .netty .channel .nio .NioEventLoopGroup ;
6+ import io .netty .channel .socket .SocketChannel ;
7+ import io .netty .channel .socket .nio .NioServerSocketChannel ;
8+ import io .netty .handler .codec .string .StringDecoder ;
9+ import io .netty .handler .codec .string .StringEncoder ;
10+ import io .netty .util .CharsetUtil ;
11+
12+ /**
13+ * Created with IntelliJ IDEA.
14+ * User: liukunyang
15+ * Date: 14-3-28
16+ * Time: 下午6:02
17+ * To change this template use File | Settings | File Templates.
18+ */
19+ /**
20+ * Discards any incoming data.
21+ */
22+ public class DiscardServer {
23+
24+ private int port ;
25+
26+ public DiscardServer (int port ) {
27+ this .port = port ;
28+ }
29+
30+ public void run () throws Exception {
31+
32+ EventLoopGroup bossGroup = new NioEventLoopGroup (); // (1)
33+ EventLoopGroup workerGroup = new NioEventLoopGroup ();
34+ try {
35+ ServerBootstrap b = new ServerBootstrap (); // (2)
36+ b .group (bossGroup , workerGroup )
37+ .channel (NioServerSocketChannel .class ) // (3)
38+ .childHandler (new ChannelInitializer <SocketChannel >() { // (4)
39+ @ Override
40+ public void initChannel (SocketChannel ch ) throws Exception {
41+ ChannelPipeline pipeline = ch .pipeline ();
42+ pipeline .addLast (new DiscardServerHandler ());
43+ pipeline .addLast ("decoder" , new StringDecoder (CharsetUtil .UTF_8 ));
44+ pipeline .addLast ("encoder" , new StringEncoder (CharsetUtil .UTF_8 ));
45+ }
46+ })
47+ .option (ChannelOption .SO_BACKLOG , 128 ) // (5)
48+ .childOption (ChannelOption .SO_KEEPALIVE , true ); // (6)
49+
50+ // Bind and start to accept incoming connections.
51+ ChannelFuture f = b .bind (port ).sync (); // (7)
52+
53+ // Wait until the server socket is closed.
54+ // In this example, this does not happen, but you can do that to gracefully
55+ // shut down your server.
56+ f .channel ().closeFuture ().sync ();
57+ } finally {
58+ workerGroup .shutdownGracefully ();
59+ bossGroup .shutdownGracefully ();
60+ }
61+ }
62+
63+ public static void main (String [] args ) throws Exception {
64+ int port ;
65+ if (args .length > 0 ) {
66+ port = Integer .parseInt (args [0 ]);
67+ } else {
68+ port = 8080 ;
69+ }
70+ new DiscardServer (port ).run ();
71+ }
72+ }
0 commit comments