序言:
微微一笑,这是一个微微波澜的时代,相较于过去冷兵器时代,多了些什么、又少了些什么……
倘若我生于那个时代,微微一笑,毅然从兵,或许因为愚忠死得其所,但是却尽了一定时间内守护家人的意愿,或许会做的更好也说不定……哈哈
记得那年冬天 我们彼此陪伴 梦想是唯一的火焰 我们一起点燃
(一些青年围坐在一起,每个脸庞上都挂着微笑)
我们长发批肩 冷冷的旷野荒芜人烟 有兄弟还有烈酒 就能取暖
(微微一笑,你知道,你的同伴、敌人,一个个死在你身边是什么感觉吗?)
当我们谈起明天 总是觉得希望无限 总说再过二十年 还是条好汉
还记得二十年前 那一群懵懂的少年 擦亮了双眼 挺起了双肩 要将梦想实现
(黑衣少年坚定的目光,我不知道能给予你什么,我只知道要好好守护你
那些你想做的梦。
我们会支持你完成。
这样就能离你更近了,不是吗)
还记得二十年前 那一句执著的誓言 忘记了永远 却忘不了当年 曾经共苦同甘
我们好久不见 不曾把酒言欢 二十年前那个冬天 怎能想到今天
长发一一剪短 冷冷的旷野一如从前 多少当年的英雄 早已不见
(七郎抓紧着红绳,即使身体万箭穿心,因为勇气不是来自愤怒,而是你保护一样东西的决心)
环境:
1、idea
2、gradle相关jar包
3、下载一个网络调试助手噢~么么哒
一、我们在gradle文件中进行配置,这一点很重要~(么么哒)
group 'com.blackking'
version '1.0-SNAPSHOT'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile(
"io.netty:netty-all:4.1.30.Final",
"com.google.protobuf:protobuf-java:3.6.1",
"com.google.protobuf:protobuf-java-util:3.6.1",
"org.apache.thrift:libthrift:0.11.0",
"org.springframework.boot:spring-boot-starter-web",
"org.springframework.boot:spring-boot-starter-test"
)
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
二、编写一个TcpClient类:帅气
public class TcpClient {
private static final Logger logger = Logger.getAnonymousLogger();
public static String HOST = "127.0.0.1";
public static int PORT = 8999;
public static Bootstrap bootstrap = getBoothstrap();
public static final Bootstrap getBoothstrap(){
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class);
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("new handler",new TcpClientHandler());
}
});
return b;
}
public static final Channel getChannel(String host,int port){
Channel channel = null;
try{
channel = bootstrap.connect(host,port).sync().channel();
}catch(Exception e){
System.out.println(String.format("连接server失败",host,port));
return null;
}
return channel;
}
public static void sendMsg(Channel channel,Object Msg) throws Exception{
if (channel != null){
channel.writeAndFlush(Msg).sync();
}else {
logger.warning("连接失败~~ 靓仔");
}
}
public static void main(String[] args) {
try{
long t0 = System.nanoTime();
byte[] value = null;
Channel channel = null;
for(int i=0;i<50000;i++){
channel = getChannel(HOST,PORT);
value = (i+"靓仔").getBytes();
ByteBufAllocator alloc = channel.alloc();
ByteBuf buf = alloc.buffer(value.length);
buf.writeBytes(value);
TcpClient.sendMsg(channel,buf);
}
long t1 = System.nanoTime();
System.out.println((t1-t0)/100000.0);
Thread.sleep(1000);
System.exit(0);
}catch(Exception e){
e.printStackTrace();
}
}
}
【上面这个是客户端,我们还要写一个服务端,或者不写嗯,用网络调试助手来实验这个客户端的质量,是否不间断发送数据包~】
三、客户端除了主类,还有处理器类,实现类
public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
private static final Logger logger = Logger.getAnonymousLogger();
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ByteBuf){
ByteBuf buf = (ByteBuf) msg;
byte[] dst = new byte[buf.capacity()];
buf.readBytes(dst);
logger.info("client 接收到服务器返回的消息"+new String(dst));
ReferenceCountUtil.release(msg);
}else{
logger.warning("so nice,continue");
}
}
}