Netty 实战:如何实现文件服务器?( 二 )

【Netty 实战:如何实现文件服务器?】整体比较简单,exceptionCaught 为异常时的处理 。
channelActive() 为客户端连接时,服务端返回客户端的提示 。
channelRead0() 为服务端对于客户端的反馈,就是通过客户端输入的文件路径,返回文件内容 。
测试验证我们直接使用本地的 telnet

  • 打开命令行
输入 telnet localhost 8889
192:~ houbinbin$ telnet localhost 8889Trying ::1...Connected to localhost.Escape character is '^]'.HELLO: Type the path of the file to retrieve.
  • 输入文件路径
/Users/houbinbin/code/_github/netty-learn/netty-learn-four/src/main/java/com/github/houbb/netty/learn/four/file/FileServer.java反馈如下:
就是把 FileServer.java 这个文件内容全部返回回来了 。
OK: 2387/* * Copyright (c)  2019. houbinbin Inc. * netty-learn All rights reserved. */package com.github.houbb.netty.learn.four.file;....... 内容省略/** * <p> </p> * * <pre> Created: 2019/9/21 11:49 PM  </pre> * <pre> Project: netty-learn  </pre> * * @author houbinbin */public class FileServer {    public static void main(String[] args) {        EventLoopGroup bossGroup = new NioEventLoopGroup();        EventLoopGroup workerGroup = new NioEventLoopGroup();        .... 内容省略    }}Connection closed by foreign host.



推荐阅读