资源简介
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个文件信息
相关资源
- Omron ETN21模块进行modbustcp通讯
- 组态王中S7-300TCP驱动
- 基于Apache Mina实现的TCP长连接和短连接
- CVI下的TCP服务器和客户端
- 代码客:G-TcpServer(IOCP) 1.0 正式版及
- TCP 发包工具(windows)
- VxWorks TCPIP协议栈
- socket小工具(可方便建立TCP UDP的soc
- vc 编写的基于TCP协议的客户/服务器
- 基于CSocket的多人聊天室
- 采用WINSOCK2 编写的TCP/UDP通信程序
- Modbus TCP Client
- 易语言 TCP使用HTTP、SOCKES5代理IP。
- 西门子S7-1200 MODBUS-TCP通讯编程
- 200SMART Modbus-TCP通信程序演示-轮询(多
- TCP_UDP对结构体加密数据传输
- 基于PIC的TCP/IP网络协议和zigbee无线传
- 欧姆龙PLC tcp通信工具
- 欧姆龙以太网通讯TCP/UDP及欧姆龙PLC程
- 精通并发与Netty 涉及到的代码
- netty-高并发编程-视频教程张龙-90讲完
- netty的视频90集
- 流媒体相关协议标准RTP/RTSP/RTCP PDF文档
- 航海模拟器中DCPA TCPA的算法
- TCPMP 支持ce6.0的播放器
- TCP/IP协议详解3卷全_高清_带书签
- stm32103c8t6ESP8266串口转WIFI模块TCP服务器
- DOS下的TCP-IP协议库文件原代码.rar
- 安卓基于netty4.x心跳,断线重连,状态
- 《TCP/IP详解卷2:实现》pdf
评论
共有 条评论