java socket直接访问netty服务器中数据接收不全的问题

在项目中,通过netty框架搭建的服务器。但是在Android端我想直接采用Socket与服务器通信。但是在这个过程中遇到两个问题。

1.服务器接收不到数据

解决方法:我发送的形式是直接将字符串转成字节流,按照tcp协议进行发送的。一开始服务器能看到客户端接入了,但是就是收不到消息。后来在字符串的末尾加上了'\n'换行符就行了。

2.服务器发送数据时,客户端接收的数据是一串奇怪的字符串。例如:[@Bd31d456

解决方法:在服务器发送数据时,一开始是直接ctx.writeAndFlush(s.getbyte()),后来改了很多的编码设置之类的都没起作用。后来在一个知乎的回答里面看到按这样的方式ctx.writeAndFlush(Unpooled.copiedBuffer(s.getBytes()))发送,就可以正常接收了。


对于netty的使用不是很熟悉,只是按照视频里的步骤粗略的写了一下,所以遇到问题就一直不知道怎么解决。

以下是一个简单的 Netty Socket 服务器示例,可将消息分发给10000个客户端: ```java import io.netty.bootstrap.ServerBootstrap; 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.NioServerSocketChannel; public class NettyServer { private final int port; public NettyServer(int port) { this.port = port; } public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new NettyServerHandler()); } }) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; NettyServer server = new NettyServer(port); server.run(); } } ``` 在上面的示例,我们创建了一个 `ServerBootstrap`,并将其绑定到指定的端口上。我们使用了两个 `EventLoopGroup`,一个用于处理连接请求,一个用于处理客户端请求。当客户端连接到服务器时,`NettyServerHandler` 类将被调用来处理客户端请求。 下面是 `NettyServerHandler` 的示例代码,它将接收到的消息广播给所有连接的客户端: ```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 NettyServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg; String received = in.toString(CharsetUtil.UTF_8); System.out.println("Server received: " + received); // Broadcast the received message to all clients ctx.writeAndFlush(Unpooled.copiedBuffer(received, CharsetUtil.UTF_8)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } } ``` 在上面的示例,当服务器接收到客户端的消息时,它将使用 `ctx.writeAndFlush()` 方法将消息发送给所有连接的客户端。 使用上述示例代码可以很容易地实现一个 Netty Socket 服务器,可将消息分发给10000个客户端。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值