大ge 帅ge 来看看 TCP 网络编程 内容 socket.shutdownOutput();的使用

问题遇到的现象和发生背景

TCP 网络编程 内容 底下是代码 为什么socket.shutdownOutput();没有这个就一直转圈 我想不过来 有没有大ge能解释一下 不需要给服务器反馈的话 就不需要关闭 老师说 read();是阻塞的 不知道什么时候能出循环 但是出循环的条件不是 != -1 吗(而且之前写的不用给服务器反馈的就不用socket.shutdownOutput();) 实在想不通 大ge 救救

用代码块功能插入代码,请勿粘贴截图
package com.heng.java1;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *  实现TCP的网络编程:
 *
 *      例3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。
 *
 * @author HengOvO
 * @create 2022/12/6-21:22
 */
public class TCPTest3 {

    @Test
    public void client(){

        Socket socket = null;
        OutputStream os = null;
        BufferedInputStream bis = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),4521);

            os = socket.getOutputStream();

            bis = new BufferedInputStream(new FileInputStream("img.jpg"));

            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1){
                os.write(buffer,0,len);
            }

            socket.shutdownOutput();

            // 在关闭流资源之前增加一个操作: 客户端接收服务器端给予的反馈,并显示到控制台上
            is = socket.getInputStream();

            baos = new ByteArrayOutputStream();

            byte[] buffer1 = new byte[1024];
            int len1;
            while ((len1 = is.read(buffer1)) != -1){
                baos.write(buffer1, 0, len1);
            }

            System.out.println(baos.toString());
            System.out.println("客户端明白");

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(baos != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }

            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        }
    }

    @Test
    public void server(){

        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        BufferedOutputStream bos = null;
        OutputStream os = null;
        try {
            ss = new ServerSocket(4521);

            socket = ss.accept();

            is = socket.getInputStream();

            bos = new BufferedOutputStream(new FileOutputStream("imgTCP1.jpg"));

            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
                bos.write(buffer, 0, len);
            }

            System.out.println("文件储存成功,文件地址为" + new File("imgTCP1.jpg").getAbsolutePath());

            // 在关闭流资源之前增加一个操作: 服务器端给予客户端反馈
            os = socket.getOutputStream();
            os.write("图片以收到".getBytes());

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if(ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}