<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] # 通道Channel java為Channel接口提供的最主要的實現類如下 * FileChannel: 用于讀取,寫入,映射和操作文件的通道 * SocketChannel: 通過tcp讀寫網絡中的數據 * ServerSocketChannel: 可以監聽新進來的tcp連接,對每一個新進來的連接都會創建一個SocketChannel * DatagramChannel: 通過UDP讀寫網絡中的數據通道 # tcp ~~~ //TCP網絡編程:使用NIO中的Channel + Buffer 實現客戶端給服務器端發送文件,同時,服務器在接收完畢以后,給予反饋 public class BlockingNIOTest1 { @Test public void client() throws Exception{ InetAddress addr = InetAddress.getByName("127.0.0.1"); SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(addr, 9090)); FileChannel fileChannel = FileChannel.open(Paths.get("beauty.jpg"), StandardOpenOption.READ); //使用Channel實現數據的讀寫 ByteBuffer byteBuffer = ByteBuffer.allocate(1024); while(fileChannel.read(byteBuffer) != -1){ byteBuffer.flip(); socketChannel.write(byteBuffer); byteBuffer.clear(); } //關閉數據的輸出 socketChannel.shutdownOutput(); //接收來自于服務器端的數據 socketChannel.read(byteBuffer); byteBuffer.flip(); System.out.println(new String(byteBuffer.array(),0,byteBuffer.limit())); byteBuffer.clear();//可有可無 fileChannel.close(); socketChannel.close(); } @Test public void server() throws Exception{ ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); //綁定端口號 serverSocketChannel.bind(new InetSocketAddress(9090)); SocketChannel socketChannel = serverSocketChannel.accept(); //使用FileChannel和SocketChannel實現數據的讀寫 FileChannel fileChannel = FileChannel.open(Paths.get("beauty4.jpg"), StandardOpenOption.WRITE,StandardOpenOption.CREATE); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); while(socketChannel.read(byteBuffer) != -1){ byteBuffer.flip(); fileChannel.write(byteBuffer); byteBuffer.clear(); } //服務器端在接收完畢數據以后,發送反饋給客戶端 byteBuffer.put("人家已收到了你的情意,么么".getBytes()); byteBuffer.flip(); socketChannel.write(byteBuffer); byteBuffer.clear();//可有可無 fileChannel.close(); socketChannel.close(); serverSocketChannel.close(); } } ~~~ # udp ~~~ //針對UDP,使用NIO實現非阻塞式的網絡通信 public class NonBlockingNIOTest1 { @Test public void send() throws IOException{ DatagramChannel dc = DatagramChannel.open(); dc.configureBlocking(false); ByteBuffer buf = ByteBuffer.allocate(1024); Scanner scan = new Scanner(System.in); while(scan.hasNext()){ String str = scan.nextLine(); buf.put((new Date().toString() + ":\n" + str).getBytes()); buf.flip(); dc.send(buf, new InetSocketAddress("127.0.0.1", 9898)); buf.clear(); } dc.close(); } @Test public void receive() throws IOException{ DatagramChannel dc = DatagramChannel.open(); dc.configureBlocking(false); dc.bind(new InetSocketAddress(9898)); Selector selector = Selector.open(); dc.register(selector, SelectionKey.OP_READ); while(selector.select() > 0){ Iterator<SelectionKey> it = selector.selectedKeys().iterator(); while(it.hasNext()){ SelectionKey sk = it.next(); if(sk.isReadable()){ ByteBuffer buf = ByteBuffer.allocate(1024); dc.receive(buf); buf.flip(); System.out.println(new String(buf.array(), 0, buf.limit())); buf.clear(); } } it.remove(); } dc.close(); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看