Java Code Examples for java.net.SocketOptions

The following are top voted examples for showing how to use java.net.SocketOptions. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: silvertunnel-ng   File: LocalSocketImpl.java   Source Code and License 6 votes vote down vote up
public Object getOption(int optID) throws IOException
{
    if (fd == null) {
        throw new IOException("socket not created");
    }

    if (optID == SocketOptions.SO_TIMEOUT) {
        return 0;
    }

    int value = getOption_native(fd, optID);
    switch (optID)
    {
        case SocketOptions.SO_RCVBUF:
        case SocketOptions.SO_SNDBUF:
            return value;
        case SocketOptions.SO_REUSEADDR:
        default:
            return value;
    }
}
 
Example 2
Project: evosuite   File: EvoSuiteSocket.java   Source Code and License 6 votes vote down vote up
@Override
protected void bind(InetAddress host, int port) throws IOException {

       if(port == 0) {
           port = VirtualNetwork.getInstance().getNewLocalEphemeralPort();
       }

	//TODO: need to check special cases like multicast
	boolean opened = VirtualNetwork.getInstance().openTcpServer(host.getHostAddress(), port);
	if(!opened) {
		throw new IOException("Failed to open TCP port");
	}
	super.localport = port;
	setOption(SocketOptions.SO_BINDADDR, host);
       isBound = true;
}
 
Example 3
Project: In-the-Box-Fork   File: BlockGuard.java   Source Code and License 6 votes vote down vote up
private boolean isLingerSocket(FileDescriptor fd) throws SocketException {
    try {
        Object lingerValue = mNetwork.getSocketOption(fd, SocketOptions.SO_LINGER);
        if (lingerValue instanceof Boolean) {
            return (Boolean) lingerValue;
        } else if (lingerValue instanceof Integer) {
            return ((Integer) lingerValue) != 0;
        }
        throw new AssertionError(lingerValue.getClass().getName());
    } catch (Exception ignored) {
        // We're called via Socket.close (which doesn't ask for us to be called), so we
        // must not throw here, because Socket.close must not throw if asked to close an
        // already-closed socket.
        return false;
    }
}
 
Example 4
Project: cInterphone   File: PlainDatagramSocketImpl.java   Source Code and License 6 votes vote down vote up
@Override
public Object getOption(int optID) throws SocketException {
    if (optID == SocketOptions.SO_TIMEOUT) {
        return Integer.valueOf(receiveTimeout);
    } else if (optID == SocketOptions.IP_TOS) {
        return Integer.valueOf(trafficClass);
    } else {
        // Call the native first so there will be
        // an exception if the socket if closed.
        Object result = netImpl.getSocketOption(fd, optID);
        if (optID == SocketOptions.IP_MULTICAST_IF
                && (netImpl.getSocketFlags() & MULTICAST_IF) != 0) {
            try {
                return InetAddress.getByAddress(ipaddress);
            } catch (UnknownHostException e) {
                return null;
            }
        }
        return result;
    }
}
 
Example 5
Project: cn1   File: PlainDatagramSocketImpl.java   Source Code and License 6 votes vote down vote up
public Object getOption(int optID) throws SocketException {
    if (optID == SocketOptions.SO_TIMEOUT) {
        return Integer.valueOf(receiveTimeout);
    } else if (optID == SocketOptions.IP_TOS) {
        return Integer.valueOf(trafficClass);
    } else {
        // Call the native first so there will be
        // an exception if the socket if closed.
        Object result = netImpl.getSocketOption(fd, optID);
        if (optID == SocketOptions.IP_MULTICAST_IF
                && (netImpl.getSocketFlags() & MULTICAST_IF) != 0) {
            try {
                return InetAddress.getByAddress(ipaddress);
            } catch (UnknownHostException e) {
                return null;
            }
        }
        return result;
    }
}
 
Example 6
Project: cn1   File: PlainSocketImpl.java   Source Code and License 6 votes vote down vote up
@Override
public Object getOption(int optID) throws SocketException {
    if (optID == SocketOptions.SO_TIMEOUT) {
        return Integer.valueOf(receiveTimeout);
    } else if (optID == SocketOptions.IP_TOS) {
        return Integer.valueOf(trafficClass);
    } else {
        // Call the native first so there will be
        // an exception if the socket if closed.
        Object result = netImpl.getSocketOption(fd, optID);
        if (optID == SocketOptions.TCP_NODELAY
                && (netImpl.getSocketFlags() & TCP_NODELAY) != 0) {
            return Boolean.valueOf(tcpNoDelay);
        }
        return result;
    }
}
 
Example 7
Project: freeVM   File: PlainDatagramSocketImpl.java   Source Code and License 6 votes vote down vote up
@Override
public Object getOption(int optID) throws SocketException {
    if (optID == SocketOptions.SO_TIMEOUT) {
        return Integer.valueOf(receiveTimeout);
    } else if (optID == SocketOptions.IP_TOS) {
        return Integer.valueOf(trafficClass);
    } else {
        // Call the native first so there will be
        // an exception if the socket if closed.
        Object result = netImpl.getSocketOption(fd, optID);
        if (optID == SocketOptions.IP_MULTICAST_IF
                && (netImpl.getSocketFlags() & MULTICAST_IF) != 0) {
            try {
                return InetAddress.getByAddress(ipaddress);
            } catch (UnknownHostException e) {
                return null;
            }
        }
        return result;
    }
}
 
