标签:add span adp ima 发送数据 传输 输出 div ace
1 /* 2 * 客户端发送数据 通过channel通道 3 * */ 4 @Test 5 public void Client() throws IOException { 6 7 //获取channel通道 并设置主机号和端口号 8 SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8080)); 9 10 //因为使用非阻塞NIO 所以必须切换为非阻塞 11 socketChannel.configureBlocking(false); //默认为true 需要改为非堵塞的 12 13 //开辟缓冲区进行存储数据 14 ByteBuffer byteBuffer = ByteBuffer.allocate(1024); 15 16 //准备工作就绪后,准备发送数据给服务端 17 //打印当前日期转为Byte数据传出 18 byteBuffer.put(new Date().toString().getBytes()); 19 //切换读写模式 20 byteBuffer.flip(); 21 //写入通道 22 socketChannel.write(byteBuffer); 23 //完毕时,清除缓冲区内容 24 byteBuffer.clear(); 25 26 //==================== 27 //关闭相关流 28 socketChannel.close(); 29 30 }
LocalDateTime.now().toString().getBytes() //转为Byte字节
1 public static SocketChannel open(SocketAddress remote) 2 throws IOException 3 { 4 SocketChannel sc = open(); 5 try { 6 sc.connect(remote); //打开一个新的channel时,绑定连接到主机和端口上 7 } catch (Throwable x) { 8 try { 9 sc.close(); //异常时关闭连接 10 } catch (Throwable suppressed) { 11 x.addSuppressed(suppressed); 12 } 13 throw x; 14 } 15 assert sc.isConnected(); 16 return sc; 17 }
*/ public InetSocketAddress(String hostname, int port) { checkHost(hostname); //检查主机号是否为空 为空返回异常。 InetAddress addr = null; String host = null; try { addr = InetAddress.getByName(hostname); } catch(UnknownHostException e) { host = hostname; } holder = new InetSocketAddressHolder(host, addr, checkPort(port)); //检查端口。 }
//检查端口方法
private static int checkPort(int port) {
if (port < 0 || port > 0xFFFF)
throw new IllegalArgumentException("port out of range:" + port);
return port;
}
//检查主机号方法
private static String checkHost(String hostname) {
if (hostname == null)
throw new IllegalArgumentException("hostname can‘t be null");
return hostname;
}
1 /* 2 * 服务端接收客户端传来的数据 3 * */ 4 @Test 5 public void server() throws IOException { 6 7 //获取channel通道 8 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); 9 //切换为非堵塞状态 10 serverSocketChannel.configureBlocking(false); 11 //分配服务端的缓冲区 12 ByteBuffer serverByteBuffer = ByteBuffer.allocate(1024); 13 //将客户端的InetSocketAddress绑定到通道,不绑定 不统一将获取不到数据 14 serverSocketChannel.bind(new InetSocketAddress(8080)); 15 //获取选择器 16 Selector selector = Selector.open(); 17 //将通道注册到选择器中,并且制定监听方式 18 serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); 19 //进行轮询选择器上就绪成功的事件 当存在就绪成功的及进行下一步 20 while (selector.select() > 0){ 21 //对已存在的就绪事件进行迭代 22 Iterator<SelectionKey> selectionKeyIterator = selector.selectedKeys().iterator(); 23 24 //有元素就进行下一步 25 while (selectionKeyIterator.hasNext()){ 26 //获取到就绪事件 27 SelectionKey next = selectionKeyIterator.next(); 28 29 //对获取到的就绪事件判断是何种类型 30 if (next.isAcceptable()){ 31 32 //获取连接 33 SocketChannel accept = serverSocketChannel.accept(); 34 35 //将获取到的连接切换为非堵塞模式 36 accept.configureBlocking(false); 37 38 //将获取到的链接 注册金selector 39 accept.register(selector,SelectionKey.OP_READ); 40 41 //判断是否准备好读 42 }else if (next.isReadable()){ 43 44 //获取已就绪的通道 45 SocketChannel channel = (SocketChannel) next.channel(); 46 47 //分配缓冲区 48 ByteBuffer byteBuffer = ByteBuffer.allocate(1024); 49 50 //读取数据 51 int length = 0 ; 52 while ((length = channel.read(byteBuffer)) > 0){ 53 byteBuffer.flip(); 54 System.out.println(new String(byteBuffer.array(),0,length)); 55 byteBuffer.clear(); 56 } 57 58 59 } 60 61 //完成传输需要取消选择键,防止下次出问题 62 selectionKeyIterator.remove(); 63 64 } 65 } 66 67 68 }
Selector selector = Selector.open();
public static Selector open() throws IOException { return SelectorProvider.provider().openSelector(); } //首先进入此方法判断是否存在选择器 public static SelectorProvider provider() { synchronized (lock) { if (provider != null) //第一次为false return provider; return AccessController.doPrivileged( new PrivilegedAction<SelectorProvider>() { public SelectorProvider run() { if (loadProviderFromProperty()) return provider; if (loadProviderAsService()) return provider; provider = sun.nio.ch.DefaultSelectorProvider.create(); return provider; } }); } }
//false时 跳入如下方法。
public static ServerSocketChannel open() throws IOException {
return SelectorProvider.provider().openServerSocketChannel();
}
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); //可多选监听操作项
Iterator<SelectionKey> selectionKeyIterator = selector.selectedKeys().iterator();
selectionKeyIterator.next()
selectionKeyIterator.remove();
public static void main(String[] args) throws IOException { //获取channel通道 并设置主机号和端口号 SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8080)); //因为使用非阻塞NIO 所以必须切换为非阻塞 socketChannel.configureBlocking(false); //开辟缓冲区进行存储数据 ByteBuffer byteBuffer = ByteBuffer.allocate(1024); //附加输入: Scanner scanner = new Scanner(System.in); //通过控制台键入数据 while (scanner.hasNext()){ String str = scanner.next(); //准备工作就绪后,准备发送数据给服务端 //打印当前日期转为Byte数据传出 byteBuffer.put((new Date().toString()+":--->"+str).getBytes()); //切换读写模式 byteBuffer.flip(); //写入通道 socketChannel.write(byteBuffer); //完毕时,清除缓冲区内容 byteBuffer.clear(); } }
//完成传输需要取消选择键,防止下次出问题
selectionKeyIterator.remove();
标签:add span adp ima 发送数据 传输 输出 div ace
原文地址:https://www.cnblogs.com/CllOVER/p/13441282.html