Netty学习笔记(二)
只是代码,建议配合http://ifeve.com/netty5-user-guide/此网站观看
package com.demo.netty;
import org.junit.Before;
import org.junit.Test;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class Server {
private int port;
@Before
public void init() {
this.port = 8088;
}
@Test
public void run() throws Exception {
NioEventLoopGroup bossGroup = new NioEventLoopGroup(); //用来接收进来的连接
NioEventLoopGroup wokerGroup = new NioEventLoopGroup();//用来处理已经被接收的连接,boss接收到连接后就会把连接信息注册到woker上,由woker去做具体处理
ServerBootstrap serverBootstrap = new ServerBootstrap();//Netty服务端启动Nio的辅助启动类
serverBootstrap.group(bossGroup,wokerGroup).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ServerHandler());
}
}).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.SO_SNDBUF, 3*1024).option(ChannelOption.SO_RCVBUF, 3*1024);
ChannelFuture sync = serverBootstrap.bind(port).sync();
sync.channel().closeFuture().sync();
bossGroup.shutdownGracefully();
wokerGroup.shutdownGracefully();
}
}
package com.demo.netty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
public class ServerHandler extends ChannelHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf b = (ByteBuf)msg;
byte [] by = new byte[b.readableBytes()];
b.readBytes(by);
System.out.println(new String(by));
ByteBuf buffer = ctx.alloc().buffer(50);
buffer.writeBytes("Hello Man".getBytes());
ctx.writeAndFlush(buffer);
}
}
package com.demo.netty;
import org.junit.Before;
import org.junit.Test;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class Client {
private int port;
@Before
public void init() {
this.port=8088;
}
@Test
public void run() throws Exception {
NioEventLoopGroup workerGroup = new NioEventLoopGroup();//客户端线程只需要指定一个,因为我们只要负责连接服务端就行
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture sync = bootstrap.connect("127.0.0.1",port).sync();
sync.channel().writeAndFlush(Unpooled.copiedBuffer("Hello Netty5".getBytes()));
sync.channel().closeFuture().sync();
}
}
package com.demo.netty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;
public class ClientHandler extends ChannelHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf b = (ByteBuf)msg;
try {
byte[] byt = new byte[b.readableBytes()];
b.readBytes(byt);
System.out.println(new String(byt));
}finally {
System.out.println("清除之前 "+b.readableBytes());
b.release();
System.out.println("清除之后 "+b.readableBytes());
}
}
}
Netty学习笔记(二)的更多相关文章
- Netty学习笔记(二) 实现服务端和客户端
在Netty学习笔记(一) 实现DISCARD服务中,我们使用Netty和Python实现了简单的丢弃DISCARD服务,这篇,我们使用Netty实现服务端和客户端交互的需求. 前置工作 开发环境 J ...
- Netty学习笔记(二)——netty组件及其用法
1.Netty是 一个异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端. 原生NIO存在的问题 1) NIO的类库和API繁杂,使用麻烦:需要熟练掌握Selector.Se ...
- [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计
源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...
- Netty 学习笔记(1)通信原理
前言 本文主要从 select 和 epoll 系统调用入手,来打开 Netty 的大门,从认识 Netty 的基础原理 —— I/O 多路复用模型开始. Netty 的通信原理 Netty 底层 ...
- Netty 学习(二):服务端与客户端通信
Netty 学习(二):服务端与客户端通信 作者: Grey 原文地址: 博客园:Netty 学习(二):服务端与客户端通信 CSDN:Netty 学习(二):服务端与客户端通信 说明 Netty 中 ...
- WPF的Binding学习笔记(二)
原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...
- AJax 学习笔记二(onreadystatechange的作用)
AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...
- JMX学习笔记(二)-Notification
Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...
- java之jvm学习笔记二(类装载器的体系结构)
java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式分为四种, 第一种,一次性解释代码,也就是当字节码转载到内存后,每次需要都会重新 ...
随机推荐
- 关于数据库timestamp类型问题
数据库使用timestamp类型字段,默人时间为0000-00-00 00:00:00 于是后台会报java.sql.SQLException: Value '0000-00-00 00:00:00' ...
- 四 : springMVC各种跳页面传值
第一种方式 : 返回值为String类型的跳转页面,犯法参数里面需要写Model modelimport org.springframework.ui.Model;包下的.返回String1):字符串 ...
- [国嵌攻略][071][Coredump故障分析]
Core Dump定义 Core Dump又叫核心转存.当程序在运行过程中发生异常,Linux系统可以把程序出错时的内存内容存储在一个core文件中,这个过程叫Core Dump. Core Dump ...
- 一步一步从原理跟我学邮件收取及发送 5.C语言的socket示例
说到 C 语言版本的程序,首先要解决的问题就是兼容性. 作为 20 年开发有 10 多年是在服务端的程序员,我深刻地感受到服务端平台的两极分化之严重,linux 派对 windows 那是超级的不屑一 ...
- Cannot declare class app\home\controller\Cases because the name is already in use
Cannot declare class app\home\controller\Cases because the name is already in use 命名空间冲突了 use 模型类的时候 ...
- [SinGuLaRiTy] NOIP2017 提高组
[SinGuLaRiTy-1048] Copyright (c) SinGuLaRiTy 2018. All Rights Reserved. NOIP2017过了这么久,现在2018了才找到寒假这么 ...
- [拾 得] 一枚迷人的贝壳 SHELL / Linux | shell 脚本初步入门
坚持知识分享,该文章由Alopex编著, 转载请注明源地址: http://www.cnblogs.com/alopex/ 索引: 什么是shell shell的分类 shell脚本的执行方式 ...
- struts配置json需要的jar包
- jquery 遍历表格,需要表格中每个td的内容
$("table tr:gt(0)").each(function(i){ alert("这是第"+i+"行内容"); $(this).ch ...
- Java数据持久层框架 MyBatis之API学习一(简介)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...