Example 8
Project: freeVM   File: PlainSocketImpl.java   Source Code and License 6 votes vote down vote up
@Override
public Object getOption(int optID) throws SocketException {
    if (optID == SocketOptions.SO_TIMEOUT) {
        return Integer.valueOf(receiveTimeout);
    } else if (optID == SocketOptions.IP_TOS) {
        return Integer.valueOf(trafficClass);
    } else {
        // Call the native first so there will be
        // an exception if the socket if closed.
        Object result = netImpl.getSocketOption(fd, optID);
        if (optID == SocketOptions.TCP_NODELAY
                && (netImpl.getSocketFlags() & TCP_NODELAY) != 0) {
            return Boolean.valueOf(tcpNoDelay);
        }
        return result;
    }
}
 
Example 9
Project: freeVM   File: PlainDatagramSocketImpl.java   Source Code and License 6 votes vote down vote up
public Object getOption(int optID) throws SocketException {
    if (optID == SocketOptions.SO_TIMEOUT) {
        return Integer.valueOf(receiveTimeout);
    } else if (optID == SocketOptions.IP_TOS) {
        return Integer.valueOf(trafficClass);
    } else {
        // Call the native first so there will be
        // an exception if the socket if closed.
        Object result = netImpl.getSocketOption(fd, optID);
        if (optID == SocketOptions.IP_MULTICAST_IF
                && (netImpl.getSocketFlags() & MULTICAST_IF) != 0) {
            try {
                return InetAddress.getByAddress(ipaddress);
            } catch (UnknownHostException e) {
                return null;
            }
        }
        return result;
    }
}
 
Example 10
Project: freeVM   File: PlainSocketImpl.java   Source Code and License 6 votes vote down vote up
@Override
public Object getOption(int optID) throws SocketException {
    if (optID == SocketOptions.SO_TIMEOUT) {
        return Integer.valueOf(receiveTimeout);
    } else if (optID == SocketOptions.IP_TOS) {
        return Integer.valueOf(trafficClass);
    } else {
        // Call the native first so there will be
        // an exception if the socket if closed.
        Object result = netImpl.getSocketOption(fd, optID);
        if (optID == SocketOptions.TCP_NODELAY
                && (netImpl.getSocketFlags() & TCP_NODELAY) != 0) {
            return Boolean.valueOf(tcpNoDelay);
        }
        return result;
    }
}
 
Example 11
Project: phonty   File: PlainDatagramSocketImpl.java   Source Code and License 6 votes vote down vote up
@Override
public Object getOption(int optID) throws SocketException {
    if (optID == SocketOptions.SO_TIMEOUT) {
        return Integer.valueOf(receiveTimeout);
    } else if (optID == SocketOptions.IP_TOS) {
        return Integer.valueOf(trafficClass);
    } else {
        // Call the native first so there will be
        // an exception if the socket if closed.
        Object result = netImpl.getSocketOption(fd, optID);
        if (optID == SocketOptions.IP_MULTICAST_IF
                && (netImpl.getSocketFlags() & MULTICAST_IF) != 0) {
            try {
                return InetAddress.getByAddress(ipaddress);
            } catch (UnknownHostException e) {
                return null;
            }
        }
        return result;
    }
}
 
Example 12
Project: netlib   File: NetSocket2SocketImpl.java   Source Code and License 6 votes vote down vote up
public Object getOption(int key) throws SocketException {
    if (key==SocketOptions.SO_TIMEOUT) {
        // get timeout
        return inputStreamTimeout;
    }
    if (key==SocketOptions.TCP_NODELAY) {
        return tcpNodelay;
    }
    if (key==SocketOptions.SO_LINGER) {
        // get timeout
        return soLinger;
    }
    
    // unsupported option
    String msg = "no implementation for getOption("+key+"). List of all options in java.net.SocketOptions.";
    if (log.isLoggable(Level.FINER)) {
        log.log(Level.FINER, msg, new Throwable("method not completely implemented"));
    } else {
        log.info(msg+" - Log with level=FINER to get call hierarchy.");
    }
    return null;
}
 
Example 13
Project: java-coap   File: QoSDatagramSocket.java   Source Code and License 5 votes vote down vote up
/**
 * Non-synchronized on purpose (TODO refactor away the need to do this)
 */
@SuppressWarnings("sync-override")
@Override
public void setTrafficClass(int tc) throws SocketException {
    if (tc < 0 || tc > 255) {
        throw new IllegalArgumentException("tc is not in range 0 -- 255");
    }
    if (isClosed()) {
        throw new SocketException("Socket is closed");
    }
    impl.setOption(SocketOptions.IP_TOS, tc);

}
 
