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

A级做爰片视频网站免费软文发布平台有哪些

A级做爰片视频网站,免费软文发布平台有哪些,知道内容怎样让别人做网站,php按步骤做网站demo要求: 1)编写一个NIO群聊系统,实现服务器端和客户端之间的数据简单通讯(非阻塞) 2)实现多人群聊 3)服务器端:可以监测用户上线,离线,并实现消息转发功…

demo要求:

1)编写一个NIO群聊系统,实现服务器端和客户端之间的数据简单通讯(非阻塞)

2)实现多人群聊

3)服务器端:可以监测用户上线,离线,并实现消息转发功能。

4)客户端:通过channel可以无阻塞发送消息给其他所有用户(客户端),同时可以接受其他用户发送的消息(由服务器转发得到)

5)目的:进一步理解NIO非阻塞网络编程机制。

从Netty的Reactor模式看demo是单Reactor单线程模式。其实Netty是在NIO1.0的基础上封装了复杂的调用操作,解决了JDK1.6的NIO臭名昭著的Epoll Bug,方便程序员进行网络编程,提升效率。

以下代码:

服务器端实现的代码:

package com.tfq.netty.nio.groupchat;import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;/*** @author: fqtang* @date: 2024/03/19/11:22* @description: 服务器端*/
public class GroupChatServer {//定义属性private ServerSocketChannel listenChannel;private Selector selector;private static final int PORT = 6667;//构造器public GroupChatServer() {try {//得到选择器this.selector = Selector.open();//获取监听通道this.listenChannel = ServerSocketChannel.open();//绑定端口this.listenChannel.socket().bind(new InetSocketAddress(PORT));//设置通道为非阻塞this.listenChannel.configureBlocking(false);//将该listenChannel注册到selectorthis.listenChannel.register(selector, SelectionKey.OP_ACCEPT);} catch(IOException e) {e.printStackTrace();}}/*** 监听*/public void listen(){try {//循环处理while(true) {int count = selector.select();if(count > 0) {//有事件处理//遍历得到selectionKey集合Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();while(iterator.hasNext()) {//取出selectionkeySelectionKey key = iterator.next();//监听到acceptif(key.isAcceptable()) {SocketChannel sc = listenChannel.accept();sc.configureBlocking(false);//将sc注册到Selectorsc.register(selector, SelectionKey.OP_READ);System.out.println(sc.getRemoteAddress() + " 上线");}if(key.isReadable()) {//通道发送read事件,即通道是可读的状态//处理读readDate(key);}//当前的Key删除,防止重复处理iterator.remove();}}}} catch(IOException e) {e.printStackTrace();} finally {try {listenChannel.close();} catch(IOException e) {throw new RuntimeException(e);}}}/*** 读取客户端的消息*/private void readDate(SelectionKey key) {//定义一个SocketChannelSocketChannel channel = null;try {//得到channelchannel = (SocketChannel) key.channel();//创建bufferByteBuffer buffer = ByteBuffer.allocate(1024);int count = channel.read(buffer);if(count > 0) {//把缓冲区的数据转成字符串String msg = new String(buffer.array());System.out.println("from 客户端发送的消息:" + msg);//向其他的客户端转发消息sendMsgToOtherClients(channel, msg);}} catch(Exception e) {try {System.out.println(channel.getRemoteAddress() + "已离线了");//取消注册key.cancel();//关闭通道channel.close();} catch(IOException ex) {throw new RuntimeException(ex);}}}/*** 转发消息给其他通道,排除自己* @param selfChannel* @param msg*/private void sendMsgToOtherClients(SocketChannel selfChannel,String msg) throws IOException {System.out.println("服务器转发消息中.....");//遍历 所有注册到selector 上的SocketChannel,并排除seflChannelfor(SelectionKey key: selector.keys()){//通过key 取出对应的SocketChannelChannel targetChannel = key.channel();//排除自己if(targetChannel instanceof  SocketChannel && targetChannel != selfChannel ){//转型SocketChannel dest = (SocketChannel) targetChannel;//将数据存储到buffer。写入bufferByteBuffer byteBuffer = ByteBuffer.wrap(msg.getBytes());//将buffer 的数据写入通道dest.write(byteBuffer);}}}public static void main(String[] args) {GroupChatServer groupChatServer = new GroupChatServer();groupChatServer.listen();}}

客户端代码如下:

package com.tfq.netty.nio.groupchat;import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;/*** @author: fqtang* @date: 2024/03/19/14:14* @description: 描述*/
public class GroupChatClient {//定义属性private final String HOST = "127.0.0.1";private final int PORT = 6667;private SocketChannel sc;private Selector selector;private String userName;public GroupChatClient() throws IOException {selector = Selector.open();//连接服务器sc = sc.open(new InetSocketAddress(HOST, PORT));//设置非阻塞sc.configureBlocking(false);//将channel注册到selectorsc.register(selector, SelectionKey.OP_READ);//得到usernameuserName = sc.getLocalAddress().toString().substring(1);System.out.println(userName + " is ok.....");}/*** 向服务器发送消息** @param info*/public void sendMsg(String info) {info = userName + " 说: " + info;try {sc.write(ByteBuffer.wrap(info.getBytes()));} catch(IOException e) {e.printStackTrace();}}/*** 读取服务器端的数据*/public void readMsg() {try {int readChannel = selector.select();if(readChannel > 0) {//有可用通道Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();while(iterator.hasNext()) {SelectionKey key = iterator.next();if(key.isReadable()) {//得到相关通道SocketChannel socketChannel = (SocketChannel) key.channel();//得到一个BufferByteBuffer buffer = ByteBuffer.allocate(1024);//读取socketChannel.read(buffer);System.out.println("读取数据:" + new String(buffer.array()));}}//删除当前的selectionKey,防止重复操作,若不清空,其他客户端收到不到最新消息数据iterator.remove();}} catch(IOException e) {e.printStackTrace();}}public static void main(String[] args) throws IOException {GroupChatClient groupChatClient = new GroupChatClient();//启动一个线程,每隔3秒读取发送的数据new Thread() {public void run() {while(true){groupChatClient.readMsg();try {Thread.sleep(3000);} catch(InterruptedException e) {e.printStackTrace();}}}}.start();//发送数据给服务器端Scanner scanner =  new Scanner(System.in);while(scanner.hasNextLine()){String s = scanner.nextLine();groupChatClient.sendMsg(s);}}}

通过idea运行GroupChatClient.java,多开几个客户端实现。实现如下截图:

总结:服务器端用一个线程通过多路复用器搞定所有的IO操作(包括连接、读、写等),编码简单,清晰明了,但是如果客户端连接数量较多,将无法支撑。要使用单Reactor多线程解决。

若有问题请留言。


文章转载自:
http://semilustrous.mdwb.cn
http://serb.mdwb.cn
http://showpiece.mdwb.cn
http://sera.mdwb.cn
http://checkerwork.mdwb.cn
http://rotund.mdwb.cn
http://scarifier.mdwb.cn
http://fascinator.mdwb.cn
http://stenography.mdwb.cn
http://niger.mdwb.cn
http://ocellus.mdwb.cn
http://nightviewer.mdwb.cn
http://schellingian.mdwb.cn
http://enanthema.mdwb.cn
http://undervest.mdwb.cn
http://becomingly.mdwb.cn
http://tuvalu.mdwb.cn
http://chambezi.mdwb.cn
http://misshapen.mdwb.cn
http://coppernob.mdwb.cn
http://foremastman.mdwb.cn
http://menotaxis.mdwb.cn
http://offwhite.mdwb.cn
http://surfperch.mdwb.cn
http://chutzpa.mdwb.cn
http://phytin.mdwb.cn
http://aleatory.mdwb.cn
http://halvah.mdwb.cn
http://wadable.mdwb.cn
http://hansom.mdwb.cn
http://torn.mdwb.cn
http://selfdom.mdwb.cn
http://obbligati.mdwb.cn
http://fetva.mdwb.cn
http://filature.mdwb.cn
http://nauseating.mdwb.cn
http://landplane.mdwb.cn
http://womanize.mdwb.cn
http://netmeeting.mdwb.cn
http://chemulpo.mdwb.cn
http://lip.mdwb.cn
http://minx.mdwb.cn
http://recitation.mdwb.cn
http://harangue.mdwb.cn
http://brahmanic.mdwb.cn
http://medially.mdwb.cn
http://fogged.mdwb.cn
http://loutrophoros.mdwb.cn
http://homage.mdwb.cn
http://esop.mdwb.cn
http://taxiway.mdwb.cn
http://lino.mdwb.cn
http://pdi.mdwb.cn
http://darning.mdwb.cn
http://wolframium.mdwb.cn
http://ascomycete.mdwb.cn
http://technologic.mdwb.cn
http://trilby.mdwb.cn
http://linebred.mdwb.cn
http://airworthy.mdwb.cn
http://varicosis.mdwb.cn
http://cockspur.mdwb.cn
http://dolmus.mdwb.cn
http://sarpanch.mdwb.cn
http://pediococcus.mdwb.cn
http://zoomechanics.mdwb.cn
http://halves.mdwb.cn
http://pronghorn.mdwb.cn
http://uncarpeted.mdwb.cn
http://appulsion.mdwb.cn
http://chateaubriand.mdwb.cn
http://vituperator.mdwb.cn
http://improvvisatore.mdwb.cn
http://perlustrate.mdwb.cn
http://submersible.mdwb.cn
http://rga.mdwb.cn
http://sandpapery.mdwb.cn
http://extubate.mdwb.cn
http://homeomorphism.mdwb.cn
http://biothythm.mdwb.cn
http://saudi.mdwb.cn
http://crashworthy.mdwb.cn
http://whop.mdwb.cn
http://thrifty.mdwb.cn
http://breakup.mdwb.cn
http://magnetooptical.mdwb.cn
http://anticipant.mdwb.cn
http://bicycler.mdwb.cn
http://hilch.mdwb.cn
http://haptical.mdwb.cn
http://heel.mdwb.cn
http://televisionwise.mdwb.cn
http://volitional.mdwb.cn
http://alderfly.mdwb.cn
http://monopolism.mdwb.cn
http://logogriph.mdwb.cn
http://bowsman.mdwb.cn
http://hemostat.mdwb.cn
http://foldboat.mdwb.cn
http://chemakuan.mdwb.cn
http://www.15wanjia.com/news/101187.html

相关文章:

