2023-05-29 10:49:37 阅读次数:144
java, netty, 服务器
效果图
依赖
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.36.Final</version>
</dependency>
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class client {
public static void main(String[] args) {
final ExecutorService pool = Executors.newSingleThreadExecutor();
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>(){
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline=socketChannel.pipeline();
pipeline.addLast(new SimpleChannelInboundHandler<ByteBuf>() {
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
System.out.println("服务器已上线!!!");
pool.execute(new Runnable() {
public void run() {
Scanner scanner=new Scanner(System.in);
String text="";
while (true){
text=scanner.nextLine();
if(text.toLowerCase().equals("bye")){
ctx.close();
}
ctx.writeAndFlush(Unpooled.copiedBuffer(text+ "\r\n ", Charset.forName("utf-8")));
System.out.println(new Date().toString()+" 【客户端】:"+text);
}
}
});
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
String str=byteBuf.toString(CharsetUtil.UTF_8);
System.out.println(new Date().toString()+"【服务器】:"+str);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
System.out.println("连接断开!!!");
System.exit(0);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
}
});
}
}); //自定义一个初始化类
ChannelFuture channelFuture = bootstrap.connect("localhost", 7000).sync();
System.out.println("客户端已启动");
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.CharsetUtil;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class server {
public static void main(String[] args) {
final ExecutorService pool = Executors.newSingleThreadExecutor();
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(3);
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).childHandler(new SimpleChannelInboundHandler<ByteBuf>() {
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
System.out.println("客户端已上线!!!");
pool.execute(new Runnable() {
public void run() {
Scanner scanner=new Scanner(System.in);
String text="";
while (true){
text=scanner.nextLine();
if(text.toLowerCase().equals("bye")){
ctx.close();
}
ctx.writeAndFlush(Unpooled.copiedBuffer(text+ "\r\n ", Charset.forName("utf-8")));
System.out.println(new Date().toString()+" 【服务器】:"+text);
}
}
});
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
String str=byteBuf.toString(CharsetUtil.UTF_8);
System.out.println(new Date().toString()+"【客户端】:"+str);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
System.out.println("连接断开!!!");
System.exit(0);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
}
}); //自定义一个初始化类
ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();
System.out.println("服务器已启动");
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
上一篇:TCP 客户端程序开发
下一篇: MySQL数据库介绍、安装(服务端软件安装、客户端软件安装(图形化界面客户端和命令行客户端))
这部分内容对 Confluence 的默认日志表现进行描述并且假设你没有对 Confluence 的默认日志配置进行修改。为了统一在不同平台中的日志输出,Confluence 使用 atlassian-confluence.log 为主要日志,而不使用应用服务器日志。
Jenkins 编译的时候提示错误 JAVA_HOME environment
有时候在调试的时候,我们希望我们的代码能够打印出需要测试的一些变量。这个时候我们就会用到这个语句了,当然我们不希望从头开始打印,那么我们可以在输入对话框中输入 sout 通过上面的代码能够帮助你少打几个字符。
在使用 IntelliJ IDEA 完成代码修改准备进行提交的时候,我们可能想知道曾经自己提交的内容是什么。比如说想知道上次提交的代码关联的 TASK ID 是多少等。
Java Arrays.asList 和 new ArrayList(Arrays.asList()) 的对比
给定一个非负数组arr,长度为N,那么有N-1种方案可以把arr切成左右两部分,每一种方案都有,min{左部分累加和,右部分累加和},求这么多方案中,min{左部分累加和,右部分累加和}的最大值是多少?
dremio 的 joust 的作用
语法补全功能,比如"as soon as possible", 当我们识别到"as soon as"时, 基本即可判定用户需要键入"possible"。
给定区间的范围[xi,yi],xi<=yi,且都是正整数, 找出一个坐标集合set,set中有若干个数字, set要和每个给定的区间,有交集。 求set的最少需要几个数。
Spring security CSRF 跨域访问限制问题
文章
17872
阅读量
2031597
2024-05-09 09:19:54
2024-05-08 07:02:21
2024-04-24 09:23:22
2024-04-24 09:23:22
2024-04-24 09:23:22
2024-04-24 09:23:22
2023-03-01 02:55:22
2023-04-25 10:23:19
2023-02-28 03:38:08
2022-11-08 07:33:17
2022-11-14 02:56:39
2023-03-07 06:59:32
随时自助获取、弹性伸缩的云服务器资源
便捷、安全、高效的云电脑服务
高品质、低成本的云上存储服务
为云上计算资源提供持久性块存储
深圳SEO优化公司果洛英文网站建设价格四平网站建设昆明企业网站建设报价漯河企业网站制作哪家好淮北SEO按天计费哪家好坑梓网站建设塘坑百度网站优化排名报价清徐阿里店铺运营松岗建网站报价遵义企业网站设计哪家好洛阳阿里店铺托管报价金昌网站推广系统哪家好固原网站优化多少钱聊城百度标王哪家好漳州百搜标王哪家好宜春企业网站建设达州网站设计推荐黄南关键词排名推荐肇庆设计公司网站公司松岗百姓网标王推广公司南澳网站搭建价格广东网站优化按天计费价格无锡网页设计推荐大庆网站建设设计多少钱哈密网站优化排名公司哈尔滨如何制作网站公司哈尔滨网页制作多少钱宁德建设网站哪家好运城优化价格珠海网站优化按天计费报价歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运