Example 14
Project: javify   File: VMPlainSocketImpl.java   Source Code and License 5 votes vote down vote up
public void setOption(int optionId, Object optionValue)
  throws SocketException
{
  int value;
  if (optionValue instanceof Integer)
    value = ((Integer) optionValue).intValue();
  else if (optionValue instanceof Boolean)
    // Switching off the linger behavior is done by setting
    // the value to -1. This is how the Java API interprets
    // it.
    value = ((Boolean) optionValue).booleanValue()
            ? 1
            : (optionId == SocketOptions.SO_LINGER)
            ? -1
            : 0;
  else
    throw new IllegalArgumentException("option value type "
                                       + optionValue.getClass().getName());

  try
    {
      setOption(nfd.getNativeFD(), optionId, value);
    }
  catch (IOException ioe)
    {
      SocketException se = new SocketException();
      se.initCause(ioe);
      throw se;
    }
}
 
Example 15
Project: javify   File: VMPlainSocketImpl.java   Source Code and License 5 votes vote down vote up
/**
 * Get a socket option. This implementation is only required to support
 * socket options that are boolean values, which include:
 *
 *  SocketOptions.IP_MULTICAST_LOOP
 *  SocketOptions.SO_BROADCAST
 *  SocketOptions.SO_KEEPALIVE
 *  SocketOptions.SO_OOBINLINE
 *  SocketOptions.SO_REUSEADDR
 *  SocketOptions.TCP_NODELAY
 *
 * and socket options that are integer values, which include:
 *
 *  SocketOptions.IP_TOS
 *  SocketOptions.SO_LINGER
 *  SocketOptions.SO_RCVBUF
 *  SocketOptions.SO_SNDBUF
 *  SocketOptions.SO_TIMEOUT
 *
 * @param optionId The option ID to fetch.
 * @return A {@link Boolean} or {@link Integer} containing the socket
 *  option.
 * @throws SocketException
 */
public Object getOption(int optionId) throws SocketException
{
  int value;
  try
    {
      value = getOption(nfd.getNativeFD(), optionId);
    }
  catch (IOException ioe)
    {
      SocketException se = new SocketException();
      se.initCause(ioe);
      throw se;
    }

  switch (optionId)
    {
      case SocketOptions.IP_MULTICAST_LOOP:
      case SocketOptions.SO_BROADCAST:
      case SocketOptions.SO_KEEPALIVE:
      case SocketOptions.SO_OOBINLINE:
      case SocketOptions.SO_REUSEADDR:
      case SocketOptions.TCP_NODELAY:
        return Boolean.valueOf(value != 0);

      case SocketOptions.IP_TOS:
      case SocketOptions.SO_LINGER:
      case SocketOptions.SO_RCVBUF:
      case SocketOptions.SO_SNDBUF:
      case SocketOptions.SO_TIMEOUT:
        return new Integer(value);

      default:
        throw new SocketException("getting option " + optionId +
                                  " not supported here");
    }
}
 
Example 16
Project: jvm-stm   File: VMPlainSocketImpl.java   Source Code and License 5 votes vote down vote up
public void setOption(int optionId, Object optionValue)
  throws SocketException
{
  int value;
  if (optionValue instanceof Integer)
    value = ((Integer) optionValue).intValue();
  else if (optionValue instanceof Boolean)
    // Switching off the linger behavior is done by setting
    // the value to -1. This is how the Java API interprets
    // it.
    value = ((Boolean) optionValue).booleanValue()
            ? 1
            : (optionId == SocketOptions.SO_LINGER)
            ? -1
            : 0;
  else
    throw new IllegalArgumentException("option value type "
                                       + optionValue.getClass().getName());
  
  try
    {
      setOption(nfd.getNativeFD(), optionId, value);
    }
  catch (IOException ioe)
    {
      SocketException se = new SocketException();
      se.initCause(ioe);
      throw se;
    }
}
 
Example 17
Project: jvm-stm   File: VMPlainSocketImpl.java   Source Code and License 5 votes vote down vote up
/**
 * Get a socket option. This implementation is only required to support
 * socket options that are boolean values, which include:
 * 
 *  SocketOptions.IP_MULTICAST_LOOP
 *  SocketOptions.SO_BROADCAST
 *  SocketOptions.SO_KEEPALIVE
 *  SocketOptions.SO_OOBINLINE
 *  SocketOptions.SO_REUSEADDR
 *  SocketOptions.TCP_NODELAY
 * 
 * and socket options that are integer values, which include:
 * 
 *  SocketOptions.IP_TOS
 *  SocketOptions.SO_LINGER
 *  SocketOptions.SO_RCVBUF
 *  SocketOptions.SO_SNDBUF
 *  SocketOptions.SO_TIMEOUT
 *
 * @param optionId The option ID to fetch.
 * @return A {@link Boolean} or {@link Integer} containing the socket
 *  option.
 * @throws SocketException
 */
