netty是jboss公司开发的,基于异步的、事件驱动的网络应用程序框架,快速开发高性能、高可靠性的服务器和客户端程序

public class TimeServer {
private int port=;
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LineBasedFrameDecoder());
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new TimeServerHandler());
}
});
ChannelFuture f = b.bind(port).sync(); // (5)
System.out.println("TimeServer Started on 8080...");
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new TimeServer().run();
}
}
public class TimeServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { //
String request = (String) msg; //
String response = null;
if ("QUERY TIME ORDER".equals(request)) { //
response = new Date(System.currentTimeMillis()).toString();
} else {
response = "BAD REQUEST";
}
response = response + System.getProperty("line.separator"); //
ByteBuf resp = Unpooled.copiedBuffer(response.getBytes()); //
ctx.writeAndFlush(resp); //
}
}
public class TimeClient {
public static void main(String[] args) throws Exception {
String host = "localhost";
int port = ;
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap(); // (1)
b.group(workerGroup); // (2)
b.channel(NioSocketChannel.class); // (3)
b.handler(new ChannelInitializer<SocketChannel>() {// (4)
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LineBasedFrameDecoder());
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new TimeClientHandler());
}
});
// Start the client.
ChannelFuture f = b.connect(host, port).sync(); // (5)
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
}
public class TimeClientHandler extends ChannelInboundHandlerAdapter {
private byte[] req = ("QUERY TIME ORDER" + System.getProperty("line.separator")).getBytes(); @Override
public void channelActive(ChannelHandlerContext ctx) throws InterruptedException {//
sendMsg(ctx);
} public void sendMsg(ChannelHandlerContext ctx) {
Executors.newSingleThreadExecutor().submit(new MsgRunnable(ctx));
} class MsgRunnable implements Runnable {
ChannelHandlerContext ctx; MsgRunnable(ChannelHandlerContext ctx) {
this.ctx = ctx;
} @Override
public void run() {
while (true) {
ByteBuf message = Unpooled.buffer(req.length);
message.writeBytes(req);
ctx.writeAndFlush(message);
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
} } }
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String) msg;
System.out.println("Now is:" + body);
}
}

netty例子-客户端每隔5秒发送查询时间的请求,服务器端响应请求的更多相关文章

  1. 我写的websocket推送例子,每隔5秒服务器向客户端浏览器发送消息(node.js和浏览器)

    node.js服务端 先要安装ws模块的支持 npm install ws 服务端(server.js) var gws; var WebSocketServer = require('ws').Se ...

  2. 关于selenium中的sendKeys()隔几秒发送一个字符

    看一下你的IEDriverServer.exe是不是64位的,我也遇到了这样的问题,换成32位的IEDriverServer.exe,瞬间速度快了

  3. 3、netty第二个例子,使用netty建立客户端,与服务端通讯

    第一个例子中,建立了http的服务器端,可以直接使用curl命令,或者浏览器直接访问. 在第二个例子中,建立一个netty的客户端来主动发送请求,模拟浏览器发送请求. 这里先启动服务端,再启动客户端, ...

  4. 上,打开SSH服务的配置文件:/etc/ssh/sshd_config 加上如下两行: ClientAliveInterval 120 ClientAliveCountMax 720 第一行,表示每隔120秒向客户端

    SSH的默认过一段时间会超时,有时候正在执行着脚本,出去一会回来就断开了,输出信息都看不到了... 禁止SSH自动超时最简单的办法就是,每隔一段时间在客户端和服务器之间发送一个"空包&quo ...

  5. Netty实现客户端和服务端通信简单例子

    Netty是建立在NIO基础之上,Netty在NIO之上又提供了更高层次的抽象. 在Netty里面,Accept连接可以使用单独的线程池去处理,读写操作又是另外的线程池来处理. Accept连接和读写 ...

  6. Unary模式下客户端从开始连接到发送接收数据的主要流程

    (原创)C/C/1.25.0-dev grpc-c/8.0.0, 使用的例子是自带的例子GreeterClient grpc Unary模式下客户端从开始连接到发送数据的主要流程 graph TD; ...

  7. android应用内存占用测试(每隔一秒打印procrank的信息)

    1.内存占用 对于智能手机而言,内存大小是固定的:因此,如果单个app的内存占用越小,手机上可以安装运行的app就越多:或者说app的内存占用越小,在手机上运行就会越流畅.所以说,内存占用的大小,也是 ...

  8. 每隔一秒自动执行函数(JavaScript)

    http://www.cnblogs.com/xlx0210/archive/2010/03/19/1689497.html 1. setInterval() ——每隔一秒自动执行方法,setInte ...

  9. 使用C#解决部分Win8.1系统窗口每隔几秒失去焦点的问题【转】

    使用了Win8.1 With Update 1后,发现重启系统后,当前激活的窗口总是每隔几秒失去焦点,过0.5~1秒焦点回来,导致输入无法正常工作,严重影响使用心情和效率. 在网上找了很久,也没找到相 ...

随机推荐

  1. 大文件上传组件webupload插件

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  2. ent 基本使用 二 简单create && query

    接上文,前边我们了解了关于基本代码生成以及schema 迁移的学习,下边我们看看基本的数据操作 参考代码: https://github.com/rongfengliang/ent-demo 环境准备 ...

  3. ksh与bash的异同

    (1) 在ksh是,数组的index只能从0到1023,而bash中没有这样的限制. (2) ksh与bash初始化数组的语法不同: 如下所示 icymoon# ksh icymoon# set -A ...

  4. JS的ES5的扩展内容

    JS的ES5 1.严格模式: (1)什么是严格模式: 在全局或函数的第一条语句定义为:  'use strict' 如果浏览器不支持, 只解析为一条简单的语句, 没有任何副作用 (2)严格模式作用: ...

  5. 05-树8 File Transfer (25 分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  6. flag&to do list&note

    没错,今天我要立几个看起来可能会倒的 flag 今天白天 早上除非有特殊情况,不许再看我的博客.不许再看我的qq空间.不许再跟别人聊闲话!!!☑已达成 今天早上一定要坚持做题,把昨天问老师的问题搞懂, ...

  7. Eureka 的高级使用

    基础架构Eureka架构中的三个核心角色: 服务注册中心 Eureka的服务端应用,提供服务注册和发现功能,就是刚刚我们建立的eureka-demo 服务提供者 提供服务的应用,可以是SpringBo ...

  8. Spring系列:下载

    Spring 官网:http://projects.spring.io/spring-framework/ Spring下载地址:https://repo.spring.io/simple/libs- ...

  9. Elasticsearch Java Rest Client简述

    ESJavaClient的历史 JavaAPI Client 优势:基于transport进行数据访问,能够使用ES集群内部的性能特性,性能相对好 劣势:client版本需要和es集群版本一致,数据序 ...

  10. kafka安装运行(docker)

    zookeeper参照https://www.cnblogs.com/wintersoft/p/11128484.html mkdir /opt/kafka -pvim /opt/kafka/Dock ...