标签:div 实例 定义 decode channels data can sre open
1 package newtest; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.net.InetSocketAddress; 7 import java.net.Socket; 8 import java.nio.ByteBuffer; 9 import java.nio.channels.Channel; 10 import java.nio.channels.SelectionKey; 11 import java.nio.channels.Selector; 12 import java.nio.channels.ServerSocketChannel; 13 import java.nio.channels.SocketChannel; 14 import java.nio.charset.Charset; 15 16 public class TcpServer 17 { 18 //用于检测所有Channel状态的selector 19 private Selector selector = null; 20 //定义编码格式 21 private Charset charset = Charset.forName("UTF-8"); 22 23 public void init()throws IOException 24 { 25 selector = Selector.open(); 26 //通过open方法来打开一个未绑定的ServerSocketChannel实例 27 ServerSocketChannel server = ServerSocketChannel.open(); 28 InetSocketAddress isa = new InetSocketAddress("127.0.0.1", 8899); 29 //将该ServerSocketChannel绑定到指定IP地址 30 server.socket().bind(isa); 31 //设置ServerSocket以非阻塞方式工作 32 server.configureBlocking(false); 33 //将Server注册到指定Selector对象 34 server.register(selector, SelectionKey.OP_ACCEPT); 35 36 while(selector.select() > 0) 37 { 38 //依次处理Selector上的每个已选择的SelectionKey 39 for(SelectionKey sk : selector.selectedKeys()) 40 { 41 //从selector上的已选择Key集中删除正在处理的SelectionKey 42 selector.selectedKeys().remove(sk); 43 //如果sk对应的通道包含客户端的连接请求 44 if(sk.isAcceptable()) 45 { 46 //调用accept方法接受连接,产生服务器端对应的SocketChannel 47 SocketChannel sc = server.accept(); 48 //设置非阻塞方式工作 49 sc.configureBlocking(false); 50 //将该SocketChnnel也注册到selector 51 sc.register(selector, SelectionKey.OP_READ); 52 //将sk对应的Channel设置成准备接受其它请求 53 sk.interestOps(SelectionKey.OP_ACCEPT); 54 } 55 //如果sk对应的通道有数据需要读取 56 if(sk.isReadable()) 57 { 58 //获取该SelectionKey对应的Channel,该Channnel中有可读的数据 59 SocketChannel sc = (SocketChannel)sk.channel(); 60 //定义准备执行读取数据的ByteBuffer 61 ByteBuffer buff = ByteBuffer.allocate(1024); 62 String content = ""; 63 //开始读数据 64 try 65 { 66 while(sc.read(buff) > 0) 67 { 68 buff.flip(); 69 content += charset.decode(buff); 70 } 71 //打印从该sk对应的Channel里读到的数据 72 System.out.println("消息:" + content); 73 //将sk对应的Channel设置成准备下一次读取 74 sk.interestOps(SelectionKey.OP_READ); 75 } 76 //如果捕捉到该sk对应的Channel出现了异常,即表明该Channel对应的Client出现了问题 77 //所以从Selector中取消sk的注册 78 catch(IOException ex) 79 { 80 //从Selector中删除指定的SelectionKey 81 sk.cancel(); 82 if(sk.channel() != null) 83 { 84 sk.channel().close(); 85 } 86 } 87 // throw new IOException(); 88 //如果content的长度大于0,即聊天信息不为空 89 if(content.length() > 0) 90 { 91 //遍历该selector里注册的所有selectionKey 92 for(SelectionKey key : selector.keys()) 93 { 94 //获取该channel是SocketChannel对象 95 Channel targetChannel = key.channel(); 96 if(targetChannel instanceof SocketChannel) 97 { 98 //将读到的内容写入该Channel中 99 SocketChannel dest = (SocketChannel)targetChannel; 100 dest.write(charset.encode(content)); 101 } 102 } 103 } 104 } 105 } 106 } 107 } 108 109 public static void main(String[] args)throws IOException 110 { 111 System.out.println("===========TCP的server端启动============="); 112 new TcpServer().init(); 113 } 114 }
package com.springboot.springbootswagger.client; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; public class TcpClient { // initialize socket and input output streams private Socket socket = null; private DataInputStream input = null; private DataOutputStream out = null; // constructor to put ip address and port public TcpClient(String address, int port) { // establish a connection try { socket = new Socket(address, port); System.out.println("Connected"); // takes input from terminal input = new DataInputStream(System.in); out = new DataOutputStream(socket.getOutputStream()); } catch(UnknownHostException u) { System.out.println(u.toString()); } catch(IOException i) { System.out.println(i); } // 待读入的String String line = ""; // 一直读到Over字符停止 while (!line.equals("Over")) { try { line = input.readLine(); out.writeUTF(line); } catch(IOException i) { System.out.println(i); } } // 关闭连接 try { input.close(); out.close(); socket.close(); } catch(IOException i) { System.out.println(i); } } public static void main(String args[]) { TcpClient client = new TcpClient("127.0.0.1", 8899); } }
标签:div 实例 定义 decode channels data can sre open
原文地址:https://www.cnblogs.com/chenyun-/p/11711500.html