public Object getOption(int optionId) throws SocketException
{
  int value;
  try
    {
      value = getOption(nfd.getNativeFD(), optionId);
    }
  catch (IOException ioe)
    {
      SocketException se = new SocketException();
      se.initCause(ioe);
      throw se;
    }
  
  switch (optionId)
    {
      case SocketOptions.IP_MULTICAST_LOOP:
      case SocketOptions.SO_BROADCAST:
      case SocketOptions.SO_KEEPALIVE:
      case SocketOptions.SO_OOBINLINE:
      case SocketOptions.SO_REUSEADDR:
      case SocketOptions.TCP_NODELAY:
        return Boolean.valueOf(value != 0);
        
      case SocketOptions.IP_TOS:
      case SocketOptions.SO_LINGER:
      case SocketOptions.SO_RCVBUF:
      case SocketOptions.SO_SNDBUF:
      case SocketOptions.SO_TIMEOUT:
        return new Integer(value);
        
      default:
        throw new SocketException("getting option " + optionId +
                                  " not supported here");
    }
}
 
Example 18
Project: j2objc   File: PlainServerSocketImpl.java   Source Code and License 5 votes vote down vote up
@Override
protected void create(boolean isStreaming) throws IOException {
    super.create(isStreaming);
    if (isStreaming) {
        setOption(SocketOptions.SO_REUSEADDR, Boolean.TRUE);
    }
}
 
Example 19
Project: silvertunnel-monteux   File: NetSocket2SocketImpl.java   Source Code and License 5 votes vote down vote up
@Override
public Object getOption(final int key) throws SocketException
{
	if (key == SocketOptions.SO_TIMEOUT)
	{
		// get timeout
		return inputStreamTimeout;
	}
	if (key == SocketOptions.TCP_NODELAY)
	{
		return tcpNodelay;
	}
	if (key == SocketOptions.SO_LINGER)
	{
		// get timeout
		return soLinger;
	}

	// unsupported option
	final String msg = "no implementation for getOption(" + key
			+ "). List of all options in java.net.SocketOptions.";
	if (logger.isDebugEnabled())
	{
		logger.debug(msg, new Throwable("method not completely implemented"));
	}
	else
	{
		logger.info(msg + " - Log with level=debug to get call hierarchy.");
	}
	return null;
}
 
Example 20
Project: evosuite   File: MockSocket.java   Source Code and License 5 votes vote down vote up
@Override
public void setSoLinger(boolean on, int linger) throws SocketException {
	if (isClosed())
		throw new SocketException("Socket is closed");
	if (!on) {
		getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(on));
	} else {
		if (linger < 0) {
			throw new IllegalArgumentException("invalid value for SO_LINGER");
		}
		if (linger > 65535)
			linger = 65535;
		getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));
	}
}
 
Example 21
Project: evosuite   File: MockSocket.java   Source Code and License 5 votes vote down vote up
@Override
public int getSoLinger() throws SocketException {
	if (isClosed())
		throw new SocketException("Socket is closed");
	Object o = getImpl().getOption(SocketOptions.SO_LINGER);
	if (o instanceof Integer) {
		return ((Integer) o).intValue();
	} else {
		return -1;
	}
}
 
Example 22
Project: evosuite   File: MockSocket.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized void setSoTimeout(int timeout) throws SocketException {
	if (isClosed())
		throw new SocketException("Socket is closed");
	if (timeout < 0)
		throw new IllegalArgumentException("timeout can't be negative");

	getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
}
 
Example 23
Project: evosuite   File: MockSocket.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized int getSoTimeout() throws SocketException {
	if (isClosed())
		throw new SocketException("Socket is closed");
	Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
	if (o instanceof Integer) {
		return ((Integer) o).intValue();
	} else {
		return 0;
	}
}
 
Example 24
Project: evosuite   File: MockSocket.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized void setSendBufferSize(int size) throws SocketException{
	if (!(size > 0)) {
		throw new IllegalArgumentException("negative send size");
	}
	if (isClosed())
		throw new SocketException("Socket is closed");
	getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));
}
 
Example 25
Project: evosuite   File: MockSocket.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized int getSendBufferSize() throws SocketException {
	if (isClosed())
		throw new SocketException("Socket is closed");
	int result = 0;
	Object o = getImpl().getOption(SocketOptions.SO_SNDBUF);
	if (o instanceof Integer) {
		result = ((Integer)o).intValue();
	}
	return result;
}
 
Example 26
Project: evosuite   File: MockSocket.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized void setReceiveBufferSize(int size) throws SocketException{
	if (size <= 0) {
		throw new IllegalArgumentException("invalid receive size");
	}
	if (isClosed())
		throw new SocketException("Socket is closed");
	getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
}
 
Example 27
Project: evosuite   File: MockSocket.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized int getReceiveBufferSize() throws SocketException{
	if (isClosed())
		throw new SocketException("Socket is closed");
	int result = 0;
	Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
	if (o instanceof Integer) {
		result = ((Integer)o).intValue();
	}
	return result;
}
 
