避免您抽我,我还是把所有的代码贴上来
来源:3-4 编写自定义助手类

慕尼黑0536602
2020-12-08
老师您上班辛苦,不着急,慢慢看哈。我是已经看了好几遍,跟着视频敲,但是还是查不出原因。。。
两个地方跟着视频敲之后被要求强转:
Channel channel = (Channel) channelHandlerContext.channel();
System.out.println(((io.netty.channel.Channel) channel).remoteAddress());
麻烦您了,阿里嘎多!
package imoocNettyHello;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class HelloServer {
/**
* 客户端发起一个请求,然后服务器响应“Hello Netty!”
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 1.创建一个主线程组
EventLoopGroup bossGroup = new NioEventLoopGroup();
//2.创建一个从线程组
EventLoopGroup workerGroup = new NioEventLoopGroup();
//3.创建一个服务器的启动类
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new HelloServerInitializer());
//ServerBootstrap要绑定端口号:
ChannelFuture channelFuture = serverBootstrap.bind(8080).sync();
//关闭端口号
channelFuture.channel().closeFuture().sync();
}finally {
//优雅地关闭主线程 和 从线程
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
package imoocNettyHello;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
public class HelloServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel channel) throws Exception {
// 利用channel 拿到 pipeline
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast("HttpServerCodec",new HttpServerCodec());
pipeline.addLast("customHandler",new CustomHandler());
}
}
package imoocNettyHello;
import java.nio.channels.Channel;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;
public class CustomHandler extends SimpleChannelInboundHandler<HttpObject>{
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject msg) throws Exception {
// 利用上下文拿到channel
Channel channel = (Channel) channelHandlerContext.channel();
//打出来我们客户端的位置
System.out.println(((io.netty.channel.Channel) channel).remoteAddress());
//把我们要回复的内容深度拷贝到ByteBuf
ByteBuf content = Unpooled.copiedBuffer("Hello Nettty!!!!",CharsetUtil.UTF_8);
//构建一个Http response
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK,content);
//设置当前内容的类型和长度
//类型
response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
//长度
response.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());
//把相应刷回客户端
channelHandlerContext.writeAndFlush(response);
}
}
写回答
1回答
-
风间影月
2020-12-08
之前有个同学也这样,他是因为因素的依赖版本啥的和我不一样,你检查一下。直接用老师的源码应该就可以的。好几个同学遇到过了,务必保证环境jar依赖版本啥的一致哈
00
Netty+Spring Boot仿微信-全栈开发高性能后台及客户端
SpringBoot/Netty+MUI全栈开发 同时搞定后台+ Android&iOS
1498 学习 · 684 问题
相似问题