《Netty权威指南》之java NIO的阻塞式网络通信

使用java的NIO写阻塞式网络通信程序,通过TCP连接的方式

v1版本表示客户端向服务端发送请求,服务端接收到文件上传请求后把文件写到服务端的某个目录下并关闭输出流程序结束

v2在v1的基础上,服务端写完文件后回写一句 "写入成功"给客户端,客户端在控制台显示,程序结束

package com.lyzx.netty.day01;

import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @author hero.li
 * 使用NIO写阻塞式的网络通信demo程序
 */
public class BlockingNetWork {

    /**
     * 客户端程序
     * @throws IOException
     */
    @Test
    public void client_v1() throws IOException {
        //1、首先获取网络通道  在获取本地文件的通道
        SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9988));
        FileChannel fChannel = FileChannel.open(Paths.get("F:\\1.pptx"), StandardOpenOption.READ);

        //2、把从本地通道读取的文件信息写到网络通道中
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 128);
        while(fChannel.read(buffer) != -1){
            buffer.flip();
            sChannel.write(buffer);
            buffer.clear();
        }

        //3、关闭通道
        fChannel.close();
        sChannel.close();
    }


    /**
     * 服务端程序
     * @throws IOException
     */
    @Test
    public void server_v1() throws IOException{
        //1、获取服务器端的通道并绑定端口
        ServerSocketChannel ssChannel = ServerSocketChannel.open();
        ssChannel.bind(new InetSocketAddress(9988));

        //2、等待(阻塞)接收用户的请求
        SocketChannel socketChannel = ssChannel.accept();

        //3、当用户请求发送过来时,获取服务器端的文件通道 ,把从用户请求的数据写入到文件通道中
        FileChannel fChannel = FileChannel.open(Paths.get("./current.pptx"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        ByteBuffer buffer = ByteBuffer.allocateDirect(128 * 1024);
        while(socketChannel.read(buffer) != -1){
            buffer.flip();
            fChannel.write(buffer);
            buffer.clear();
        }

        //4、关闭通道
        fChannel.close();
        socketChannel.close();
        ssChannel.close();
    }


    @Test
    public void client_v2() throws IOException{
        //1、首先获取网络通道  在获取本地文件的通道
        SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9988));
        FileChannel fChannel = FileChannel.open(Paths.get("F:\\1.pptx"), StandardOpenOption.READ);

        //2、把从本地通道读取的文件信息写到网络通道中
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 128);
        while(fChannel.read(buffer) != -1){
            buffer.flip();
            sChannel.write(buffer);
            buffer.clear();
        }

        //3、关闭网络通道中的输出流  即告诉服务器文件写完毕了
        sChannel.shutdownOutput();

        int len = 0;
        while((len = sChannel.read(buffer)) != -1){
            buffer.flip();
            byte[] by = new byte[buffer.limit()];
            buffer.get(by);
            System.out.println(new String(by,0,by.length));
            buffer.clear();
        }
        sChannel.shutdownInput();

        //3、关闭通道
        fChannel.close();
        sChannel.close();
    }


    @Test
    public void server_2() throws IOException{
        //1、获取服务器端的通道并绑定端口
        ServerSocketChannel ssChannel = ServerSocketChannel.open();
        ssChannel.bind(new InetSocketAddress(9988));

        //2、等待(阻塞)接收用户的请求
        SocketChannel socketChannel = ssChannel.accept();

        //3、当用户请求发送过来时,获取服务器端的文件通道 ,把从用户请求的数据写入到文件通道中
        FileChannel fChannel = FileChannel.open(Paths.get("./current.pptx"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        ByteBuffer buffer = ByteBuffer.allocateDirect(128 * 1024);
        while(socketChannel.read(buffer) != -1){
            buffer.flip();
            fChannel.write(buffer);
            buffer.clear();
        }
        socketChannel.shutdownInput();

        //4、写完文件后告诉客户端写入成功
        buffer.put("写入成功".getBytes("UTF-8"));
        buffer.flip();
        socketChannel.write(buffer);
        socketChannel.shutdownOutput();

        //5、关闭通道
        fChannel.close();
        socketChannel.close();
        ssChannel.close();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值