資源簡介
netty搭建tcp服務,并以相應的編碼解決粘包,拆包問題

代碼片段和文件信息
package?com.study.netty;
import?java.net.InetSocketAddress;
import?io.netty.bootstrap.Bootstrap;
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;
import?io.netty.channel.socket.nio.NioSocketChannel;
import?io.netty.handler.codec.LinebasedframeDecoder;
import?io.netty.handler.codec.string.StringDecoder;
public?class?TimeClient?{
public?void??connect?(String?hostint?port){
//配置客戶端nio線程組
EventLoopGroup?group?=?new?NioEventLoopGroup();
try?{
//創建啟動輔助類
Bootstrap?b?=?new?Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.remoteAddress(new?InetSocketAddress(host?port))
.handler(new?ChannelInitializer()?{
@Override
protected?void?initChannel(SocketChannel?arg0)?throws?Exception?{
//?TODO?Auto-generated?method?stub
arg0.pipeline().addLast(new?LinebasedframeDecoder(1024));//粘包和拆包問題處理
arg0.pipeline().addLast(new?StringDecoder());//把接收的字節轉為字符串
arg0.pipeline().addLast(new?TimeClientHandle002());
}
});
//發起異步連接操作
ChannelFuture?future?=?b.connect().sync();
//等待客戶端鏈路關閉
future.channel().closeFuture().sync();
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}finally?{
//釋放nio線程組
group.shutdownGracefully();
}
}
public?static?void?main(String[]?args)?{
int?port?=?10001;
if(args?!=?null?&&?args.length?>?0){
try?{
port?=?Integer.valueOf(args[0]);
}?catch?(NumberFormatException?e)?{
e.printStackTrace();
}
}
for?(int?i?=?0;?i?100;?i++)?{
new?TimeClient().connect(“127.0.0.1“?port);
System.err.println(“循環第“+i+“次“);
}
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-05-10?10:53??nettytcpserver\
?????文件?????????557??2018-05-10?10:55??nettytcpserver\.classpath
?????文件?????????390??2018-05-10?10:53??nettytcpserver\.project
?????目錄???????????0??2018-05-10?10:54??nettytcpserver\.settings\
?????文件?????????252??2018-05-10?10:54??nettytcpserver\.settings\org.eclipse.core.resources.prefs
?????文件?????????598??2018-05-10?10:53??nettytcpserver\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2018-05-10?10:55??nettytcpserver\bin\
?????目錄???????????0??2018-05-10?10:55??nettytcpserver\bin\com\
?????目錄???????????0??2018-05-10?10:55??nettytcpserver\bin\com\study\
?????目錄???????????0??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\
?????文件????????1504??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\TimeClient$1.class
?????文件????????2817??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\TimeClient.class
?????文件????????2937??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\TimeClientHandle.class
?????文件????????2968??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\TimeClientHandle002.class
?????文件????????2656??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\TimeClientHandle01.class
?????文件????????1473??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\TimeServer$1.class
?????文件????????1300??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\TimeServer$ChildChannelHandle.class
?????文件????????2568??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\TimeServer.class
?????文件????????2419??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\TimeServerHandle.class
?????文件????????2496??2018-05-10?10:55??nettytcpserver\bin\com\study\netty\TimeServerHandle01.class
?????目錄???????????0??2018-05-10?10:55??nettytcpserver\lib\
?????文件??????489884??2018-04-25?09:48??nettytcpserver\lib\log4j-1.2.17.jar
?????文件?????2291648??2018-04-27?17:12??nettytcpserver\lib\netty-all-4.0.56.Final.jar
?????文件???????26083??2018-04-25?09:48??nettytcpserver\lib\slf4j-api-1.7.2.jar
?????文件????????8819??2018-04-25?09:48??nettytcpserver\lib\slf4j-log4j12-1.7.2.jar
?????目錄???????????0??2018-05-10?10:54??nettytcpserver\src\
?????目錄???????????0??2018-05-10?10:54??nettytcpserver\src\com\
?????目錄???????????0??2018-05-10?10:54??nettytcpserver\src\com\study\
?????目錄???????????0??2018-05-10?10:54??nettytcpserver\src\com\study\netty\
?????文件????????2013??2018-05-10?10:54??nettytcpserver\src\com\study\netty\TimeClient.java
?????文件????????1725??2018-05-10?10:54??nettytcpserver\src\com\study\netty\TimeClientHandle.java
............此處省略5個文件信息
評論
共有 條評論