socket.read()的阻塞问题

我们都知道,BIO是同步阻塞的IO方式,当BIO服务器调用accept()和read()方法时,都有可能发生阻塞。
下面是一个简易的BIO服务器程序:

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        while (true){
            Socket accept = serverSocket.accept();
            InputStream in = accept.getInputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer))!=-1){
                System.out.println(new String(buffer,0,len));
            }
            System.out.println("接收完毕");
            in.close();
            accept.close();
        }
    }
}

在读取文件时,len = in.read(buffer)当到达文件末尾时就会返回-1,但是在网络编程中,不知道客户端是否发送完毕,那么到底什么时候会返回-1,什么时候会阻塞呢?

  • 首先先开一个这样的客户端
public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",8080);
        OutputStream out = socket.getOutputStream();
        out.write("Hello,this is Client".getBytes());
        out.flush();
        out.close();
    }
}

开启后,发现服务器能输出“接收完毕”,即此时返回了-1,并没有阻塞。

  • 然后我们把out.close();去掉
public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",8080);
        OutputStream out = socket.getOutputStream();
        out.write("Hello,this is Client".getBytes());
        out.flush();
    }
}

开启后,发现服务器依旧能输出“接收完毕”,即此时也返回了-1

  • 我们再测试下面一个客户端,让客户端程序不停止,也不关闭输出流
public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",8080);
        OutputStream out = socket.getOutputStream();
        out.write("Hello,this is Client".getBytes());
        out.flush();
        while (true){
        }
    }
}

这时发现服务器只接收到了数据,并没有输出“接收完毕”,即这时已经阻塞在了read方法上

  • 如果在while (true)之前先关闭out流
public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",8080);
        OutputStream out = socket.getOutputStream();
        out.write("Hello,this is Client".getBytes());
        out.flush();
        out.close();
        while (true){

        }
    }
}

这时服务器又能返回“接收完毕”了,说明此时read()又返回了-1,而没有发生阻塞。

通过以上的实验可以得出结论,socket的read()方法在所有数据读取完毕且客户端的输出流关闭时会返回-1,而如果客户端的输出流如果没有关闭则会阻塞。
而第二种情况之所以也会返回-1是因为客户端程序执行完毕后,自动回收关闭了socket的输出流。

回归实际情况,如果在服务器不限制连接时间的情况下,客户端很有可能一直保持连接状态但不发送数据(不活跃状态),因此这时服务器就会一直阻塞在read方法上,因此如果使用BIO则必须创建多个线程。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值