iosapp做网站,排版设计网站有哪些,ps怎么制作网页教程,做网站开发所需的知识技能BIO : 同步阻塞I/O#xff08;Block IO#xff09; 服务器实现模式为每一个连接一个线程#xff0c;即客户端有连接请求时服务器就需要启动一个线程进行处理#xff0c;如果这个连接不做任何事情会造成不必要的线程开销#xff0c;此处可以通过线程池机制进行优化。
impo…BIO : 同步阻塞I/OBlock IO 服务器实现模式为每一个连接一个线程即客户端有连接请求时服务器就需要启动一个线程进行处理如果这个连接不做任何事情会造成不必要的线程开销此处可以通过线程池机制进行优化。
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;/*** 多人聊天室 - 服务端*/
public class BioServer {static ListSocket clientList new ArrayList();public static void main(String[] args) throws Exception {int port 8080;ServerSocket serverSocket new ServerSocket(port);while (true) {Socket client serverSocket.accept();System.out.println(客户端: client.getPort() 连接成功!);clientList.add(client);forwardProcess(client);}}/*** 转发处理*/public static void forwardProcess(Socket socket) {new Thread(new Runnable() {Overridepublic void run() {while (true) {forwardMsg(socket);}}}).start();}/*** 转发消息*/public static void forwardMsg(Socket socket) {try {String msg readMsg(socket);System.out.println(msg);for (Socket client : clientList) {if (client ! socket) {writeMsg(client, msg);}}} catch (IOException e) {throw new RuntimeException(e);}}/*** 写入消息*/public static void writeMsg(Socket socket, String msg) throws IOException {PrintStream ps new PrintStream(socket.getOutputStream());ps.println(msg);ps.flush();}/*** 读取消息*/public static String readMsg(Socket socket) throws IOException {InputStream inputStream socket.getInputStream();BufferedReader br new BufferedReader(new InputStreamReader(inputStream));String msg;if ((msg br.readLine()) ! null) {msg socket.getPort() 说: msg;}return msg;}} import java.io.*;
import java.net.Socket;
import java.util.Scanner;public class BilClient {public static void main(String[] args) throws IOException {String ip 127.0.0.1;int port 8080;Socket client new Socket(ip, port);readProcess(client);OutputStream os client.getOutputStream();PrintStream ps new PrintStream(os);Scanner scanner new Scanner(System.in);while (true) {String input scanner.nextLine();ps.println(input);ps.flush();}}/*** 读取处理*/public static void readProcess(Socket socket) {new Thread(() - {while (true) {try {System.out.println(readMsg(socket));} catch (IOException e) {throw new RuntimeException(e);}}}).start();}/*** 读取消息*/public static String readMsg(Socket socket) throws IOException {InputStream inputStream socket.getInputStream();BufferedReader br new BufferedReader(new InputStreamReader(inputStream));String msg;if ((msg br.readLine()) ! null) {}return msg;}
}NIO: 同步非阻塞式IO服务器实现模式为一个线程处理多个请求(连接)即客户端发送的连接请求会被注册到多路复用器上多路复用器轮询到有 I/O 请求就会进行处理。 Channel翻译过来就是“通道”就是数据传输的管道类似于“流”但是与“流”又有着区别。 既可以从Channel中读取数据又可以写数据到Channel但流的读写通常是单向的——输入输出流通道可以异步读写通道中的数据总是先读取到buffer缓冲区或者总是需要从一个buffer写入不能直接访问数据非阻塞特性Channel在设计上采用了非阻塞的特性它不会像传统的流一样在读写操作上阻塞线程而是立即返回结果告诉调用者当前的状态。这使得程序可以在等待数据准备的过程中同时进行其他操作实现了非阻塞IO。事件通知机制Channel通常搭配选择器Selector来使用选择器能够检测多个Channel的就绪状态如是否可读、可写等并通过事件通知例如轮询或回调及时地通知程序哪些Channel处于就绪状态从而可以进行相应的读写操作。这种机制支持程序实现异步IO模型。操作系统底层支持Channel的异步读写也依赖于操作系统底层的异步IO支持。Java NIO中的Channel实际上是对操作系统底层异步IO的封装和抽象利用了操作系统提供的异步IO机制来实现其自身的异步读写功能。Buffer是一个对象里面是要写入或者读出的数据在java.nio库中所有的数据都是用缓冲区处理的。 在读取数据时它是直接读到缓冲区中的在写入数据时也是直接写到缓冲区中任何时候访问Channel中的数据都是通过缓冲区进行操作的。缓冲区实质上是一个数组通常是一个字节数组ByteBuffer当然也有其他类型的Selector被称为选择器Selector会不断地轮询注册在其上的Channel如果某个Channel上发生读或写事件这个Channel就被判定处于就绪状态会被Selector轮询出来然后通过SelectionKey可以获取到就绪Channel的集合进行后续的I/O操作。 一个多路复用器Selector可以同时轮询多个ChannelJDK使用了epoll()代替了传统的select实现所以并没有最大连接句柄的限制这意味着只需要一个线程负责Selector的轮询就可以接入成千上万的客户端。 import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.Charset;
import java.util.Set;/*** NIO 聊天室 服务端*/
public class NioServer {private Integer port;ByteBuffer readBuffer ByteBuffer.allocate(1024);ByteBuffer writerBuffer ByteBuffer.allocate(1024);private Charset charset Charset.forName(UTF-8);public NioServer(Integer port) {this.port port;}public static void main(String[] args) {NioServer nioServer new NioServer(8080);nioServer.start();}private void start() {try {// 开启socketServerSocketChannel server ServerSocketChannel.open();// 设置非阻塞server.configureBlocking(false);// 绑定端口server.socket().bind(new InetSocketAddress(port));// 开启通道, 得到 Selector (选择器)Selector selector Selector.open();// 注册 selector 监听事件server.register(selector, SelectionKey.OP_ACCEPT);System.out.println(启动服务器 监听端口 port ...);while (true) {// 阻塞监控所有注册的通道,当有对应的事件操作时, 会将SelectionKey放入 集合内部并返回事件数量selector.select();// 返回存有SelectionKey的集合SetSelectionKey selectionKeys selector.selectedKeys();for (SelectionKey selectionKey : selectionKeys) {handle(selectionKey, selector);}// 处理后清理 selectionKeysselectionKeys.clear();}} catch (Exception e) {e.printStackTrace();}}/*** 事件处理*/private void handle(SelectionKey key, Selector selector) throws IOException {// SelectionKey 常用方法//isAcceptable() 是否是连接继续事件//isConnectable() 是否是连接就绪事件//isReadable() 是否是读就绪事件//isWritable() 是否是写就绪事件// SelectionKey 常用事件//SelectionKey.OP_ACCEPT 接收连接继续事件表示服务器监听到了客户连接服务器可以接收这个连接了//SelectionKey.OP_CONNECT 连接就绪事件表示客户端与服务器的连接已经建立成功//SelectionKey.OP_READ 读就绪事件表示通道中已经有了可读的数据可以执行读操作了通道目前有数据可以进行读操作了//SelectionKey.OP_WRITE 写就绪事件表示已经可以向通道写数据了通道目前可以用于写操作//处理连接if (key.isAcceptable()) {ServerSocketChannel server (ServerSocketChannel) key.channel();SocketChannel client server.accept();client.configureBlocking(false);client.register(selector, SelectionKey.OP_READ);System.out.println(client.socket().getPort() 已建立连接 ...);}// 读取消息else if (key.isReadable()) {SocketChannel client (SocketChannel) key.channel();String msg client.socket().getPort() 说: readMsg(client);System.out.println(msg);forwardMsg(msg, client, selector);}}/*** 读取通道消息*/private String readMsg(SocketChannel client) throws IOException {readBuffer.clear();while (client.read(readBuffer) 0) ;readBuffer.flip();return String.valueOf(charset.decode(readBuffer));}/*** 转发*/private void forwardMsg(String msg, SocketChannel client, Selector selector) throws IOException {for (SelectionKey key : selector.keys()) {Channel connectedClient key.channel();if (connectedClient instanceof ServerSocketChannel) {continue;}if (key.isValid() !client.equals(connectedClient)) {writerBuffer.clear();writerBuffer.put(charset.encode(msg));writerBuffer.flip();while (writerBuffer.hasRemaining())((SocketChannel) connectedClient).write(writerBuffer);}}}
} 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.nio.charset.Charset;
import java.util.Scanner;
import java.util.Set;/*** NIO 聊天室 客户端*/
public class NioClient {private String ip;private Integer port;private ByteBuffer writerBuffer ByteBuffer.allocate(1024);private ByteBuffer readBuffer ByteBuffer.allocate(1024);private Charset charset Charset.forName(UTF-8);public NioClient(String ip, Integer port) {this.ip ip;this.port port;}public static void main(String[] args) {NioClient nioClient new NioClient(127.0.0.1, 8080);nioClient.start();}public void start() {try {// 开启通道SocketChannel client SocketChannel.open();// 设置非阻塞client.configureBlocking(false);Selector selector Selector.open();client.register(selector, SelectionKey.OP_CONNECT);client.connect(new InetSocketAddress(ip, port));while (true) {selector.select();SetSelectionKey selectionKeys selector.selectedKeys();for (SelectionKey selectionKey : selectionKeys) {handle(selectionKey, selector);}selectionKeys.clear();}} catch (Exception e) {e.printStackTrace();}}private void handle(SelectionKey key, Selector selector) throws IOException {// 处理连接事件if (key.isConnectable()) {SocketChannel client (SocketChannel) key.channel();if (client.isConnectionPending()) {client.finishConnect();// 处理用户输入new Thread(() - {Scanner scanner new Scanner(System.in);while (true) {String msg scanner.nextLine();writerBuffer.clear();writerBuffer.put(charset.encode(msg));writerBuffer.flip();while (writerBuffer.hasRemaining()) {try {client.write(writerBuffer);} catch (IOException e) {throw new RuntimeException(e);}}}}).start();}client.register(selector, SelectionKey.OP_READ);}// 读取消息信息else if (key.isReadable()) {SocketChannel client (SocketChannel) key.channel();String s readMsg(client);System.out.println(s);}}private String readMsg(SocketChannel client) throws IOException {readBuffer.clear();while (client.read(readBuffer) 0) ;readBuffer.flip();return String.valueOf(charset.decode(readBuffer));}
}
文章转载自: http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.hpdpp.cn.gov.cn.hpdpp.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.wkws.cn.gov.cn.wkws.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.knpmj.cn.gov.cn.knpmj.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.xqbgm.cn.gov.cn.xqbgm.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.zlzpz.cn.gov.cn.zlzpz.cn http://www.morning.xbbrh.cn.gov.cn.xbbrh.cn http://www.morning.knswz.cn.gov.cn.knswz.cn http://www.morning.bnbzd.cn.gov.cn.bnbzd.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn http://www.morning.mwlxk.cn.gov.cn.mwlxk.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.slmbg.cn.gov.cn.slmbg.cn http://www.morning.nbpqx.cn.gov.cn.nbpqx.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.dfhkh.cn.gov.cn.dfhkh.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.rlwcs.cn.gov.cn.rlwcs.cn http://www.morning.kkrnm.cn.gov.cn.kkrnm.cn http://www.morning.21r000.cn.gov.cn.21r000.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.rwlns.cn.gov.cn.rwlns.cn http://www.morning.qyhcg.cn.gov.cn.qyhcg.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.xclgf.cn.gov.cn.xclgf.cn http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.fylqz.cn.gov.cn.fylqz.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.zdzgf.cn.gov.cn.zdzgf.cn http://www.morning.kghhl.cn.gov.cn.kghhl.cn http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.qpsxz.cn.gov.cn.qpsxz.cn http://www.morning.yhjlg.cn.gov.cn.yhjlg.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.ndynz.cn.gov.cn.ndynz.cn http://www.morning.ylklr.cn.gov.cn.ylklr.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.lmmh.cn.gov.cn.lmmh.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.fkffr.cn.gov.cn.fkffr.cn http://www.morning.gmyhq.cn.gov.cn.gmyhq.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.xxhc.cn.gov.cn.xxhc.cn http://www.morning.rdqzl.cn.gov.cn.rdqzl.cn http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.zrwlz.cn.gov.cn.zrwlz.cn http://www.morning.dljujia.com.gov.cn.dljujia.com http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn