当前位置: 首页 > news >正文

网站建设公司ttmwl临沂建设大型网站建设

网站建设公司ttmwl,临沂建设大型网站建设,哪些网站有中文域名,石家庄核酸检测1设计思路#xff1a;启动一个服务端#xff0c;多个客户端第一个客户端启动时#xff0c;会告诉服务器上线了第二个客户端启动时#xff0c;告诉服务器上线#xff0c;并且通知第一个启动的客户端第三个客户端启动时#xff0c;告诉服务器上线#xff0c;并且通知第一个…1设计思路启动一个服务端多个客户端第一个客户端启动时会告诉服务器上线了第二个客户端启动时告诉服务器上线并且通知第一个启动的客户端第三个客户端启动时告诉服务器上线并且通知第一个和第二个启动的客户端其中一个客户端离开时通知其它客户单端和服务端2 代码服务端import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder;/*** Author: liubujun* Date: 2023/2/15 10:31*/public class GroupChatServer {//监听端口private int port;public GroupChatServer(int port){this.port port;}//编写run方法处理客户端请求public void run() throws Exception{NioEventLoopGroup bossGroup new NioEventLoopGroup();NioEventLoopGroup workGroup new NioEventLoopGroup();try {ServerBootstrap b new ServerBootstrap();b.group(bossGroup,workGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG,128).childOption(ChannelOption.SO_KEEPALIVE,true).childHandler(new ChannelInitializerSocketChannel() {Overrideprotected void initChannel(SocketChannel ch) throws Exception {//获取到pipelineChannelPipeline pipeline ch.pipeline();//向pipeline加入解码器pipeline.addLast(decoder,new StringDecoder());//向pipeline加入编码器pipeline.addLast(encoder,new StringEncoder());//加入自己的业务处理handlepipeline.addLast(new GroupChatServerHandle());}});System.out.println(netty 服务器启动);ChannelFuture channelFuture b.bind(port).sync();//监听关闭channelFuture.channel().closeFuture().sync();}finally {bossGroup.shutdownGracefully();workGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception {new GroupChatServer(7000).run();} }服务端处理器import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.DefaultChannelProgressivePromise; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.DefaultChannelGroup; import io.netty.util.concurrent.GlobalEventExecutor; import jdk.nashorn.internal.runtime.GlobalConstants;import java.text.SimpleDateFormat; import java.util.Date;/*** Author: liubujun* Date: 2023/2/15 14:49*/public class GroupChatServerHandle extends SimpleChannelInboundHandlerString {//定义一个channel组管理所有的channel//GlobalEventExecutor.INSTANCE 是全局事件的执行器是一个单例private static ChannelGroup channelGroup new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);SimpleDateFormat sf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);//handlerAdded表示连接建立一旦连接表示第一个被执行//将当前channel加入到channelGroupOverridepublic void handlerAdded(ChannelHandlerContext ctx) throws Exception {Channel channel ctx.channel();//将该客户加入的聊天信息推送给其他在线的客户端/*** 该方法会将channelGroup中所有的channel遍历并发送消息*/channelGroup.writeAndFlush([客户端]channel.remoteAddress()加入聊天sf.format(new Date())\n);channelGroup.add(channel);}//断开连接将xx客户离开信息推送给其他在线的客户端Overridepublic void handlerRemoved(ChannelHandlerContext ctx) throws Exception {Channel channel ctx.channel();channelGroup.writeAndFlush([客户端]channel.remoteAddress()离开了);System.out.println(channelGroup sizechannelGroup.size());}//表示channel处于活动状态提示xxx上线Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {System.out.println(ctx.channel().remoteAddress()上线了~);}//表示channel处于非活动状态提示xxx离线Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {System.out.println(ctx.channel().remoteAddress()离线了~);}Overrideprotected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {//获取到当前的channelChannel channel ctx.channel();//这时我们遍历channelGroup,根据不同的情况回送不同的消息channelGroup.forEach(ch-{if (channel ! ch ){ //不是当前的channel转发消息ch.writeAndFlush([客户]channel.remoteAddress()发送了消息msg \n);}else {ch.writeAndFlush([自己]发送了消息msg\n);}});}Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {ctx.close();} }客户端代码:import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder;/*** Author: liubujun* Date: 2023/2/15 10:31*/public class GroupChatServer {//监听端口private int port;public GroupChatServer(int port){this.port port;}//编写run方法处理客户端请求public void run() throws Exception{NioEventLoopGroup bossGroup new NioEventLoopGroup();NioEventLoopGroup workGroup new NioEventLoopGroup();try {ServerBootstrap b new ServerBootstrap();b.group(bossGroup,workGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG,128).childOption(ChannelOption.SO_KEEPALIVE,true).childHandler(new ChannelInitializerSocketChannel() {Overrideprotected void initChannel(SocketChannel ch) throws Exception {//获取到pipelineChannelPipeline pipeline ch.pipeline();//向pipeline加入解码器pipeline.addLast(decoder,new StringDecoder());//向pipeline加入编码器pipeline.addLast(encoder,new StringEncoder());//加入自己的业务处理handlepipeline.addLast(new GroupChatServerHandle());}});System.out.println(netty 服务器启动);ChannelFuture channelFuture b.bind(port).sync();//监听关闭channelFuture.channel().closeFuture().sync();}finally {bossGroup.shutdownGracefully();workGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception {new GroupChatServer(7000).run();} }客户端处理器import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler;/*** Author: liubujun* Date: 2023/2/15 16:51*/public class GroupChatClientHandle extends SimpleChannelInboundHandlerString {Overrideprotected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {System.out.println(msg.trim());} } 演示先启动服务端启动第一个客户端服务端和客服端控制台输出如下启动第二个客户端服务端、客服端1、客户端2控制台输出如下启动第三个客户端服务端控制台输出如下客户端1、客户端2、客户端3输出如下关闭其中一个客户端我这边关闭的是第二个服务端输出如下三个客户端打印台输出如下看到结果发现符合当初的设计。注意打开多个客户端需要在idea中配置
http://www.hkea.cn/news/14447459/

