当前位置: 首页>>代码示例>>Java>>正文


Java SocketOptions类代码示例

本文整理汇总了Java中java.net.SocketOptions的典型用法代码示例。如果您正苦于以下问题:Java SocketOptions类的具体用法?Java SocketOptions怎么用?Java SocketOptions使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


SocketOptions类属于java.net包,在下文中一共展示了SocketOptions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getOption

import java.net.SocketOptions; //导入依赖的package包/类
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;
    }
}
 
开发者ID:B4dT0bi,项目名称:silvertunnel-ng,代码行数:22,代码来源:LocalSocketImpl.java

示例2: bind

import java.net.SocketOptions; //导入依赖的package包/类
@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;
}
 
开发者ID:EvoSuite,项目名称:evosuite,代码行数:17,代码来源:EvoSuiteSocket.java

示例3: isLingerSocket

import java.net.SocketOptions; //导入依赖的package包/类
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;
    }
}
 
开发者ID:keplersj,项目名称:In-the-Box-Fork,代码行数:17,代码来源:BlockGuard.java

示例4: getOption

import java.net.SocketOptions; //导入依赖的package包/类
@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;
    }
}
 
开发者ID:mnhkahn,项目名称:cInterphone,代码行数:22,代码来源:PlainDatagramSocketImpl.java

示例5: getOption

import java.net.SocketOptions; //导入依赖的package包/类
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;
    }
}
 
开发者ID:shannah,项目名称:cn1,代码行数:21,代码来源:PlainDatagramSocketImpl.java

示例6: getOption

import java.net.SocketOptions; //导入依赖的package包/类
@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;
    }
}
 
开发者ID:shannah,项目名称:cn1,代码行数:18,代码来源:PlainSocketImpl.java

示例7: getOption

import java.net.SocketOptions; //导入依赖的package包/类
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;
}
 
开发者ID:sirvaliance,项目名称:netlib,代码行数:23,代码来源:NetSocket2SocketImpl.java

示例8: setTrafficClass

import java.net.SocketOptions; //导入依赖的package包/类
/**
 * 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);

}
 
开发者ID:ARMmbed,项目名称:java-coap,代码行数:16,代码来源:QoSDatagramSocket.java

示例9: setOption

import java.net.SocketOptions; //导入依赖的package包/类
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;
    }
}
 
开发者ID:vilie,项目名称:javify,代码行数:31,代码来源:VMPlainSocketImpl.java

示例10: getOption

import java.net.SocketOptions; //导入依赖的package包/类
/**
 * 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");
    }
}
 
开发者ID:vilie,项目名称:javify,代码行数:61,代码来源:VMPlainSocketImpl.java

示例11: setOption

import java.net.SocketOptions; //导入依赖的package包/类
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;
    }
}
 
开发者ID:nmldiegues,项目名称:jvm-stm,代码行数:31,代码来源:VMPlainSocketImpl.java

示例12: getOption

import java.net.SocketOptions; //导入依赖的package包/类
/**
 * 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");
    }
}
 
开发者ID:nmldiegues,项目名称:jvm-stm,代码行数:61,代码来源:VMPlainSocketImpl.java

示例13: create

import java.net.SocketOptions; //导入依赖的package包/类
@Override
protected void create(boolean isStreaming) throws IOException {
    super.create(isStreaming);
    if (isStreaming) {
        setOption(SocketOptions.SO_REUSEADDR, Boolean.TRUE);
    }
}
 
开发者ID:Sellegit,项目名称:j2objc,代码行数:8,代码来源:PlainServerSocketImpl.java

示例14: getOption

import java.net.SocketOptions; //导入依赖的package包/类
@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;
}
 
开发者ID:rovemonteux,项目名称:silvertunnel-monteux,代码行数:32,代码来源:NetSocket2SocketImpl.java

示例15: setSoLinger

import java.net.SocketOptions; //导入依赖的package包/类
@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));
	}
}
 
开发者ID:EvoSuite,项目名称:evosuite,代码行数:16,代码来源:MockSocket.java


注:本文中的java.net.SocketOptions类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。