Example 28
Project: evosuite   File: MockSocket.java   Source Code and License 5 votes vote down vote up
@Override
public void setTrafficClass(int tc) throws SocketException {
	if (tc < 0 || tc > 255)
		throw new IllegalArgumentException("tc is not in range 0 -- 255");

	if (isClosed())
		throw new SocketException("Socket is closed");
	getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc));
}
 
Example 29
Project: evosuite   File: EvoSuiteSocket.java   Source Code and License 5 votes vote down vote up
private void initOptions() {
	// see AbstractPlainSocketImpl
	options.put(SocketOptions.SO_TIMEOUT, 0);
	options.put(SocketOptions.SO_RCVBUF, 131072);
	options.put(SocketOptions.SO_SNDBUF, 131072);
	options.put(SocketOptions.SO_LINGER, -1);
	options.put(SocketOptions.SO_KEEPALIVE, false);
	options.put(SocketOptions.SO_OOBINLINE, false);
	options.put(SocketOptions.SO_REUSEADDR, false);
	options.put(SocketOptions.TCP_NODELAY, false);
       options.put(SocketOptions.IP_TOS, 0);
}
 
Example 30
Project: evosuite   File: EvoSuiteSocket.java   Source Code and License 5 votes vote down vote up
@Override
protected void accept(SocketImpl s) throws IOException {

	if(!(s instanceof EvoSuiteSocket)) {
		throw new IOException("Can only handle mocked sockets");
	}

	EvoSuiteSocket mock = (EvoSuiteSocket) s;

	/*
	 * If the test case has set up an incoming connection, then
	 * simulate an immediate connection.
	 * If not, there is no point in blocking the SUT for
	 * a connection that will never arrive: just throw an exception
	 */

	InetAddress localHost = (InetAddress) options.get(SocketOptions.SO_BINDADDR); 
	NativeTcp tcp = VirtualNetwork.getInstance().pullTcpConnection(localHost.getHostAddress(),localport);

	if(tcp == null) {
		throw new IOException("Simulated exception on waiting server");
	} else {
		mock.openedConnection = tcp;
		mock.setOption(SocketOptions.SO_BINDADDR, localHost);		
		mock.setLocalPort(localport);
		mock.setRemoteAddress(InetAddress.getByName(tcp.getRemoteEndPoint().getHost()));
		mock.setRemotePort(tcp.getRemoteEndPoint().getPort());
	}
}
 
Example 31
Project: evosuite   File: MockServerSocket.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized void setSoTimeout(int timeout) throws SocketException {
       if(!MockFramework.isEnabled()){
           super.setSoTimeout(timeout);
           return;
       }
       if (isClosed())
		throw new SocketException("Socket is closed");
	getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
}
 
Example 32
Project: evosuite   File: MockServerSocket.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized int getSoTimeout() throws IOException {
       if(!MockFramework.isEnabled()){
           return super.getSoTimeout();
       }
       if (isClosed())
		throw new SocketException("Socket is closed");
	Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
	/* extra type safety */
	if (o instanceof Integer) {
		return ((Integer) o).intValue();
	} else {
		return 0;
	}
}
 
Example 33
Project: evosuite   File: MockServerSocket.java   Source Code and License 5 votes vote down vote up
@Override
public void setReuseAddress(boolean on) throws SocketException {
       if(!MockFramework.isEnabled()){
           super.setReuseAddress(on);
           return;
       }
       if (isClosed())
		throw new SocketException("Socket is closed");
	getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
}
 
Example 34
Project: evosuite   File: MockServerSocket.java   Source Code and License 5 votes vote down vote up
@Override
public boolean getReuseAddress() throws SocketException {
       if(!MockFramework.isEnabled()){
           return super.getReuseAddress();
       }
       if (isClosed())
		throw new SocketException("Socket is closed");
	return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue();
}
 
Example 35
Project: In-the-Box-Fork   File: PlainServerSocketImpl.java   Source Code and License 5 votes vote down vote up
@Override
protected void create(boolean isStreaming) throws IOException {
    super.create(isStreaming);
    if (isStreaming) {
        setOption(SocketOptions.SO_REUSEADDR, Boolean.TRUE);
    }
}
 
Example 36
Project: cn1   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
/**
 * @see java.net.Socket#getKeepAlive()
 */
@Override
public boolean getKeepAlive() throws SocketException {
    checkOpen();
    return ((Boolean) socketImpl.getOption(SocketOptions.SO_KEEPALIVE))
            .booleanValue();
}
 
Example 37
Project: cn1   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
/**
 * @see java.net.Socket#getOOBInline()
 */
@Override
public boolean getOOBInline() throws SocketException {
    checkOpen();
    return ((Boolean) socketImpl.getOption(SocketOptions.SO_OOBINLINE))
            .booleanValue();
}
 
Example 38
Project: cn1   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
/**
 * @see java.net.Socket#getSoLinger()
 */
@Override
public int getSoLinger() throws SocketException {
    checkOpen();
    return ((Integer) socketImpl.getOption(SocketOptions.SO_LINGER))
            .intValue();
}
 
