java服务器无法响应,【Java】Netty http客户端无法接收到服务器的响应信息

这是我的客户端的Handler代码

class RobotHandler : ChannelInboundHandlerAdapter() {

private val logger = LoggerFactory.getLogger(this.javaClass)

override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) {

logger.info(msg?.toString())

}

override fun channelActive(ctx: ChannelHandlerContext?) {

val request = DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/robot")

ctx?.writeAndFlush(request)

}

override fun exceptionCaught(ctx: ChannelHandlerContext?, cause: Throwable?) {

cause?.printStackTrace()

ctx?.close()

}

}

这是客户端的代码

@Component

@Order(1)

class RobotNettyClient : ApplicationRunner{

private val logger = LoggerFactory.getLogger(this.javaClass)

override fun run(args: ApplicationArguments?) {

val group = NioEventLoopGroup()

try {

val bootstrap = Bootstrap()

bootstrap.group(group)

.channel(NioSocketChannel::class.java)

.option(ChannelOption.SO_KEEPALIVE, true)

.handler(object: ChannelInitializer() {

override fun initChannel(ch: SocketChannel?) {

val pipeline = ch?.pipeline()

pipeline?.addLast(HttpClientCodec())

pipeline?.addLast(HttpContentCompressor())

pipeline?.addLast(RobotHandler())

}

})

logger.info("Netty started")

bootstrap.connect("127.0.0.1",8080).sync()

} finally {

group.shutdownGracefully()

}

}

}

我的服务器选用的是Spring boot,像在内部通过Netty实现数据不断读取和发送响应,然后获取到数据。可是我通过Netty向Spring boot发送响应过后,触发Controller但是无法接收到返回的消息,请问怎么解决

回答

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个 Netty Socket 服务器示例,可以主动向10000个客户分发消息: ```java import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; public class NettyClient { private final String host; private final int port; public NettyClient(String host, int port) { this.host = host; this.port = port; } public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new NettyClientHandler()); } }); ChannelFuture f = b.connect(host, port).sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } } public static void main(String[] args) throws Exception { String host = "localhost"; int port = 8080; NettyClient client = new NettyClient(host, port); client.run(); } } import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.CharsetUtil; public class NettyClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // Send message to server String message = "Hello, server!"; ByteBuf buf = Unpooled.copiedBuffer(message, CharsetUtil.UTF_8); ctx.writeAndFlush(buf); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg; String received = in.toString(CharsetUtil.UTF_8); System.out.println("Client received: " + received); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } } ``` 在上面的示例中,我们创建了一个 `Bootstrap`,并将其连接到指定的主机和口。当客户服务器连接时,`NettyClientHandler` 类将被调用来处理服务器响应。 下面是 `NettyClientHandler` 的示例代码,它将接收到的消息打印到控制台上: ```java import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.CharsetUtil; public class NettyClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // Send message to server String message = "Hello, server!"; ByteBuf buf = Unpooled.copiedBuffer(message, CharsetUtil.UTF_8); ctx.writeAndFlush(buf); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg; String received = in.toString(CharsetUtil.UTF_8); System.out.println("Client received: " + received); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } } ``` 在上面的示例中,当客户接收服务器的消息时,它将使用 `System.out.println()` 方法将消息打印到控制台上。 使用上述示例代码可以很容易地实现一个 Netty Socket 服务器,可以主动向10000个客户分发消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值