⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket.java

📁 kaffe Java 解释器语言,源码,Java的子集系统,开放源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    if (getChannel() != null        && !getChannel().isBlocking ())      throw new IllegalBlockingModeException ();      if (!isBound ())      bind (null);    try      {        getImpl().connect (endpoint, timeout);      }    catch (IOException exception)      {        close ();        throw exception;      }    catch (RuntimeException exception)      {        close ();        throw exception;      }    catch (Error error)      {        close ();        throw error;      }  }  /**   * Returns the address of the remote end of the socket.  If this socket   * is not connected, then <code>null</code> is returned.   *   * @return The remote address this socket is connected to   */  public InetAddress getInetAddress ()  {    if (!isConnected())      return null;    try      {	return getImpl().getInetAddress();      }    catch (SocketException e)      {	// This cannot happen as we are connected.      }    return null;  }  /**   * Returns the local address to which this socket is bound.  If this socket   * is not connected, then <code>null</code> is returned.   *   * @return The local address   *   * @since 1.1   */  public InetAddress getLocalAddress ()  {    InetAddress addr = null;        try      {        addr = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);      }    catch(SocketException e)      {        // (hopefully) shouldn't happen        // throw new java.lang.InternalError        //      ("Error in PlainSocketImpl.getOption");        return null;      }    // FIXME: According to libgcj, checkConnect() is supposed to be called    // before performing this operation.  Problems: 1) We don't have the    // addr until after we do it, so we do a post check.  2). The docs I    // see don't require this in the Socket case, only DatagramSocket, but    // we'll assume they mean both.    SecurityManager sm = System.getSecurityManager();    if (sm != null)      sm.checkConnect(addr.getHostName(), getLocalPort());    return addr;  }  /**   * Returns the port number of the remote end of the socket connection.  If   * this socket is not connected, then -1 is returned.   *   * @return The remote port this socket is connected to   */  public int getPort ()  {    if (!isConnected())      return 0;    try      {	if (getImpl() != null)	  return getImpl().getPort();      }    catch (SocketException e)      {	// This cannot happen as we are connected.      }    return -1;  }  /**   * Returns the local port number to which this socket is bound.  If this   * socket is not connected, then -1 is returned.   *   * @return The local port   */  public int getLocalPort ()  {    if (!isBound())      return -1;        try      {	if (getImpl() != null)	  return getImpl().getLocalPort();      }    catch (SocketException e)      {	// This cannot happen as we are bound.      }    return -1;  }  /**   * If the socket is already bound this returns the local SocketAddress,   * otherwise null   *   * @since 1.4   */  public SocketAddress getLocalSocketAddress()  {    if (!isBound())      return null;        InetAddress addr = getLocalAddress ();    try      {	return new InetSocketAddress (addr, getImpl().getLocalPort());      }    catch (SocketException e)      {	// This cannot happen as we are bound.	return null;      }  }  /**   * If the socket is already connected this returns the remote SocketAddress,   * otherwise null   *   * @since 1.4   */  public SocketAddress getRemoteSocketAddress()  {    if (!isConnected ())      return null;    try      {	return new InetSocketAddress (getImpl().getInetAddress (), getImpl().getPort ());      }    catch (SocketException e)      {	// This cannot happen as we are connected.	return null;      }  }  /**   * Returns an InputStream for reading from this socket.   *   * @return The InputStream object   *   * @exception IOException If an error occurs or Socket is not connected   */  public InputStream getInputStream () throws IOException  {    if (isClosed())      throw new SocketException("socket is closed");        if (!isConnected())      throw new IOException("not connected");	        return getImpl().getInputStream();  }  /**   * Returns an OutputStream for writing to this socket.   *   * @return The OutputStream object   *   * @exception IOException If an error occurs or Socket is not connected   */  public OutputStream getOutputStream () throws IOException  {    if (isClosed())      throw new SocketException("socket is closed");        if (!isConnected())      throw new IOException("not connected");        return getImpl().getOutputStream();  }  /**   * Sets the TCP_NODELAY option on the socket.    *   * @param on true to enable, false to disable   *    * @exception SocketException If an error occurs or Socket is not connected   *   * @since 1.1   */  public void setTcpNoDelay (boolean on)  throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");        getImpl().setOption(SocketOptions.TCP_NODELAY, new Boolean(on));  }  /**   * Tests whether or not the TCP_NODELAY option is set on the socket.    * Returns true if enabled, false if disabled. When on it disables the   * Nagle algorithm which means that packets are always send immediatly and   * never merged together to reduce network trafic.   *   * @return Whether or not TCP_NODELAY is set   *    * @exception SocketException If an error occurs or Socket not connected   *   * @since 1.1   */  public boolean getTcpNoDelay() throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");        Object on = getImpl().getOption(SocketOptions.TCP_NODELAY);      if (on instanceof Boolean)      return(((Boolean)on).booleanValue());    else      throw new SocketException("Internal Error");  }  /**   * Sets the value of the SO_LINGER option on the socket.  If the    * SO_LINGER option is set on a socket and there is still data waiting to   * be sent when the socket is closed, then the close operation will block   * until either that data is delivered or until the timeout period   * expires.  The linger interval is specified in hundreths of a second   * (platform specific?)   *   * @param on true to enable SO_LINGER, false to disable   * @param linger The SO_LINGER timeout in hundreths of a second or -1 if    * SO_LINGER not set.   *   * @exception SocketException If an error occurs or Socket not connected   * @exception IllegalArgumentException If linger is negative   *   * @since 1.1   */  public void setSoLinger(boolean on, int linger) throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");        if (on == true)      {        if (linger < 0)          throw new IllegalArgumentException("SO_LINGER must be >= 0");        if (linger > 65535)          linger = 65535;        getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));      }    else      {        getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(false));      }  }  /**   * Returns the value of the SO_LINGER option on the socket.  If the    * SO_LINGER option is set on a socket and there is still data waiting to   * be sent when the socket is closed, then the close operation will block   * until either that data is delivered or until the timeout period   * expires.  This method either returns the timeouts (in hundredths of   * of a second (platform specific?)) if SO_LINGER is set, or -1 if   * SO_LINGER is not set.   *   * @return The SO_LINGER timeout in hundreths of a second or -1    * if SO_LINGER not set   *   * @exception SocketException If an error occurs or Socket is not connected   *   * @since 1.1   */  public int getSoLinger() throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");        Object linger = getImpl().getOption(SocketOptions.SO_LINGER);    if (linger instanceof Integer)      return(((Integer)linger).intValue());    else      return -1;  }  /**   * Sends urgent data through the socket   *   * @param data The data to send.   * Only the lowest eight bits of data are sent   *   * @exception IOException If an error occurs   *   * @since 1.4   */  public void sendUrgentData (int data) throws IOException  {    if (isClosed())      throw new SocketException("socket is closed");        getImpl().sendUrgentData (data);  }  /**   * Enables/disables the SO_OOBINLINE option   *    * @param on True if SO_OOBLINE should be enabled    *    * @exception SocketException If an error occurs   *    * @since 1.4   */  public void setOOBInline (boolean on) throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");        getImpl().setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));  }  /**   * Returns the current setting of the SO_OOBINLINE option for this socket   *    * @return True if SO_OOBINLINE is set, false otherwise.   *   * @exception SocketException If an error occurs   *    * @since 1.4   */  public boolean getOOBInline () throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");        Object buf = getImpl().getOption(SocketOptions.SO_OOBINLINE);    if (buf instanceof Boolean)      return(((Boolean)buf).booleanValue());    else      throw new SocketException("Internal Error: Unexpected type");  }    /**   * Sets the value of the SO_TIMEOUT option on the socket.  If this value   * is set, and an read/write is performed that does not complete within   * the timeout period, a short count is returned (or an EWOULDBLOCK signal   * would be sent in Unix if no data had been read).  A value of 0 for   * this option implies that there is no timeout (ie, operations will    * block forever).  On systems that have separate read and write timeout   * values, this method returns the read timeout.  This   * value is in milliseconds.   *   * @param timeout The length of the timeout in milliseconds, or    * 0 to indicate no timeout.   *   * @exception SocketException If an error occurs or Socket not connected   *   * @since 1.1   */  public synchronized void setSoTimeout (int timeout) throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");        if (timeout < 0)      throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");          getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));  }  /**   * Returns the value of the SO_TIMEOUT option on the socket.  If this value   * is set, and an read/write is performed that does not complete within   * the timeout period, a short count is returned (or an EWOULDBLOCK signal   * would be sent in Unix if no data had been read).  A value of 0 for   * this option implies that there is no timeout (ie, operations will    * block forever).  On systems that have separate read and write timeout   * values, this method returns the read timeout.  This   * value is in thousandths of a second (implementation specific?).   *   * @return The length of the timeout in thousandth's of a second or 0 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -