netty4.0.22分包问题

wuxiaochun 2014-09-09 09:58:30
写了一个异步的http的netty服务器和客户端。在windows下传输600K左右的内容没问题任何问题,但是把服务器部署在linux下传输 小文本不分包没有问题。比如8k,如果发送几百K的客户端就接受不到信息。怀疑是分包问题,但是分包已经制指定了。红色部分已经标出。(代码是粘贴主要部分和设置,其他忽略不关紧要代码)

客户端源码:
public void run(String str, String url) {
EventLoopGroup group = new NioEventLoopGroup();
try {
long start = System.currentTimeMillis();
Bootstrap b = new Bootstrap();
b.group(group);
b.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true);
b.handler(new ChannelInitializer<SocketChannel>() {

@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));

pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));

pipeline.addLast("handler", new HelloClient());
}
});

ChannelFuture f = b.connect("172.168.8.52", 5656).sync();
f.channel().writeAndFlush(“测试数据”);
f.channel().closeFuture().sync();
long end = System.currentTimeMillis();
System.out.println("用时=" + (end - start));
}
catch (Exception e) {

}
finally {
group.shutdownGracefully();
}
}
代理类:

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("client接收到服务器返回的消息:" + msg);
}

服务器端:

/** 服务器ip地址 */
private static final String IP = "172.168.1.11";

/** 服务器端口 */
private static final int PORT = 5656;

/** 网络连接的线程数 */
private static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors() * 2;

/** 网络连接服务业务逻辑线程数 */
private static final int BIZTHREADSIZE = 10;

/** 网络连接 */
private static final EventLoopGroup bossGroup = new NioEventLoopGroup(BIZGROUPSIZE);

/** 业务逻辑 */
private static final EventLoopGroup workerGroup = new NioEventLoopGroup(BIZTHREADSIZE);

/**
* 服务启动
*
* @throws Exception
*/
public static void service() throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new TcpChannelInitializer());
ChannelFuture f = bootstrap.bind(IP, PORT).sync();
// f.channel().closeFuture().sync();
System.out.println("TCP服务器已启动");
}

protected static void shutdown() {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}

public static void main(String[] args) throws Exception {
System.out.println("开始启动TCP服务器...");
DiscardServer.service();
}

服务器设置初始类:
// TODO Auto-generated method stub
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));

pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new TcpServerHandlerImpl());


代理服务类:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// TODO Auto-generated method stub
System.out.println("server receive message :" + msg);
//此处增加读取600K左右的问题windows没问题,linux发送客户接受不到消息
ctx.channel().writeAndFlush("yes server already accept your message" + msg);
ctx.close();
}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
System.out.println("channelActive>>>>>>>>");
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("exception is general");
}

...全文
806 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

6,787

社区成员

发帖
与我相关
我的任务
社区描述
JBoss技术交流
社区管理员
  • JBoss技术交流社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