netty 实现socket服务端编写
import java.net.InetSocketAddress; import io.netty.bootstrap.ServerBootstrap;
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; public class TimeServer { public static void main(String[] args) throws Exception{
new TimeServer().bind("192.168.1.102", 8400);
} public void bind(String addr,int port) {
//配置服务端的nio线程组
EventLoopGroup boosGroup=new NioEventLoopGroup();
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
ServerBootstrap b=new ServerBootstrap();
b.group(boosGroup,workerGroup);
b.channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG,1024)
.childHandler(new ChildChannelHandler());
//绑定端口,同步等待成功
ChannelFuture f=b.bind(new InetSocketAddress(addr, port)).sync();
//等等服务器端监听端口关闭
f.channel().closeFuture().sync();
} catch (Exception e) {
// TODO: handle exception
}finally{
boosGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
} private class ChildChannelHandler extends ChannelInitializer<SocketChannel>{ @Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new TimeServerHandler()); } } }
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext; public class TimeServerHandler extends ChannelHandlerAdapter { @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf=(ByteBuf)msg;
byte[] bytes=new byte[buf.readableBytes()];
buf.readBytes(bytes);
String body=new String(bytes,"UTF-8");
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=dateFormat.format(new Date());
String res="来自与服务端的回应,时间:"+ time;
ByteBuf resp=Unpooled.copiedBuffer(res.getBytes());
ctx.write(resp); } @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
} }
这个比传统的nio好用多了,netty的版本为netty-all-5.0.0.Alpha1.jar
天天学习,天天进步
netty 实现socket服务端编写的更多相关文章
- 基于netty的socket服务端触发了channelInactive方法,但实际连接没有断开的问题
背景: 一个中小型H5游戏,后端使用基于 netty 的socket服务 服务端 分为 分发服务器 & 业务服务器,业务服务器可负载 用户客户端与分发服务器连接 分发服务器再作为客户端与每台业 ...
- 在python中编写socket服务端模块(二):使用poll或epoll
在linux上编写socket服务端程序一般可以用select.poll.epoll三种方式,本文主要介绍使用poll和epoll编写socket服务端模块. 使用poll方式的服务器端程序代码: i ...
- Netty 搭建 WebSocket 服务端
一.编码器.解码器 ... ... @Autowired private HttpRequestHandler httpRequestHandler; @Autowired private TextW ...
- AutoCAD.net支持后台线程-Socket服务端
最近因为公司项目的需求,CAD作为服务端在服务器中常驻运行,等待客户端远程发送执行任务的指令,最终确认用Socket-tcp通讯,CAD需要实时监听客户端发送的消息,这时就需要开启线程执行Socket ...
- 使用NewLife网络库构建可靠的自动售货机Socket服务端(一)
最近有个基于tcp socket 协议和设备交互需求,想到了新生命团队的各种组件,所以决定用NewLife网络库作为服务端来完成一系列的信息交互. 第一,首先说一下我们需要实现的功能需求吧 1,首先客 ...
- 第一个socket服务端程序
第一个socket服务端程序 #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...
- C# Socket服务端与客户端通信(包含大文件的断点传输)
步骤: 一.服务端的建立 1.服务端的项目建立以及页面布局 2.各功能按键的事件代码 1)传输类型说明以及全局变量 2)Socket通信服务端具体步骤: (1)建立一个Socket (2)接收 ...
- MSDN上的异步socket 服务端例子
MSDN上的异步socket 服务端例子 2006-11-22 17:12:01| 分类: 代码学习 | 标签: |字号大中小 订阅 Imports SystemImports Syste ...
- 利用多线程使socket服务端可以与多个客户端同时通讯
利用多线程使socket服务端可以与多个客户端同时通讯 server import socket 1. 符合TCP协议的手机 server = socket.socket(socket.AF_INET ...
随机推荐
- DEDECMS中,channel标签
获取栏目列表标签 dede:channel 标签: {dede:channel type='top' row='8' currentstyle="<li><a href=' ...
- jsp链接数据库
数据库表代码: /*Navicat MySQL Data Transfer Source Server : localhost_3306Source Server Version : 50528Sou ...
- [DevExpress]ChartControl之SeriesTemplate示例
关键代码: using System; using System.Data; using System.Windows.Forms; using CSharpUtilHelpV2; using Dev ...
- 开源 侧滑 和 Tab滑动翻页 控件
侧滑 https://github.com/jfeinstein10/SlidingMenu Tab滑动翻页 https://github.com/astuetz/PagerSlidingTabStr ...
- mysql中使用update select
UPDATE t_user INNER JOIN t_shake ON t_shake.user_id = t_user.user_id SET t_user.shake_total_num = t_ ...
- YII千万级PV架构经验分享--俯瞰篇--架构即产品
hello,大家好,我是方少,今天想问大家一个问题,如果我们自己就是产品,那么我们怎样才能说服别人,我们是最优秀的呢?高学历,不错,别人成功过了.会php,java,c#,python不行再学c++, ...
- sirius的学习笔记(2)
原文来自网络,侵权删 if both values of in a or expression are true ,Python will select the first one, and the ...
- 使用公钥登录SSL
在本地生成密钥对 ssh-keygen -t rsa 如果不想设置密码,可以直接点击回车. 如果你想使用DSA可以用-t DSA替换. 确保远程计算机上用户目录下有.ssh目录 确保你的连接服务器上的 ...
- Asp.Net MVC过滤器小试牛刀
在上学期间学习的Asp.Net MVC,基本只是大概马马虎虎的了解,基本处于知其然而不知其所以然.现在到上班,接触到真实的项目,才发现还不够用,于是从最简单的过滤器开始学习.不得不说MVC的过滤器真是 ...
- sharepoint warmup
/---------------- using System;using System.Collections.Generic; using System.Text;using System.Net; ...