📄 plainsocketimpl.java
字号:
// This is normally done in Socket.connect() but some // subclasses of Socket may call impl.connect() directly! if (socket != null) { socket.setBound(); socket.setConnected(); } } finally { releaseFD(); } } catch (IOException e) { close(); throw e; } } /** * Binds the socket to the specified address of the specified local port. * @param address the address * @param port the port */ protected synchronized void bind(InetAddress address, int lport) throws IOException { socketBind(address, lport); if (socket != null) socket.setBound(); if (serverSocket != null) serverSocket.setBound(); } /** * Listens, for a specified amount of time, for connections. * @param count the amount of time to listen for connections */ protected synchronized void listen(int count) throws IOException { socketListen(count); } /** * Accepts connections. * @param s the connection */ protected synchronized void accept(SocketImpl s) throws IOException { acquireFD(); try { socketAccept(s); } finally { releaseFD(); } } /** * Gets an InputStream for this socket. */ protected synchronized InputStream getInputStream() throws IOException { if (isClosedOrPending()) { throw new IOException("Socket Closed"); } if (shut_rd) { throw new IOException("Socket input is shutdown"); } if (socketInputStream == null) { socketInputStream = new SocketInputStream(this); } return socketInputStream; } void setInputStream(SocketInputStream in) { socketInputStream = in; } /** * Gets an OutputStream for this socket. */ protected synchronized OutputStream getOutputStream() throws IOException { if (isClosedOrPending()) { throw new IOException("Socket Closed"); } if (shut_wr) { throw new IOException("Socket output is shutdown"); } return new SocketOutputStream(this); } /** * Returns the number of bytes that can be read without blocking. */ protected synchronized int available() throws IOException { if (isClosedOrPending()) { throw new IOException("Stream closed."); } /* * If connection has been reset then return 0 to indicate * there are no buffered bytes. */ if (isConnectionReset()) { return 0; } /* * If no bytes available and we were previously notified * of a connection reset then we move to the reset state. * * If are notified of a connection reset then check * again if there are bytes buffered on the socket. */ int n = 0; try { n = socketAvailable(); if (n == 0 && isConnectionResetPending()) { setConnectionReset(); } } catch (ConnectionResetException exc1) { setConnectionResetPending(); try { n = socketAvailable(); if (n == 0) { setConnectionReset(); } } catch (ConnectionResetException exc2) { } } return n; } /** * Closes the socket. */ protected void close() throws IOException { synchronized(fdLock) { if (fd != null) { if (fdUseCount == 0) { closePending = true; /* * We close the FileDescriptor in two-steps - first the * "pre-close" which closes the socket but doesn't * release the underlying file descriptor. This operation * may be lengthy due to untransmitted data and a long * linger interval. Once the pre-close is done we do the * actual socket to release the fd. */ try { socketPreClose(); } finally { socketClose(); } fd = null; return; } else { /* * If a thread has acquired the fd and a close * isn't pending then use a deferred close. * Also decrement fdUseCount to signal the last * thread that releases the fd to close it. */ if (!closePending) { closePending = true; fdUseCount--; socketPreClose(); } } } } } /** * Shutdown read-half of the socket connection; */ protected void shutdownInput() throws IOException { if (fd != null) { socketShutdown(SHUT_RD); if (socketInputStream != null) { socketInputStream.setEOF(true); } shut_rd = true; } } /** * Shutdown write-half of the socket connection; */ protected void shutdownOutput() throws IOException { if (fd != null) { socketShutdown(SHUT_WR); shut_wr = true; } } protected boolean supportsUrgentData () { return true; } protected void sendUrgentData (int data) throws IOException { if (fd == null) { throw new IOException("Socket Closed"); } socketSendUrgentData (data); } /** * Cleans up if the user forgets to close it. */ protected void finalize() throws IOException { close(); } /* * "Acquires" and returns the FileDescriptor for this impl * * A corresponding releaseFD is required to "release" the * FileDescriptor. */ public final FileDescriptor acquireFD() { synchronized (fdLock) { fdUseCount++; return fd; } } /* * "Release" the FileDescriptor for this impl. * * If the use count goes to -1 then the socket is closed. */ public final void releaseFD() { synchronized (fdLock) { fdUseCount--; if (fdUseCount == -1) { if (fd != null) { try { socketClose(); } catch (IOException e) { } finally { fd = null; } } } } } public boolean isConnectionReset() { synchronized (resetLock) { return (resetState == CONNECTION_RESET); } } public boolean isConnectionResetPending() { synchronized (resetLock) { return (resetState == CONNECTION_RESET_PENDING); } } public void setConnectionReset() { synchronized (resetLock) { resetState = CONNECTION_RESET; } } public void setConnectionResetPending() { synchronized (resetLock) { if (resetState == CONNECTION_NOT_RESET) { resetState = CONNECTION_RESET_PENDING; } } } /* * Return true if already closed or close is pending */ public boolean isClosedOrPending() { /* * Lock on fdLock to ensure that we wait if a * close is in progress. */ synchronized (fdLock) { if (closePending || fd == null) { return true; } else { return false; } } } /* * Return the current value of SO_TIMEOUT */ public int getTimeout() { return timeout; } /* * "Pre-close" a socket by dup'ing the file descriptor - this enables * the socket to be closed without releasing the file descriptor. */ private void socketPreClose() throws IOException { socketClose0(true); } /* * Close the socket (and release the file descriptor). */ private void socketClose() throws IOException { socketClose0(false); } private native void socketCreate(boolean isServer) throws IOException; private native void socketConnect(InetAddress address, int port, int timeout) throws IOException; private native void socketBind(InetAddress address, int port) throws IOException; private native void socketListen(int count) throws IOException; private native void socketAccept(SocketImpl s) throws IOException; private native int socketAvailable() throws IOException; private native void socketClose0(boolean useDeferredClose) throws IOException; private native void socketShutdown(int howto) throws IOException; private static native void initProto(); private native void socketSetOption(int cmd, boolean on, Object value) throws SocketException; private native int socketGetOption(int opt, Object iaContainerObj) throws SocketException; private native void socketSendUrgentData(int data) throws IOException; public final static int SHUT_RD = 0; public final static int SHUT_WR = 1;}class InetAddressContainer { InetAddress addr;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -