首页>代码>netty4解决粘包的项目实例>/SocketServer/com/itstack/netty/ChildChannelHandler.java
package com.itstack.netty;

import java.nio.charset.Charset;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {

		System.out.println("报告");
		System.out.println("信息:有一客户端链接到本服务端");
		System.out.println("IP:" + ch.localAddress().getHostName());
		System.out.println("Port:" + ch.localAddress().getPort());
		System.out.println("报告完毕");

		// 半包处理【基于换行符】
		//ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
		//半包处理, ‘;’ 作为分包符
		ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,Unpooled.copiedBuffer(";".getBytes())));
//		 字符串编码
		ch.pipeline().addLast(new StringDecoder());
		// 字符串解码
		ch.pipeline().addLast(new StringEncoder());
		// 在管道中添加我们自己的接收数据实现方法
		ch.pipeline().addLast(new MyServerHanlder());

	}

}
最近下载更多
jaonsang  LV25 2022年4月8日
13021603815  LV7 2021年4月12日
yangctz  LV25 2021年1月2日
bian21bian  LV2 2020年8月24日
lililililiapi  LV1 2019年7月6日
dagf113225  LV68 2019年1月29日
494821341  LV1 2018年12月12日
1847731288  LV13 2018年8月18日
1943815081  LV13 2018年4月13日
369647247  LV9 2018年4月10日
最近浏览更多
1909741473  LV8 2024年2月19日
sunlzh888888  LV29 2023年9月12日
yinyun1985  LV14 2022年10月31日
jaonsang  LV25 2022年4月7日
ericxu1116  LV24 2021年5月20日
13021603815  LV7 2021年4月12日
aloner_joker 2021年4月3日
暂无贡献等级
yangctz  LV25 2021年1月2日
wxm4252  LV12 2020年12月18日
名字随便的啦啦  LV7 2020年10月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友