• 【Java nio】 Blocking nio


     1 package com.slp.nio;
     2 
     3 
     4 import org.junit.Test;
     5 
     6 import java.io.File;
     7 import java.io.IOException;
     8 import java.net.InetSocketAddress;
     9 import java.nio.ByteBuffer;
    10 import java.nio.channels.FileChannel;
    11 import java.nio.channels.ServerSocketChannel;
    12 import java.nio.channels.SocketChannel;
    13 import java.nio.file.Paths;
    14 import java.nio.file.StandardOpenOption;
    15 
    16 /**
    17  * Created by sanglp on 2017/3/1.
    18  * 一、使用NIO完成网络通信的三个核心
    19  * 1、通道:负责连接
    20  *     |--java.nio.channels.channel接口
    21  *        |--SelectableChannel
    22  *           |--SocketChannel
    23  *           |--ServerSocketChannel
    24  *           |--DatagramChannel
    25  *
    26  *           |--Pipe.SinkChannel
    27  *           |--Pipe.SourceChannel
    28  * 2、缓冲区:负责数据的存取
    29  * 3、选择器:是SelectableChannel的多路复用器,用于监控SelectableChannel的IO状况
    30  *
    31  */
    32 public class TestBlockingNIO {
    33     //客户端
    34     @org.junit.Test
    35     public void client() throws IOException {
    36       // 1、获取通道
    37        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",9898));
    38         FileChannel inChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.READ);
    39         //2、分配指定大小的缓冲区
    40         ByteBuffer buffer =ByteBuffer.allocate(1024);
    41         //3、读取本地文件 并发送到服务端
    42         while (inChannel.read(buffer)!=-1){
    43             buffer.flip();
    44             socketChannel.write(buffer);
    45             buffer.clear();
    46         }
    47         //4、关闭通道
    48         socketChannel.close();
    49         inChannel.close();
    50     }
    51     //服务端
    52     @Test
    53     public void server() throws IOException {
    54         //1、获取通道
    55         ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    56         FileChannel outChannel = FileChannel.open(Paths.get("3.jpg"),StandardOpenOption.WRITE,StandardOpenOption.CREATE);
    57         //2、绑定连接
    58         serverSocketChannel.bind(new InetSocketAddress(9898));
    59         //3、获取客户端连接
    60         SocketChannel socketChannel = serverSocketChannel.accept();
    61         //4、分配指定大小的缓冲区
    62         ByteBuffer buffer = ByteBuffer.allocate(1024);
    63 
    64         //5、接收客户端的数据,并保存到本地
    65 
    66         while (socketChannel.read(buffer)!=-1){
    67             buffer.flip();
    68             outChannel.write(buffer);
    69             buffer.clear();
    70         }
    71         //关闭通道
    72         socketChannel.close();
    73         serverSocketChannel.close();
    74 
    75     }
    76 }
  • 相关阅读:
    SDNU 1416.一元三次方程求解(数学)
    SDNU 1423.入学考试(01背包)
    SDNU 1427.分解质因数(水题)
    SDNU 1429.区间k大数查询(水题)
    1452.接水问题(思维)
    SDNU 1464.最大最小公倍数(思维)
    SDNU 1467.杨辉三角形(水题)
    SDNU 1469.校门外的树(水题)
    SDNU 1474.特殊回文数(水题)
    OC中String与Data之间的转换
  • 原文地址:https://www.cnblogs.com/dream-to-pku/p/6505532.html
Copyright © 2020-2023  润新知