netty 實現socket服務端編寫


 1 import java.net.InetSocketAddress;
 2 
 3 import io.netty.bootstrap.ServerBootstrap;
 4 import io.netty.channel.ChannelFuture;
 5 import io.netty.channel.ChannelInitializer;
 6 import io.netty.channel.ChannelOption;
 7 import io.netty.channel.EventLoopGroup;
 8 import io.netty.channel.nio.NioEventLoopGroup;
 9 import io.netty.channel.socket.SocketChannel;
10 import io.netty.channel.socket.nio.NioServerSocketChannel;
11 
12 public class TimeServer {
13     
14     
15     public static void main(String[] args) throws Exception{
16         new TimeServer().bind("192.168.1.102", 8400);
17     }
18     
19 
20     public void bind(String addr,int port) {
21         //配置服務端的nio線程組
22         EventLoopGroup boosGroup=new NioEventLoopGroup();
23         EventLoopGroup workerGroup=new NioEventLoopGroup();
24         try {
25             ServerBootstrap b=new ServerBootstrap();
26             b.group(boosGroup,workerGroup);
27             b.channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG,1024)
28             .childHandler(new ChildChannelHandler());
29             //綁定端口,同步等待成功
30             ChannelFuture f=b.bind(new InetSocketAddress(addr, port)).sync();
31             //等等服務器端監聽端口關閉
32             f.channel().closeFuture().sync();
33         } catch (Exception e) {
34             // TODO: handle exception
35         }finally{
36             boosGroup.shutdownGracefully();
37             workerGroup.shutdownGracefully();
38         }
39     }
40     
41     private class ChildChannelHandler extends ChannelInitializer<SocketChannel>{
42 
43         @Override
44         protected void initChannel(SocketChannel ch) throws Exception {
45             ch.pipeline().addLast(new TimeServerHandler());
46             
47         }
48         
49     }
50     
51     
52 }
 1 import java.nio.ByteBuffer;
 2 import java.text.SimpleDateFormat;
 3 import java.util.Date;
 4 
 5 import io.netty.buffer.ByteBuf;
 6 import io.netty.buffer.Unpooled;
 7 import io.netty.channel.ChannelHandlerAdapter;
 8 import io.netty.channel.ChannelHandlerContext;
 9 
10 public class TimeServerHandler extends ChannelHandlerAdapter {
11 
12     @Override
13     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
14         ByteBuf buf=(ByteBuf)msg;
15         byte[] bytes=new byte[buf.readableBytes()];
16         buf.readBytes(bytes);
17         String body=new String(bytes,"UTF-8");
18         SimpleDateFormat  dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
19         String time=dateFormat.format(new Date());
20         String res="來自與服務端的回應,時間:"+ time;
21         ByteBuf resp=Unpooled.copiedBuffer(res.getBytes());
22         ctx.write(resp);
23         
24     }
25 
26     @Override
27     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
28         ctx.flush();
29     }
30 
31     @Override
32     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
33         ctx.close();
34     }
35 
36     
37 
38     
39 }

 

這個比傳統的nio好用多了,netty的版本為netty-all-5.0.0.Alpha1.jar

 

天天學習,天天進步

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM