记录TCP协议使用Socket连接,客户端请求服务器read()阻塞问题

14 篇文章 0 订阅

如图:客户端与服务端连接不上,服务端处于阻塞状态:
在这里插入图片描述
首先,使用FileInputStream流输入文件,使用FileInputStream.read()方法读取文件返回int型(与题无关,加深记忆,总是忘记强转成int);
其次,关于read()方法的介绍:Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available; 表明read()方式是一个阻塞式方法;
解决方法:使用socket.shutdownOutput();关闭数据的输出;

如图:
在这里插入图片描述

客户端:

@Test
    public void client(){
        InetAddress inet = null;
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;//读入文件
        try {
            //1.创建Socket
//            socket = new Socket(InetAddress.getByName(("127.0.0.1"),9988));
            inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet, 9988);

            //2.输出流
            os = socket.getOutputStream();
            //3.输入流
            fis = new FileInputStream(new File("程潇.jpg"));

            //4.读写过程
            byte[] buffer = new byte[1024];
            int len;
            while((len = fis.read(buffer)) != -1){
                os.write(buffer,0,len);
            }
          
            //关闭数据的输出
            socket.shutdownOutput();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.资源关闭
            if(os != null){

                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(fis != null){

                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(socket != null){

                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

服务端:

 @Test
    public void server(){

        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            ServerSocket ss = new ServerSocket(9988);
            socket = ss.accept();
            is = socket.getInputStream();
            fos = new FileOutputStream("潇潇子.jpg");

            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
            System.out.println("潇潇子服务端已收到来自:"+socket.getInetAddress().getHostName()+"的消息");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(is != null){

                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(socket != null){

                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值