关于服务端的一些代码。最后的close为什么不用像客户端一样try cash,还有accept.shutdownInput()什么时候用,顺序有要求没


       ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream inputStream = null;
        OutputStream outputStream1 = null;
        try {
            serverSocket = new ServerSocket(8686);
            accept = serverSocket.accept();
            inputStream = accept.getInputStream();
            outputStream1 = accept.getOutputStream();
//        FileOutputStream fileOutputStream = new FileOutputStream("D:\\workspace_idea1\\JavaSenior\\day06\\src\\xwgc.txt");
            byte[] bytes = new byte[1024];
            int ten;
            String s = null;
            while ((ten = inputStream.read(bytes)) != -1){
    //            fileOutputStream.write(bytes,0,ten);
                s = new String(bytes,0,ten).toUpperCase();
            }
            accept.shutdownInput();//这里用在什么位置合适
            outputStream1.write(s.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(outputStream1 != null) {//服务端的close不用try cash
                outputStream1.close();
            }
            if(inputStream != null) {
                inputStream.close();
            }
            if(accept != null) {
                accept.close();
            }
            if(serverSocket != null) {
                serverSocket.close();
            }
        }

服务端不用try catch是不是当前方法throws Exception抛出了异常。

img


处理异常可以用try catch捕获,也可以向外throws抛出当前异常。
调用Socket.shutdownInput( )后, 禁用此套接字的输入流,发送到套接字的输入流端的任何数据都将被确认然后被静默丢弃。
这个方法调用后,不会导致异常,只是接收不到消息了。

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632

jdk7特性直接释放资源,有啥问我