Example 39
Project: cn1   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
/**
 * @see java.net.Socket#getTcpNoDelay()
 */
@Override
public boolean getTcpNoDelay() throws SocketException {
    checkOpen();
    return ((Boolean) socketImpl.getOption(SocketOptions.TCP_NODELAY))
            .booleanValue();
}
 
Example 40
Project: cn1   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized void setReceiveBufferSize(int size)
        throws SocketException {
    checkOpen();
    if (size < 1) {
        throw new IllegalArgumentException(Messages.getString("nio.0E")); //$NON-NLS-1$
    }
    socketImpl
            .setOption(SocketOptions.SO_RCVBUF, Integer.valueOf(size));
}
 
Example 41
Project: cn1   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized void setSendBufferSize(int size) throws SocketException {
    checkOpen();
    if (size < 1) {
        throw new IllegalArgumentException(Messages.getString("nio.0E")); //$NON-NLS-1$
    }
    socketImpl.setOption(SocketOptions.SO_SNDBUF, Integer.valueOf(size));
}
 
Example 42
Project: cn1   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
@Override
public void setSoLinger(boolean on, int timeout) throws SocketException {
    checkOpen();
    if (on && timeout < 0) {
        throw new IllegalArgumentException(Messages.getString("nio.0F")); //$NON-NLS-1$
    }
    int val = on ? (65535 < timeout ? 65535 : timeout) : -1;
    socketImpl.setOption(SocketOptions.SO_LINGER, Integer.valueOf(val));
}
 
Example 43
Project: cn1   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized void setSoTimeout(int timeout) throws SocketException {
    checkOpen();
    if (timeout < 0) {
        throw new IllegalArgumentException(Messages.getString("nio.10")); //$NON-NLS-1$
    }
    socketImpl.setOption(SocketOptions.SO_TIMEOUT, Integer.valueOf(timeout));
}
 
Example 44
Project: cn1   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
@Override
public void setTrafficClass(int value) throws SocketException {
    checkOpen();
    if (value < 0 || value > 255) {
        throw new IllegalArgumentException();
    }
    socketImpl.setOption(SocketOptions.IP_TOS, Integer.valueOf(value));
}
 
Example 45
Project: cn1   File: PlainSocketImpl.java   Source Code and License 5 votes vote down vote up
@Override
public void setOption(int optID, Object val) throws SocketException {
    if (optID == SocketOptions.SO_TIMEOUT) {
        receiveTimeout = ((Integer) val).intValue();
    } else {
        try {
            netImpl.setSocketOption(fd, optID, val);
            if (optID == SocketOptions.TCP_NODELAY
                    && (netImpl.getSocketFlags() & TCP_NODELAY) != 0) {
                tcpNoDelay = ((Boolean) val).booleanValue();
            }
        } catch (SocketException e) {
            // we don't throw an exception for IP_TOS even if the platform
            // won't let us set the requested value
            if (optID != SocketOptions.IP_TOS) {
                throw e;
            }
        }

        /*
         * save this value as it is actually used differently for IPv4 and
         * IPv6 so we cannot get the value using the getOption. The option
         * is actually only set for IPv4 and a masked version of the value
         * will be set as only a subset of the values are allowed on the
         * socket. Therefore we need to retain it to return the value that
         * was set. We also need the value to be passed into a number of
         * natives so that it can be used properly with IPv6
         */
        if (optID == SocketOptions.IP_TOS) {
            trafficClass = ((Integer) val).intValue();
        }
    }
}
 
Example 46
Project: JamVM-PH   File: VMPlainSocketImpl.java   Source Code and License 5 votes vote down vote up
public void setOption(int optionId, Object optionValue)
  throws SocketException
{
  int value;
  if (optionValue instanceof Integer)
    value = ((Integer) optionValue).intValue();
  else if (optionValue instanceof Boolean)
    // Switching off the linger behavior is done by setting
    // the value to -1. This is how the Java API interprets
    // it.
    value = ((Boolean) optionValue).booleanValue()
            ? 1
            : (optionId == SocketOptions.SO_LINGER)
            ? -1
            : 0;
  else
    throw new IllegalArgumentException("option value type "
                                       + optionValue.getClass().getName());
  
  try
    {
      setOption(nfd.getNativeFD(), optionId, value);
    }
  catch (IOException ioe)
    {
      SocketException se = new SocketException();
      se.initCause(ioe);
      throw se;
    }
}
 
Example 47
Project: JamVM-PH   File: VMPlainSocketImpl.java   Source Code and License 5 votes vote down vote up
/**
 * Get a socket option. This implementation is only required to support
 * socket options that are boolean values, which include:
 * 
 *  SocketOptions.IP_MULTICAST_LOOP
 *  SocketOptions.SO_BROADCAST
 *  SocketOptions.SO_KEEPALIVE
 *  SocketOptions.SO_OOBINLINE
 *  SocketOptions.SO_REUSEADDR
 *  SocketOptions.TCP_NODELAY
 * 
 * and socket options that are integer values, which include:
 * 
 *  SocketOptions.IP_TOS
 *  SocketOptions.SO_LINGER
 *  SocketOptions.SO_RCVBUF
 *  SocketOptions.SO_SNDBUF
 *  SocketOptions.SO_TIMEOUT
 *
 * @param optionId The option ID to fetch.
 * @return A {@link Boolean} or {@link Integer} containing the socket
 *  option.
 * @throws SocketException
 */