相关文章:

  • 联想公司网站建设现状无限成都成都市广播电视台官方网站
  • 网站建设销售工作怎么样瑞诺国际的数字营销模式
  • 五莲网站建设网络广告策划的内容
  • 做2手物品通过网站去卖掉好做吗木地板企业网站模版
  • 青岛网站建设seo优化凡科的网站怎么仿
  • 遵义市双控体系建设网站徐州焊接球网架公司
  • 机电建设工程施工网站百度关键词点击价格查询
  • 深圳本地做网站android开发基础教程
  • 建设人才证书查询网站孩子学编程的利弊
  • 人力招聘网站建设任务执行书上海外贸公司招聘职位
  • 金华金义东轨道建设网站织梦中二次开发新的网站
  • 河南省建设厅官方网站李学军网站建设与发布需要什么
  • 深圳做微信网站建设分发平台
  • 手机wap网站如何建设网站建设咨询云尚网络
  • 自己怎么建网站一般网站的后台
  • 东乡族网站建设智慧团建pc端入口
  • 网站重要组成部分网站安装部署
  • 网站建设的行业代码是多少小程序短链接生成网址
  • 做网站去什么公司重庆安全监督工程信息网
  • 找别人做网站 自己管理网站查询关键词排名软件
  • 搭建网站要多久wdcp 网站备份
  • 杭州市建设职业中心网站中国十大广告公司排行榜
  • 网站开发什么技术网站开发员属于
  • wordpress安装 linux漯河seo推广
  • 东莞网站seo优化托管网上怎么查自己的房屋结构图
  • 济宁网站建设软件开发米拓建站最新进展
  • 大连建设主管部门网站做翻译网站 知乎
  • php如何给网站做支付接口在线注册
  • 做淘宝网站的如何做分类网站信息营销
  • 寺庙网站素材东莞专业全网推广建站公司