Socket.outputStream 是如何感知到链接断开而抛出 IOException 的

首先查看 Socket.getOutputStream 方法可以知道此 outputStream 是由 impl 获取的

    public OutputStream getOutputStream() throws IOException {
   
   
        ...
        OutputStream os = null;
        try {
   
   
            os = AccessController.doPrivileged(
                new PrivilegedExceptionAction<OutputStream>() {
   
   
                    public OutputStream run() throws IOException {
   
   
                        return impl.getOutputStream();
                    }
                });
        } catch (java.security.PrivilegedActionException e) {
   
   
            throw (IOException) e.getException();
        }
        return os;
    }

impl 的实现就得看 ServerSocket 是拿到的什么样的 Socket 了,看 Socket.accept() 方法

    public Socket accept() throws IOException {
   
   
        if (isClosed())
            throw new SocketException("Socket is closed");
        if (!isBound())
            throw new SocketException
``` package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets; /** * 服务端 */ public class Server { /** * java.net.ServerSocket * 运行在服务端,用于开启服务端口,并等待客户端的链接。 * 如果我们将Socket比喻为"插座",那么ServerSocket相当于"总机" */ private ServerSocket serverSocket; public Server(){ try { System.out.println("正在启动服务端..."); /* 创建ServerSocket时要指定服务端口,该端口不能与其他程序一致,否则 会抛出异常:java.net.BindException:address already in use 此时只能更换其他端口或者将占用该端口的程序关闭 */ serverSocket = new ServerSocket(8088); System.out.println("服务端启动完毕!"); } catch (IOException e) { e.printStackTrace(); } } public void start(){ try { System.out.println("等待客户端链接..."); Socket socket = serverSocket.accept(); System.out.println("一个客户端链接了!"); /* Socket提供的方法: InputStream getInputStream() 通过Socket获取的输入流可以读取远端计算机发送过来的字节 */ InputStream in = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr); String message = br.readLine(); System.out.println("客户端说:"+message); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Server server = new Server(); server.start(); } } package socket; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.nio.charset.StandardCharsets; /** * 客户端 */ public class Client { /** * java.net.Socket * 使用Socket可以与远端计算机建立TCP链接,并通过两条流的读写完成与对方的数据交换 */ private Socket socket; /** * 构造方法,用于客户端初始化工作 */ public Client(){ try { System.out.println("正在链接服务端..."); /* 实例化Socket时需要传入两个参数 1:服务端的IP地址 2:服务端程序开启的端口 localhost:代表本机IP 还可以使用127.0.0.1,也是表示本机IP。如:socket = new Socket("127.0.0.1",8088); */ socket = new Socket("localhost",8088); System.out.println("与服务端链接成功!"); } catch (IOException e) { e.printStackTrace(); } } /** * 客户端开始工作的方法 */ public void start(){ try { /* Socket提供的方法: OutputStream getOutputStream() 该方法获取的输出流写出的字节会发送给远端计算机 */ //低级流:作用是将写出的字节发送给对方 OutputStream out = socket.getOutputStream(); //高级流:负责将写出的字符按照指定的字符集转换为字节 OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8); //高级流:负责按行写出字符串(第二个参数为true,用于println一次就实际写出一次) PrintWriter pw = new PrintWriter(osw,true); pw.println("你好服务端!"); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Client client = new Client(); client.start(); } }```加入功能,让客户端可以一直向服务端发送信息
最新发布
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幽蓝丶流月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值