public Object getOption(int optionId) throws SocketException
{
  int value;
  try
    {
      value = getOption(nfd.getNativeFD(), optionId);
    }
  catch (IOException ioe)
    {
      SocketException se = new SocketException();
      se.initCause(ioe);
      throw se;
    }
  
  switch (optionId)
    {
      case SocketOptions.IP_MULTICAST_LOOP:
      case SocketOptions.SO_BROADCAST:
      case SocketOptions.SO_KEEPALIVE:
      case SocketOptions.SO_OOBINLINE:
      case SocketOptions.SO_REUSEADDR:
      case SocketOptions.TCP_NODELAY:
        return Boolean.valueOf(value != 0);
        
      case SocketOptions.IP_TOS:
      case SocketOptions.SO_LINGER:
      case SocketOptions.SO_RCVBUF:
      case SocketOptions.SO_SNDBUF:
      case SocketOptions.SO_TIMEOUT:
        return new Integer(value);
        
      default:
        throw new SocketException("getting option " + optionId +
                                  " not supported here");
    }
}
 
Example 48
Project: quarkcoinj   File: OrchidSocketImpl.java   Source Code and License 5 votes vote down vote up
public Object getOption(int optID) throws SocketException {
	if(optID == SocketOptions.SO_LINGER) {
		return 0;
	} else if(optID == SocketOptions.TCP_NODELAY) {
		return Boolean.TRUE;
	} else if(optID == SocketOptions.SO_TIMEOUT) {
		return 0;
	} else {
		return 0;
	}
}
 
Example 49
Project: silvertunnel-monteux   File: NetSocket2SocketImpl.java   Source Code and License 5 votes vote down vote up
@Override
public Object getOption(final int key) throws SocketException
{
	if (key == SocketOptions.SO_TIMEOUT)
	{
		// get timeout
		return inputStreamTimeout;
	}
	if (key == SocketOptions.TCP_NODELAY)
	{
		return tcpNodelay;
	}
	if (key == SocketOptions.SO_LINGER)
	{
		// get timeout
		return soLinger;
	}

	// unsupported option
	final String msg = "no implementation for getOption(" + key
			+ "). List of all options in java.net.SocketOptions.";
	if (logger.isDebugEnabled())
	{
		logger.debug(msg, new Throwable("method not completely implemented"));
	}
	else
	{
		logger.info(msg + " - Log with level=debug to get call hierarchy.");
	}
	return null;
}
 
Example 50
Project: animecoinj   File: OrchidSocketImpl.java   Source Code and License 5 votes vote down vote up
public Object getOption(int optID) throws SocketException {
	if(optID == SocketOptions.SO_LINGER) {
		return 0;
	} else if(optID == SocketOptions.TCP_NODELAY) {
		return Boolean.TRUE;
	} else if(optID == SocketOptions.SO_TIMEOUT) {
		return 0;
	} else {
		return 0;
	}
}
 
Example 51
Project: docker-java   File: AFUNIXSocketImpl.java   Source Code and License 5 votes vote down vote up
@Override
public void setOption(int optID, Object value) throws SocketException {
  try {
    super.setOption(optID, value);
  } catch (SocketException e) {
    switch (optID) {
      case SocketOptions.TCP_NODELAY:
        return;
      default:
        throw e;
    }
  }
}
 
Example 52
Project: docker-java   File: AFUNIXSocketImpl.java   Source Code and License 5 votes vote down vote up
@Override
public Object getOption(int optID) throws SocketException {
  try {
    return super.getOption(optID);
  } catch (SocketException e) {
    switch (optID) {
      case SocketOptions.TCP_NODELAY:
      case SocketOptions.SO_KEEPALIVE:
        return false;
      default:
        throw e;
    }
  }
}
 
Example 53
Project: classpath   File: VMPlainSocketImpl.java   Source Code and License 5 votes vote down vote up
public void setOption(int optionId, Object optionValue)
  throws SocketException
{
  int value;
  if (optionValue instanceof Integer)
    value = ((Integer) optionValue).intValue();
  else if (optionValue instanceof Boolean)
    // Switching off the linger behavior is done by setting
    // the value to -1. This is how the Java API interprets
    // it.
    value = ((Boolean) optionValue).booleanValue()
            ? 1
            : (optionId == SocketOptions.SO_LINGER)
            ? -1
            : 0;
  else
    throw new IllegalArgumentException("option value type "
                                       + optionValue.getClass().getName());

  try
    {
      setOption(nfd.getNativeFD(), optionId, value);
    }
  catch (IOException ioe)
    {
      SocketException se = new SocketException();
      se.initCause(ioe);
      throw se;
    }
}
 
