Ejemplo n.º 1
0
  /**
   * 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 (impl == null) throw new SocketException("No socket created");

    if (on == true) {
      if (linger < 0) throw new IllegalArgumentException("SO_LINGER must be >= 0");

      if (linger > 65535) linger = 65535;

      impl.setOption(SocketOptions.SO_LINGER, new Integer(linger));
    } else {
      impl.setOption(SocketOptions.SO_LINGER, new Boolean(false));
    }
  }
Ejemplo n.º 2
0
  /**
   * Sets the traffic class value
   *
   * @param tc The traffic class
   * @exception SocketException If an error occurs
   * @exception IllegalArgumentException If tc value is illegal
   * @see Socket:getTrafficClass
   * @since 1.4
   */
  public void setTrafficClass(int tc) throws SocketException {
    if (impl == null) throw new SocketException("Cannot initialize Socket implementation");

    if (tc < 0 || tc > 255) throw new IllegalArgumentException();

    impl.setOption(SocketOptions.IP_TOS, new Integer(tc));
  }
Ejemplo n.º 3
0
  /**
   * 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 (impl == null) throw new SocketException("Not connected");

    Object linger = impl.getOption(SocketOptions.SO_LINGER);
    if (linger instanceof Integer) return (((Integer) linger).intValue());
    else return -1;
  }
Ejemplo n.º 4
0
  /**
   * 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 if not set
   * @exception SocketException If an error occurs or Socket not connected
   * @since 1.1
   */
  public synchronized int getSoTimeout() throws SocketException {
    if (impl == null) throw new SocketException("Not connected");

    Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
    if (timeout instanceof Integer) return (((Integer) timeout).intValue());
    else return 0;
  }
Ejemplo n.º 5
0
  /**
   * Closes the socket.
   *
   * @exception IOException If an error occurs
   */
  public synchronized void close() throws IOException {
    if (impl != null) impl.close();

    if (ch != null) ch.close();

    closed = true;
  }
Ejemplo n.º 6
0
  /**
   * This method sets the value for the system level socket option SO_RCVBUF to the specified value.
   * Note that valid values for this option are specific to a given operating system.
   *
   * @param size The new receive buffer size.
   * @exception SocketException If an error occurs or Socket is not connected
   * @exception IllegalArgumentException If size is 0 or negative
   * @since 1.2
   */
  public void setReceiveBufferSize(int size) throws SocketException {
    if (impl == null) throw new SocketException("Not connected");

    if (size <= 0) throw new IllegalArgumentException("SO_RCVBUF value must be > 0");

    impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
  }
Ejemplo n.º 7
0
  /**
   * If the socket is already bound this returns the local SocketAddress, otherwise null
   *
   * @since 1.4
   */
  public SocketAddress getLocalSocketAddress() {
    InetAddress addr = getLocalAddress();

    if (addr == null) return null;

    return new InetSocketAddress(addr, impl.getLocalPort());
  }
Ejemplo n.º 8
0
  /**
   * 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 thousandths of a second (****????*****)
   *
   * @param timeout The length of the timeout in thousandth's of a second or 0 if not set
   * @exception SocketException If an error occurs or Socket not connected
   * @since 1.1
   */
  public synchronized void setSoTimeout(int timeout) throws SocketException {
    if (impl == null) throw new SocketException("Not connected");

    if (timeout < 0) throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");

    impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
  }
Ejemplo n.º 9
0
  /**
   * Binds the socket to the givent local address/port
   *
   * @param bindpoint The address/port to bind to
   * @exception IOException If an error occurs
   * @exception SecurityException If a security manager exists and its checkConnect method doesn't
   *     allow the operation
   * @exception IllegalArgumentException If the address type is not supported
   * @since 1.4
   */
  public void bind(SocketAddress bindpoint) throws IOException {
    if (closed) throw new SocketException("Socket is closed");

    if (!(bindpoint instanceof InetSocketAddress)) throw new IllegalArgumentException();

    InetSocketAddress tmp = (InetSocketAddress) bindpoint;
    impl.bind(tmp.getAddress(), tmp.getPort());
  }
Ejemplo n.º 10
0
  /**
   * 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 (impl == null) throw new SocketException("Not connected");

    Object on = impl.getOption(SocketOptions.TCP_NODELAY);

    if (on instanceof Boolean) return (((Boolean) on).booleanValue());
    else throw new SocketException("Internal Error");
  }
Ejemplo n.º 11
0
  /**
   * This method returns the value of the system level socket option SO_RCVBUF, which is used by the
   * operating system to tune buffer sizes for data transfers.
   *
   * @return The receive buffer size.
   * @exception SocketException If an error occurs or Socket is not connected
   * @since 1.2
   */
  public int getReceiveBufferSize() throws SocketException {
    if (impl == null) throw new SocketException("Not connected");

    Object buf = impl.getOption(SocketOptions.SO_RCVBUF);

    if (buf instanceof Integer) return (((Integer) buf).intValue());
    else throw new SocketException("Internal Error: Unexpected type");
  }
Ejemplo n.º 12
0
  /**
   * This method returns the value of the socket level socket option SO_KEEPALIVE.
   *
   * @return The setting
   * @exception SocketException If an error occurs or Socket is not connected
   * @since 1.3
   */
  public boolean getKeepAlive() throws SocketException {
    if (impl == null) throw new SocketException("Not connected");

    Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE);

    if (buf instanceof Boolean) return (((Boolean) buf).booleanValue());
    else throw new SocketException("Internal Error: Unexpected type");
  }
