socket.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,458 行 · 第 1/4 页
JAVA
1,458 行
impl.getClass().getDeclaredMethod("connect", cl); return null; } }); } catch (java.security.PrivilegedActionException e) { oldImpl = true; } } static Class implClass = null; /** * Sets impl to the system-default type of SocketImpl. * @since 1.4 */ void setImpl() { checkSocks(); if (factory != null) { impl = factory.createSocketImpl(); checkOldImpl(); } else { if (implClass == null) { try { String prefix = (String) AccessController.doPrivileged( new sun.security.action.GetPropertyAction("impl.prefix.stream", "Plain")); implClass = Class.forName("java.net."+prefix+"SocketImpl"); } catch (Exception e) { implClass = java.net.PlainSocketImpl.class; } } try { impl = (SocketImpl) implClass.newInstance(); } catch (Exception e) { impl = new PlainSocketImpl(); } if (impl != null && !(impl instanceof java.net.PlainSocketImpl)) checkOldImpl(); } if (impl != null) impl.setSocket(this); } /** * Get the <code>SocketImpl</code> attached to this socket, creating * it if necessary. * * @return the <code>SocketImpl</code> attached to that ServerSocket. * @throws SocketException if creation fails * @since 1.4 */ SocketImpl getImpl() throws SocketException { if (!created) createImpl(true); return impl; } /** * Connects this socket to the server. * * @param endpoint the <code>SocketAddress</code> * @throws IOException if an error occurs during the connection * @throws IllegalArgumentException if endpoint is null or is a * SocketAddress subclass not supported by this socket * @since 1.4 * @spec JSR-51 */ public void connect(SocketAddress endpoint) throws IOException { connect(endpoint, 0); } /** * Connects this socket to the server with a specified timeout value. * A timeout of zero is interpreted as an infinite timeout. The connection * will then block until established or an error occurs. * * @param endpoint the <code>SocketAddress</code> * @param timeout the timeout value to be used in milliseconds. * @throws IOException if an error occurs during the connection * @throws SocketTimeoutException if timeout expires before connecting * @throws IllegalArgumentException if endpoint is null or is a * SocketAddress subclass not supported by this socket * @since 1.4 * @spec JSR-51 */ public void connect(SocketAddress endpoint, int timeout) throws IOException { if (endpoint == null) throw new IllegalArgumentException("connect: The address can't be null"); if (timeout < 0) throw new IllegalArgumentException("connect: timeout can't be negative"); if (isClosed()) throw new SocketException("Socket is closed"); if (!oldImpl && isConnected()) throw new SocketException("already connected"); if (!(endpoint instanceof InetSocketAddress)) throw new IllegalArgumentException("Unsupported address type"); InetSocketAddress epoint = (InetSocketAddress) endpoint; SecurityManager security = System.getSecurityManager(); if (security != null) { if (epoint.isUnresolved()) security.checkConnect(epoint.getHostName(), epoint.getPort()); else security.checkConnect(epoint.getAddress().getHostAddress(), epoint.getPort()); } if (!created) createImpl(true); if (!oldImpl) impl.connect(epoint, timeout); else if (timeout == 0) { if (epoint.isUnresolved()) impl.connect(epoint.getAddress().getHostName(), epoint.getPort()); else impl.connect(epoint.getAddress(), epoint.getPort()); } else throw new UnsupportedOperationException("SocketImpl.connect(addr, timeout)"); connected = true; /* * If the socket was not bound before the connect, it is now because * the kernel will have picked an ephemeral port & a local address */ bound = true; } /** * Binds the socket to a local address. * <P> * If the address is <code>null</code>, then the system will pick up * an ephemeral port and a valid local address to bind the socket. * * @param bindpoint the <code>SocketAddress</code> to bind to * @throws IOException if the bind operation fails, or if the socket * is already bound. * @throws IllegalArgumentException if bindpoint is a * SocketAddress subclass not supported by this socket * * @since 1.4 * @see #isBound */ public void bind(SocketAddress bindpoint) throws IOException { if (isClosed()) throw new SocketException("Socket is closed"); if (!oldImpl && isBound()) throw new SocketException("Already bound"); if (bindpoint != null && (!(bindpoint instanceof InetSocketAddress))) throw new IllegalArgumentException("Unsupported address type"); InetSocketAddress epoint = (InetSocketAddress) bindpoint; if (epoint != null && epoint.isUnresolved()) throw new SocketException("Unresolved address"); if (bindpoint == null) getImpl().bind(InetAddress.anyLocalAddress(), 0); else getImpl().bind(epoint.getAddress(), epoint.getPort()); bound = true; } /** * set the flags after an accept() call. */ final void postAccept() { connected = true; created = true; bound = true; } void setCreated() { created = true; } void setBound() { bound = true; } void setConnected() { connected = true; } /** * Returns the address to which the socket is connected. * * @return the remote IP address to which this socket is connected, * or <code>null</code> if the socket is not connected. */ public InetAddress getInetAddress() { if (!isConnected()) return null; try { return getImpl().getInetAddress(); } catch (SocketException e) { } return null; } /** * Gets the local address to which the socket is bound. * * @return the local address to which the socket is bound or * <code>InetAddress.anyLocalAddress()</code> * if the socket is not bound yet. * @since JDK1.1 */ public InetAddress getLocalAddress() { // This is for backward compatibility if (!isBound()) return InetAddress.anyLocalAddress(); InetAddress in = null; try { in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR); if (in.isAnyLocalAddress()) { in = InetAddress.anyLocalAddress(); } } catch (Exception e) { in = InetAddress.anyLocalAddress(); // "0.0.0.0" } return in; } /** * Returns the remote port to which this socket is connected. * * @return the remote port number to which this socket is connected, or * 0 if the socket is not connected yet. */ public int getPort() { if (!isConnected()) return 0; try { return getImpl().getPort(); } catch (SocketException e) { // Shouldn't happen as we're connected } return -1; } /** * Returns the local port to which this socket is bound. * * @return the local port number to which this socket is bound or -1 * if the socket is not bound yet. */ public int getLocalPort() { if (!isBound()) return -1; try { return getImpl().getLocalPort(); } catch(SocketException e) { // shouldn't happen as we're bound } return -1; } /** * Returns the address of the endpoint this socket is connected to, or * <code>null</code> if it is unconnected. * @return a <code>SocketAddress</code> reprensenting the remote endpoint of this * socket, or <code>null</code> if it is not connected yet. * @see #getInetAddress() * @see #getPort() * @see #connect(SocketAddress, int) * @see #connect(SocketAddress) * @since 1.4 */ public SocketAddress getRemoteSocketAddress() { if (!isConnected()) return null; return new InetSocketAddress(getInetAddress(), getPort()); } /** * Returns the address of the endpoint this socket is bound to, or * <code>null</code> if it is not bound yet. * * @return a <code>SocketAddress</code> representing the local endpoint of this * socket, or <code>null</code> if it is not bound yet. * @see #getLocalAddress() * @see #getLocalPort() * @see #bind(SocketAddress) * @since 1.4 */ public SocketAddress getLocalSocketAddress() { if (!isBound()) return null; return new InetSocketAddress(getLocalAddress(), getLocalPort()); } /** * Returns an input stream for this socket. * * <p> If this socket has an associated channel then the resulting input * stream delegates all of its operations to the channel. * * <p>Under abnormal conditions the underlying connection may be * broken by the remote host or the network software (for example * a connection reset in the case of TCP connections). When a * broken connection is detected by the network software the * following applies to the returned input stream :- * * <ul> * * <li><p>The network software may discard bytes that are buffered * by the socket. Bytes that aren't discarded by the network * software can be read using {@link java.io.InputStream#read read}. * * <li><p>If there are no bytes buffered on the socket, or all * buffered bytes have been consumed by * {@link java.io.InputStream#read read}, then all subsequent * calls to {@link java.io.InputStream#read read} will throw an * {@link java.io.IOException IOException}. * * <li><p>If there are no bytes buffered on the socket, and the * socket has not been closed using {@link #close close}, then * {@link java.io.InputStream#available available} will * return <code>0</code>. * * </ul> * * @return an input stream for reading bytes from this socket. * @exception IOException if an I/O error occurs when creating the * input stream, the socket is closed, the socket is * not connected, or the socket input has been shutdown * using {@link #shutdownInput()} * * @revised 1.4 * @spec JSR-51 */ public InputStream getInputStream() throws IOException { if (isClosed()) throw new SocketException("Socket is closed"); if (!isConnected()) throw new SocketException("Socket is not connected"); if (isInputShutdown()) throw new SocketException("Socket input is shutdown"); final Socket s = this; InputStream is = null; try { is = (InputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws IOException { return impl.getInputStream(); } }); } catch (java.security.PrivilegedActionException e) { throw (IOException) e.getException(); } return is; } /** * Returns an output stream for this socket. * * <p> If this socket has an associated channel then the resulting output * stream delegates all of its operations to the channel. * * @return an output stream for writing bytes to this socket.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?