Example 54
Project: classpath   File: VMPlainSocketImpl.java   Source Code and License 5 votes vote down vote up
/**
 * Get a socket option. This implementation is only required to support
 * socket options that are boolean values, which include:
 *
 *  SocketOptions.IP_MULTICAST_LOOP
 *  SocketOptions.SO_BROADCAST
 *  SocketOptions.SO_KEEPALIVE
 *  SocketOptions.SO_OOBINLINE
 *  SocketOptions.SO_REUSEADDR
 *  SocketOptions.TCP_NODELAY
 *
 * and socket options that are integer values, which include:
 *
 *  SocketOptions.IP_TOS
 *  SocketOptions.SO_LINGER
 *  SocketOptions.SO_RCVBUF
 *  SocketOptions.SO_SNDBUF
 *  SocketOptions.SO_TIMEOUT
 *
 * @param optionId The option ID to fetch.
 * @return A {@link Boolean} or {@link Integer} containing the socket
 *  option.
 * @throws SocketException
 */
public Object getOption(int optionId) throws SocketException
{
  int value;
  try
    {
      value = getOption(nfd.getNativeFD(), optionId);
    }
  catch (IOException ioe)
    {
      SocketException se = new SocketException();
      se.initCause(ioe);
      throw se;
    }

  switch (optionId)
    {
      case SocketOptions.IP_MULTICAST_LOOP:
      case SocketOptions.SO_BROADCAST:
      case SocketOptions.SO_KEEPALIVE:
      case SocketOptions.SO_OOBINLINE:
      case SocketOptions.SO_REUSEADDR:
      case SocketOptions.TCP_NODELAY:
        return Boolean.valueOf(value != 0);

      case SocketOptions.IP_TOS:
      case SocketOptions.SO_LINGER:
      case SocketOptions.SO_RCVBUF:
      case SocketOptions.SO_SNDBUF:
      case SocketOptions.SO_TIMEOUT:
        return new Integer(value);

      default:
        throw new SocketException("getting option " + optionId +
                                  " not supported here");
    }
}
 
Example 55
Project: freeVM   File: PlainSocketImpl.java   Source Code and License 5 votes vote down vote up
@Override
public void setOption(int optID, Object val) throws SocketException {
    if (optID == SocketOptions.SO_TIMEOUT) {
        receiveTimeout = ((Integer) val).intValue();
    } else {
        try {
            netImpl.setSocketOption(fd, optID, val);
            if (optID == SocketOptions.TCP_NODELAY
                    && (netImpl.getSocketFlags() & TCP_NODELAY) != 0) {
                tcpNoDelay = ((Boolean) val).booleanValue();
            }
        } catch (SocketException e) {
            // we don't throw an exception for IP_TOS even if the platform
            // won't let us set the requested value
            if (optID != SocketOptions.IP_TOS) {
                throw e;
            }
        }

        /*
         * save this value as it is actually used differently for IPv4 and
         * IPv6 so we cannot get the value using the getOption. The option
         * is actually only set for IPv4 and a masked version of the value
         * will be set as only a subset of the values are allowed on the
         * socket. Therefore we need to retain it to return the value that
         * was set. We also need the value to be passed into a number of
         * natives so that it can be used properly with IPv6
         */
        if (optID == SocketOptions.IP_TOS) {
            trafficClass = ((Integer) val).intValue();
        }
    }
}
 
Example 56
Project: freeVM   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
/**
 * @see java.net.Socket#getKeepAlive()
 */
@Override
public boolean getKeepAlive() throws SocketException {
    checkOpen();
    return ((Boolean) socketImpl.getOption(SocketOptions.SO_KEEPALIVE))
            .booleanValue();
}
 
Example 57
Project: freeVM   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
/**
 * @see java.net.Socket#getOOBInline()
 */
@Override
public boolean getOOBInline() throws SocketException {
    checkOpen();
    return ((Boolean) socketImpl.getOption(SocketOptions.SO_OOBINLINE))
            .booleanValue();
}
 
Example 58
Project: freeVM   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
/**
 * @see java.net.Socket#getSoLinger()
 */
@Override
public int getSoLinger() throws SocketException {
    checkOpen();
    return ((Integer) socketImpl.getOption(SocketOptions.SO_LINGER))
            .intValue();
}
 
Example 59
Project: freeVM   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
/**
 * @see java.net.Socket#getTcpNoDelay()
 */
@Override
public boolean getTcpNoDelay() throws SocketException {
    checkOpen();
    return ((Boolean) socketImpl.getOption(SocketOptions.TCP_NODELAY))
            .booleanValue();
}
 
Example 60
Project: freeVM   File: SocketChannelImpl.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized void setReceiveBufferSize(int size)
        throws SocketException {
    checkOpen();
    if (size < 1) {
        throw new IllegalArgumentException(Messages.getString("nio.0E")); //$NON-NLS-1$
    }
    socketImpl
            .setOption(SocketOptions.SO_RCVBUF, Integer.valueOf(size));
}