Ejemplo n.º 13
0
  /**
   * Returns the current traffic class
   *
   * @exception SocketException If an error occurs
   * @see Socket:setTrafficClass
   * @since 1.4
   */
  public int getTrafficClass() throws SocketException {
    if (impl == null) throw new SocketException("Cannot initialize Socket implementation");

    Object obj = impl.getOption(SocketOptions.IP_TOS);

    if (obj instanceof Integer) return ((Integer) obj).intValue();
    else throw new SocketException("Unexpected type");
  }
Ejemplo n.º 14
0
  /**
   * Checks if the SO_REUSEADDR option is enabled
   *
   * @exception SocketException If an error occurs
   * @since 1.4
   */
  public boolean getReuseAddress() throws SocketException {
    if (impl == null) throw new SocketException("Cannot initialize Socket implementation");

    Object reuseaddr = impl.getOption(SocketOptions.SO_REUSEADDR);

    if (!(reuseaddr instanceof Boolean)) throw new SocketException("Internal Error");

    return ((Boolean) reuseaddr).booleanValue();
  }
Ejemplo n.º 15
0
  /**
   * Connects the socket with a remote address. A timeout of zero is interpreted as an infinite
   * timeout. The connection will then block until established or an error occurs.
   *
   * @param endpoint The address to connect to
   * @exception IOException If an error occurs
   * @exception IllegalArgumentException If the address type is not supported
   * @exception IllegalBlockingModeException If this socket has an associated channel, and the
   *     channel is in non-blocking mode
   * @exception SocketTimeoutException If the timeout is reached
   * @since 1.4
   */
  public void connect(SocketAddress endpoint, int timeout) throws IOException {
    if (closed) throw new SocketException("Socket is closed");

    if (!(endpoint instanceof InetSocketAddress))
      throw new IllegalArgumentException("Address type not supported");

    if (ch != null && !ch.isBlocking()) throw new IllegalBlockingModeException();

    impl.connect(endpoint, timeout);
  }
Ejemplo n.º 16
0
  /**
   * This constructor is where the real work takes place. Connect to the specified address and port.
   * Use default local values if not specified, otherwise use the local host and port passed in.
   * Create as stream or datagram based on "stream" argument.
   *
   * <p>
   *
   * @param raddr The remote address to connect to
   * @param rport The remote port to connect to
   * @param laddr The local address to connect to
   * @param lport The local port to connect to
   * @param stream true for a stream socket, false for a datagram socket
   * @exception IOException If an error occurs
   * @exception SecurityException If a security manager exists and its checkConnect method doesn't
   *     allow the operation
   */
  private Socket(InetAddress raddr, int rport, InetAddress laddr, int lport, boolean stream)
      throws IOException {
    this();
    this.inputShutdown = false;
    this.outputShutdown = false;

    if (impl == null) throw new IOException("Cannot initialize Socket implementation");

    SecurityManager sm = System.getSecurityManager();
    if (sm != null) sm.checkConnect(raddr.getHostName(), rport);

    impl.create(stream);

    // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
    // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
    // that default.  JDK 1.2 doc infers not to do a bind.
    if (laddr != null) impl.bind(laddr, lport);

    if (raddr != null) impl.connect(raddr, rport);
  }
Ejemplo n.º 17
0
  /**
   * 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() {
    if (impl == null) return null;

    InetAddress addr = null;
    try {
      addr = (InetAddress) impl.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;
  }
Ejemplo n.º 18
0
 /**
  * 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 {
   impl.sendUrgentData(data);
 }
Ejemplo n.º 19
0
  /**
   * Enables/Disables the SO_REUSEADDR option
   *
   * @exception SocketException If an error occurs
   * @since 1.4
   */
  public void setReuseAddress(boolean on) throws SocketException {
    if (impl == null) throw new SocketException("Cannot initialize Socket implementation");

    impl.setOption(SocketOptions.SO_REUSEADDR, new Boolean(on));
  }
Ejemplo n.º 20
0
  /**
   * 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 (impl != null) return impl.getInetAddress();

    return null;
  }
Ejemplo n.º 21
0
  /**
   * Closes the output side of the socket stream.
   *
   * @exception IOException If an error occurs.
   * @since 1.3
   */
  public void shutdownOutput() throws IOException {
    if (impl != null) impl.shutdownOutput();

    outputShutdown = true;
  }
Ejemplo n.º 22
0
  /**
   * 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 (impl != null) return impl.getLocalPort();

    return -1;
  }
Ejemplo n.º 23
0
  /**
   * This method sets the value for the socket level socket option SO_KEEPALIVE.
   *
   * @param on True if SO_KEEPALIVE should be enabled
   * @exception SocketException If an error occurs or Socket is not connected
   * @since 1.3
   */
  public void setKeepAlive(boolean on) throws SocketException {
    if (impl == null) throw new SocketException("Not connected");

    impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on));
  }
Ejemplo n.º 24
0
  /**
   * If the socket is already connected this returns the remote SocketAddress, otherwise null
   *
   * @since 1.4
   */
  public SocketAddress getRemoteSocketAddress() {
    if (!isConnected()) return null;

    return new InetSocketAddress(impl.getInetAddress(), impl.getPort());
  }
Ejemplo n.º 25
0
  /**
   * 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 (impl != null) return impl.getOutputStream();

    throw new IOException("Not connected");
  }
Ejemplo n.º 26
0
  /**
   * 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 (impl == null) throw new SocketException("Not connected");

    impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(on));
  }
Ejemplo n.º 27
0
 /**
  * Checks if the socket is connected
  *
  * @since 1.4
  */
 public boolean isConnected() {
   return impl.getInetAddress() != null;
 }
Ejemplo n.º 28
0
  /**
   * 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 (impl == null) throw new SocketException("Not connected");

    impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));
  }