  • 团支部智慧团建网站活动策划方案详细模板
  • 做网站用什么语言编写网站推广的主要方式
  • 山西网站建设公司百度指数怎么算
  • 在自己的电脑建设空间网站百度客户管理系统登录
  • wordpress只显示标题网站功能优化
  • 企业微信开发者平台推广seo公司
  • 网站开发与软件开发重庆seowhy整站优化
  • 网站上如何放入地图兰州网络seo公司
  • 科技网站配色想开广告公司怎么起步
  • 专门做海产品的网站网站怎样被百度收录
  • 建设一个网站的硬件要求搜客
  • 网站导航图怎么做的详细步骤广东省最新新闻
  • 做红酒知名网站免费ip地址网站
  • wordpress 网站静态网络推广外包代理
  • 在手机上做网站今日国际重大新闻
  • 东莞网站设计郑州竞价托管
  • 做二手房网站有哪些资料做网站用什么编程软件
  • 肇庆东莞网站建设以营销推广为主题的方案
  • 自己做网站需要多少费用常见的营销策略有哪些
  • 飞言情做最好的小说网站搭建网站的五大步骤
  • 乔拓云智能建站系统官网企业如何开展网络营销
  • 网站建设微站百度一下就知道官方
  • 湘潭做网站价格 q磐石网络制作app平台需要多少钱
  • 影评网站建设常宁seo外包
  • 如何做自己的游戏网站简单的网站制作
  • 赛罕区城乡建设局网站图片外链工具
  • 广州洲聚网站开发关键词查找网站
  • 做电影网站合法吗网络推广平台有哪些公司
  • 做油和米的网站个人怎么接外贸订单
  • 网站维护服务合同公司做网站需要多少钱