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("Socket is not bound yet");
        Socket s = new Socket((SocketImpl) null);
        implAccept(s);
        return s;
    }

很明显,逻辑都在 implAccept(s) 里

    protected final void implAccept(Socket s) throws IOException {
        SocketImpl si = null;
        try {
            if (s.impl == null)
              s.setImpl();
            else {
                s.impl.reset();
            }
            si = s.impl;
            s.impl = null;
            si.address = new InetAddress();
            si.fd = new FileDescriptor();
            getImpl().accept(si);
            ...
        } catch (IOException e) {
            ...
        } catch (SecurityException e) {
            ...
        }
        s.impl = si;
        s.postAccept();
    }

很明显,逻辑都在 s.setImpl() 里,而这里的代码在 AndroidStudio 又看不到了

    void setImpl() {
        if (factory != null) {
            impl = factory.createSocketImpl();
            checkOldImpl();
        } else {
            // No need to do a checkOldImpl() here, we know it's an up to date
            // SocketImpl!
            impl = new SocksSocketImpl();
        }
        if (impl != null)
            impl.setSocket(this);
    }

那无参数创建的 Socket,factory 默认为 null,这里就一定会把 impl 赋值为 SocksSocketImpl。SocksSocketImpl 是继承于 PlainSocketImpl 的,PlainSocketImpl 又是继承于 AbstractPlainSocketImpl 的,查看 AbstractPlainSocketImpl 的 getOutputStream 方法

    protected synchronized OutputStream getOutputStream() throws IOException {
        synchronized (fdLock) {
            if (isClosedOrPending())
                throw new IOException("Socket Closed");
            if (shut_wr)
                throw new IOException("Socket output is shutdown");
            if (socketOutputStream == null)
                socketOutputStream = new SocketOutputStream(this);
        }
        return socketOutputStream;
    }

得知 Socket.outputStream 的真实实现是 SocketOutputStream。

它的 write(byte b[], int off, int len) 已经被 SocketOutputStream 重写了

    public void write(byte b[], int off, int len) throws IOException {
        socketWrite(b, off, len);
    }
    private void socketWrite(byte b[], int off, int len) throws IOException {
				...
        FileDescriptor fd = impl.acquireFD();
        try {
            // Android-added: Check BlockGuard policy in socketWrite.
            BlockGuard.getThreadPolicy().onNetwork();
            socketWrite0(fd, b, off, len);
        } catch (SocketException se) {
            if (impl.isClosedOrPending()) {
                throw new SocketException("Socket closed");
            } else {
                throw se;
            }
        } finally {
            impl.releaseFD();
        }
    }
private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
                                     int len) throws IOException;

实际上是交给了 socketWrite0 来处理,这是一个 native 方法。这里的BlockGuard.getThreadPolicy().onNetwork() 不用关注,这是线程策略相关的,不影响流程。

JNIEXPORT void JNICALL
SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
                                              jobject fdObj,
                                              jbyteArray data,
                                              jint off, jint len) {
    char *bufP;
    char BUF[MAX_BUFFER_LEN];
    int buflen;
    int fd;

    if (IS_NULL(fdObj)) {
        JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
        return;
    } else {
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
        /* Bug 4086704 - If the Socket associated with this file descriptor
         * was closed (sysCloseFD), the the file descriptor is set to -1.
         */
        if (fd == -1) {
            JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
            return;
        }

    }

    if (len <= MAX_BUFFER_LEN) {
        bufP = BUF;
        buflen = MAX_BUFFER_LEN;
    } else {
        buflen = min(MAX_HEAP_BUFFER_LEN, len);
        bufP = (char *)malloc((size_t)buflen);

        /* if heap exhausted resort to stack buffer */
        if (bufP == NULL) {
            bufP = BUF;
            buflen = MAX_BUFFER_LEN;
        }
    }

    while(len > 0) {
        int loff = 0;
        int chunkLen = min(buflen, len);
        int llen = chunkLen;
        (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);

        while(llen > 0) {
            int n = NET_Send(fd, bufP + loff, llen, 0);
            if (n > 0) {
                llen -= n;
                loff += n;
                continue;
            }
            if (n == JVM_IO_INTR) {
                JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
            } else {
                if (errno == ECONNRESET) {
                    JNU_ThrowByName(env, "sun/net/ConnectionResetException",
                        "Connection reset");
                } else {
                    NET_ThrowByNameWithLastError(env, "java/net/SocketException",
                        "Write failed");
                }
            }
            if (bufP != BUF) {
                free(bufP);
            }
            return;
        }
        len -= chunkLen;
        off += chunkLen;
    }

    if (bufP != BUF) {
        free(bufP);
    }
}

