《Netty權威指南》(一)簡單的時間伺服器P69

來源:互聯網
上載者:User

標籤:channel   不一致   指南   creat   object   []   new   elf   pool   

由於該書是基於Netty5編寫的範例代碼,而Netty5已經被官方廢棄。目前基於推薦版的4.1.12.Final在學習過程中,可能會出現個別介面不一致的情況。所以記錄可在4.1.12下編譯通過的代碼
 
  1. package net.xjdsz.n;
  2. import io.netty.bootstrap.ServerBootstrap;
  3. import io.netty.buffer.ByteBuf;
  4. import io.netty.buffer.Unpooled;
  5. import io.netty.channel.*;
  6. import io.netty.channel.nio.NioEventLoopGroup;
  7. import io.netty.channel.socket.SocketChannel;
  8. import io.netty.channel.socket.nio.NioServerSocketChannel;
  9. import java.util.Date;
  10. /**
  11. * Created by dingshuo on 2017/6/14.
  12. */
  13. public class TimeServer {
  14. public void bind(int port) throws Exception{
  15. EventLoopGroup bossGroup=new NioEventLoopGroup();
  16. EventLoopGroup workerGroup=new NioEventLoopGroup();
  17. try{
  18. ServerBootstrap b=new ServerBootstrap();
  19. b.group(bossGroup,workerGroup)
  20. .channel(NioServerSocketChannel.class)
  21. .option(ChannelOption.SO_BACKLOG,1024)
  22. .childHandler(new ChannelInitializer<SocketChannel>() {
  23. @Override
  24. protected void initChannel(SocketChannel socketChannel) throws Exception {
  25. socketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter(){
  26. @Override
  27. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  28. ByteBuf buf=(ByteBuf)msg;
  29. byte[] req=new byte[buf.readableBytes()];
  30. buf.readBytes(req);
  31. String body=new String(req,"UTF-8");
  32. System.out.println("收到資料:"+body);
  33. String currentTime=new Date(System.currentTimeMillis()).toString();
  34. ByteBuf resp= Unpooled.copiedBuffer(currentTime.getBytes());
  35. ctx.write(resp);
  36. }
  37. @Override
  38. public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
  39. ctx.flush();
  40. }
  41. @Override
  42. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  43. ctx.close();
  44. }
  45. });
  46. }
  47. });
  48. //綁定連接埠,同步等待成功
  49. ChannelFuture f=b.bind(port).sync();
  50. //等待服務端監聽連接埠關閉
  51. f.channel().closeFuture().sync();
  52. }finally {
  53. //退出,釋放資源
  54. bossGroup.shutdownGracefully();
  55. workerGroup.shutdownGracefully();
  56. }
  57. }
  58. public static void main(String[] args) throws Exception{
  59. TimeServer timeServer=new TimeServer();
  60. timeServer.bind(2000);
  61. }
  62. }



《Netty權威指南》(一)簡單的時間伺服器P69

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.