本文整理汇总了Java中java.net.SocketOption类的典型用法代码示例。如果您正苦于以下问题:Java SocketOption类的具体用法?Java SocketOption怎么用?Java SocketOption使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SocketOption类属于java.net包,在下文中一共展示了SocketOption类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOption
import java.net.SocketOption; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public final <T> T getOption(SocketOption<T> name) throws IOException {
if (name == null)
throw new NullPointerException();
if (!supportedOptions().contains(name))
throw new UnsupportedOperationException("'" + name + "' not supported");
try {
begin();
if (name == StandardSocketOptions.SO_REUSEADDR &&
Net.useExclusiveBind())
{
// SO_REUSEADDR emulated when using exclusive bind
return (T)Boolean.valueOf(isReuseAddress);
}
return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
} finally {
end();
}
}
示例2: setOption
import java.net.SocketOption; //导入依赖的package包/类
@Override
public final <T> AsynchronousServerSocketChannel setOption(SocketOption<T> name,
T value)
throws IOException
{
if (name == null)
throw new NullPointerException();
if (!supportedOptions().contains(name))
throw new UnsupportedOperationException("'" + name + "' not supported");
try {
begin();
if (name == StandardSocketOptions.SO_REUSEADDR &&
Net.useExclusiveBind())
{
// SO_REUSEADDR emulated when using exclusive bind
isReuseAddress = (Boolean)value;
} else {
Net.setSocketOption(fd, Net.UNSPEC, name, value);
}
return this;
} finally {
end();
}
}
示例3: setOption
import java.net.SocketOption; //导入依赖的package包/类
@Override
public final <T> AsynchronousSocketChannel setOption(SocketOption<T> name, T value)
throws IOException
{
if (name == null)
throw new NullPointerException();
if (!supportedOptions().contains(name))
throw new UnsupportedOperationException("'" + name + "' not supported");
try {
begin();
if (writeShutdown)
throw new IOException("Connection has been shutdown for writing");
if (name == StandardSocketOptions.SO_REUSEADDR &&
Net.useExclusiveBind())
{
// SO_REUSEADDR emulated when using exclusive bind
isReuseAddress = (Boolean)value;
} else {
Net.setSocketOption(fd, Net.UNSPEC, name, value);
}
return this;
} finally {
end();
}
}
示例4: setOption
import java.net.SocketOption; //导入依赖的package包/类
@Override
public void setOption(FileDescriptor fd,
SocketOption<?> option,
Object value)
throws SocketException
{
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(new NetworkPermission("setOption." + option.name()));
if (fd == null || !fd.valid())
throw new SocketException("socket closed");
if (option == SO_FLOW_SLA) {
assert flowSupported;
SocketFlow flow = checkValueType(value, option.type());
setFlowOption(fd, flow);
} else {
throw new InternalError("Unexpected option " + option);
}
}
示例5: getOption
import java.net.SocketOption; //导入依赖的package包/类
@Override
public Object getOption(FileDescriptor fd,
SocketOption<?> option)
throws SocketException
{
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(new NetworkPermission("getOption." + option.name()));
if (fd == null || !fd.valid())
throw new SocketException("socket closed");
if (option == SO_FLOW_SLA) {
assert flowSupported;
SocketFlow flow = SocketFlow.create();
getFlowOption(fd, flow);
return flow;
} else {
throw new InternalError("Unexpected option " + option);
}
}
示例6: setSocketOption
import java.net.SocketOption; //导入依赖的package包/类
/**
* Sets the supplied option on the channel to have the value if option is a member of
* allowedOptions.
*
* @throws IOException
* if the value could not be set due to IO errors.
* @throws IllegalArgumentException
* if the socket option or the value is invalid.
* @throws UnsupportedOperationException
* if the option is not a member of allowedOptions.
* @throws ClosedChannelException
* if the channel is closed
*/
public static <T> void setSocketOption(
FileDescriptorChannel channel, Set<SocketOption<?>> allowedOptions,
SocketOption<T> option, T value)
throws IOException {
if (!(option instanceof StandardSocketOptions.SocketOptionImpl)) {
throw new IllegalArgumentException("SocketOption must come from StandardSocketOptions");
}
if (!allowedOptions.contains(option)) {
throw new UnsupportedOperationException(
option + " is not supported for this type of socket");
}
if (!channel.getFD().valid()) {
throw new ClosedChannelException();
}
((StandardSocketOptions.SocketOptionImpl<T>) option).setValue(channel.getFD(), value);
}
示例7: getSocketOption
import java.net.SocketOption; //导入依赖的package包/类
/**
* Gets the supplied option from the channel if option is a member of allowedOptions.
*
* @throws IOException
* if the value could not be read due to IO errors.
* @throws IllegalArgumentException
* if the socket option is invalid.
* @throws UnsupportedOperationException
* if the option is not a member of allowedOptions.
* @throws ClosedChannelException
* if the channel is closed
*/
public static <T> T getSocketOption(
FileDescriptorChannel channel, Set<SocketOption<?>> allowedOptions,
SocketOption<T> option)
throws IOException {
if (!(option instanceof StandardSocketOptions.SocketOptionImpl)) {
throw new IllegalArgumentException("SocketOption must come from StandardSocketOptions");
}
if (!allowedOptions.contains(option)) {
throw new UnsupportedOperationException(
option + " is not supported for this type of socket");
}
if (!channel.getFD().valid()) {
throw new ClosedChannelException();
}
return ((StandardSocketOptions.SocketOptionImpl<T>) option).getValue(channel.getFD());
}
示例8: findOption
import java.net.SocketOption; //导入依赖的package包/类
private static SocketOption<?> findOption(int optID) throws SocketException {
switch (optID) {
case TCP_NODELAY:
return StandardSocketOptions.TCP_NODELAY;
case SO_REUSEADDR:
return StandardSocketOptions.SO_REUSEADDR;
case SO_BROADCAST:
return StandardSocketOptions.SO_BROADCAST;
case IP_MULTICAST_IF:
return StandardSocketOptions.IP_MULTICAST_IF;
case IP_MULTICAST_LOOP:
return StandardSocketOptions.IP_MULTICAST_LOOP;
case IP_TOS:
return StandardSocketOptions.IP_TOS;
case SO_LINGER:
return StandardSocketOptions.SO_LINGER;
case SO_SNDBUF:
return StandardSocketOptions.SO_SNDBUF;
case SO_RCVBUF:
return StandardSocketOptions.SO_RCVBUF;
case SO_KEEPALIVE:
return StandardSocketOptions.SO_KEEPALIVE;
default:
throw new SocketException("unrecognized TCP option: " + optID);
}
}
示例9: setOption
import java.net.SocketOption; //导入依赖的package包/类
public <T> Socket setOption(SocketOption<T> name, T value) throws IOException {
try {
SSLSocket.class.getMethod("setOption", SocketOption.class, Object.class).invoke(delegate, name, value);
return this;
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new AssertionError();
}
}