鱼C论坛

 找回密码
 立即注册
查看: 4071|回复: 1

Java IO网络编程为什么这里没有阻塞

[复制链接]
发表于 2019-9-23 22:08:41 | 显示全部楼层 |阅读模式
30鱼币
本帖最后由 Aojacy 于 2019-9-23 22:58 编辑
  1. package netStream;

  2. import org.junit.Test;

  3. import java.io.*;
  4. import java.net.*;

  5. public class SocketTest2 {
  6.     @Test
  7.     public void clientTest()  {
  8.         Socket socket = null;
  9.         OutputStream os = null;
  10.         try {
  11.             socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
  12.             os = socket.getOutputStream();
  13.             FileInputStream fis = new FileInputStream(new File("hello1.jpg"));
  14.             byte[] buff = new byte[1024];
  15.             int len;
  16.             while ((len = fis.read(buff))!=-1){
  17.                 os.write(buff,0,len);
  18.             }
  19.         } catch (IOException e) {
  20.             e.printStackTrace();
  21.         } finally {
  22.             if(os!=null){
  23.                 try {
  24.                     os.close();
  25.                 } catch (IOException e) {
  26.                     e.printStackTrace();
  27.                 }
  28.             }
  29.             if(socket!=null){
  30.                 try {
  31.                     socket.close();
  32.                 } catch (IOException e) {
  33.                     e.printStackTrace();
  34.                 }
  35.             }
  36.         }
  37.     }
  38.     @Test
  39.     public void serveTest() throws IOException {
  40.         ServerSocket serverSocket = null;
  41.         Socket acceptSocket = null;
  42.         InputStream is = null;
  43.         FileOutputStream fos = null;
  44.         try {
  45.             serverSocket = new ServerSocket(9090);
  46.             acceptSocket = serverSocket.accept();
  47.             is = acceptSocket.getInputStream();
  48.             fos = new FileOutputStream(new File("hello2.jpg"));
  49.             byte[] buff = new byte[1024];
  50.             int len;
  51.             while ((len = is.read(buff))!=-1){
  52.                 fos.write(buff,0,len);
  53.             }
  54.             System.out.println("复制成功");
  55.         } catch (IOException e) {
  56.             e.printStackTrace();
  57.         } finally {
  58.             if(fos!=null){
  59.                 fos.close();
  60.             }
  61.             if(is!=null){
  62.                 is.close();
  63.             }
  64.             if(acceptSocket!=null){
  65.                 acceptSocket.close();
  66.             }
  67.             if(serverSocket!=null){
  68.                 serverSocket.close();
  69.             }
  70.         }
  71.     }
  72. }
复制代码

按理说第57行不会执行到,上面循环会进入阻塞,为什么实际没有阻塞?

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-9-23 22:56:57 | 显示全部楼层
自己想通了,后面紧跟着有个close(),应该和socket.shutdownOutput()作用差不多,可以让服务器接收到文件结束标记
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 18:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表