首先第一个判断 fdObj 一般来说是不会为 null 的,查看 AbstractPlainSocketImpl 实现可佐证这一点。

第二个判断是当 fd = -1 的时候会抛一个错,且有代码注释说明当套接字被关闭时,fd 会被设置为 -1。

直接写一个单元测试,报出来的错误是

Server : processSocket error : java.net.SocketException: Broken pipe

发现报出来的错误信息是 Broken pipe,在 socketWrite0 的实现里根本没有。

进一步在 NET_Send 中寻找,这里是到 JVM 层了,底层调用的 send

__BIONIC_FORTIFY_INLINE
ssize_t send(int socket, const void* const buf __pass_object_size0, size_t len, int flags)
    __overloadable
    __clang_error_if(__bos_unevaluated_lt(__bos0(buf), len),
                     "'send' called with size bigger than buffer") {
  return sendto(socket, buf, len, flags, NULL, 0);
}

这个 send 是 bionic 库的函数,这个库中提供了网络套接字的功能,下载下来这个库,可以在库中找到这个字符串。在 bionic/libc/bionic/strerror.cpp 中可以看到 [EPIPE] = "Broken pipe",对应的错误码被定义为 32。

#define EPIPE           32              /* Broken pipe */

目前不知道这个字符串是怎么传到 java 层的,可以确定的是调用底层网络库的 send 会返回错误码,可依据此来判断发生了什么错误。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实验报告 实验一 Socket编程 一、实验目的 通过socket程序的编写、调试,掌握socket编程的基本方法,了解客户端与服务器端的特点,掌握并熟悉运用socket类与ServerSocket类。 实验内容 学习socket编程使用的类和接口,着重掌握基于TCP协议的socket。 编写服务器端的应用程序 编写客户端的应用程序 实现服务器端与客户端的交互 实验代码 1.服务器端程序 过程:在端口5000上创建一个等待连接的ServerSocket对象server:server=new ServerSocket(5000);接着调用server对象的accept()方法等待某客户程序发出连接请求。该方法一直阻塞直到有客户连接到带端口。一旦有客户发送正确请求,则连接成功,accept()方法返回一个Socket对象,于是得到了一个新的用于通信的Socket对象,通信链路建立成功。然后利用Socket类提供的方法创建Socket对象的输入流和输出流。此后即于客户端进行通信,直到客户端断开连接即关闭各个流结束通信。 代码如下: import java.net.*; import java.io.*; class aa { static public void main (String args[]) throws IOException { ServerSocket server=null; Socket socket=null; InputStream is=null; OutputStream os=null; DataInputStream in=null; PrintStream out=null; try Socket编程实验报告全文共6页,当前为第1页。 { //在端口5000注册服务 Socket编程实验报告全文共6页,当前为第1页。 server=new ServerSocket(5000); socket =server.accept();//侦听连接请求,等待连接 System.out.println("**********************服务器端界面*************************"); System.out.println("与客户端连接成功!"); System.out.println(""); System.out.println("对话内容为:"); System.out.println(""); System.out.println("等待客户发送信息....."); //获取对应的Socket的输入/输出流 is=socket.getInputStream(); os=socket.getOutputStream(); //建立数据流 in= new DataInputStream(is); out =new PrintStream(os);//表示向对方输出 out.println("Welcome!");//表示向对方输出 String str=in.readLine();//逐行读取 do { System.out.println("客户端说:"+ str); str=in.readLine(); }while(str.trim().equals("BYE")) //如果是"BYE"就退出 System.out.println("客户想要离开"); } catch(Exception e) //捕获程序异常 { System.out.println("Error:"+e); } finally { is.close();//关闭输入流 os.close();//关闭输出流 in.close();//关闭数据输入流 socket.close();//关闭socket } } } 2.客户端程序 过程:首先创建一个指向服务器的指定端口号(5000)的Socket对象socketsocket=new Socket("localhost",5000);此时服务器指定为本地计算机,若要在网络中指定服务器,只需要将参数localhost改成相应的服务器名或者IP地址即可。 然后程序利用Socket类提供的方法创建Socket对象的输入流和输出流。此后即于服务器通信,断开连接即关闭各个流结束通信。 Socket编程实验报告全文共6页,当前为第2页。代码如下: Socket编程实验报告全文共6页,当前为第2页。 import java.net.*; import java.io.*; class bb { static public void main (String args[]) throws IOException { Socket socket=null; Input
### 回答1: java.io.outputstream中定义了许多方法,包括write、flush、close等。其中,write方法用于将数据写入输出流中,flush方法用于刷新输出流,将缓冲区中的数据写入到目标设备中,而close方法用于关闭输出流,释放资源。此外,还有一些其他的方法,如write(byte[] b)、write(byte[] b, int off, int len)等,它们都是用于向输出流中写入数据的。总之,java.io.outputstream中定义的方法可以帮助我们实现数据的输出和传输。 ### 回答2: java.io.OutputStream是一个抽象类,它是所有输出流类的超类。它定义了一些基本的输出操作方法,具体如下: 1. write(int b):写入一个字节的数据。以int形式表示字节,只取低8位写入输出流。如果写入成功,则返回写入的字节数;如果发生错误,则抛出IOException异常。 2. write(byte[] b):写入一个字节数组的数据。将字节数组b中的所有字节写入输出流。如果写入成功,则返回写入的字节数;如果发生错误,则抛出IOException异常。 3. write(byte[] b, int off, int len):写入字节数组的一部分数据。从字节数组b的索引off开始,写入长度为len的字节数据到输出流。如果写入成功,则返回写入的字节数;如果发生错误,则抛出IOException异常。 4. flush():刷新输出流。将缓冲区中的数据立即写入到目标设备中。在某些情况下,数据只有在调用flush方法后才会真正写入目标设备。如果发生错误,则抛出IOException异常。 5. close():关闭输出流。先调用flush方法刷新缓冲区,然后释放相关的系统资源。一旦输出流被关闭后,就不能再往其中写入数据。如果发生错误,则抛出IOException异常。 这些方法提供了一些基本的输出操作,可以将数据写入到输出流中,并根据需要进行刷新和关闭操作。根据具体的需求,可以选择合适的方法来进行输出操作。 ### 回答3: 在java.io.outputstream类中,定义了一些常用的方法来处理输出流。以下是其中一些主要方法的简要介绍: 1. write(byte[] b) - 该方法用于将字节数组b中的数据写入输出流。它将字节数组中的数据写入输出流中,可以用于将数据传输到其他地方,比如文件或网络连接。 2. flush() - 该方法用于强制将缓冲区中的数据写入输出流。当输出流中的数据量较小时,系统会将数据暂存在缓冲区中,使用flush()方法可以将缓冲区中的数据立即写入到输出流中,确保数据的及时性。 3. close() - 该方法用于关闭输出流并释放与其相关的资源。在不再需要输出流时,应该调用该方法来关闭流,并且释放资源以便系统能够回收这些资源。注意,一旦关闭输出流,将无法再写入数据。 4. write(int b) - 该方法用于将一个字节写入输出流。它接收一个int类型的参数b,但只会写入低8位的字节数据。如果需要写入多个字节数据,通常使用write(byte[] b)方法。 5. write(byte[] b, int off, int len) - 该方法用于将字节数组b中从off位置开始的len个字节写入输出流。在某些情况下,可能只需要部分数据而不是整个字节数组,这时可以使用该方法来指定写入的起始位置和长度。 这些方法是java.io.outputstream类中常用的一些方法,通过它们可以实现数据的输出和流的操作。了解这些方法可以帮助我们更好地使用输出流来处理数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

幽蓝丶流月

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

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

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

打赏作者

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

抵扣说明:

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

余额充值