java.net.ServerSocket.setSoTimeout()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(193)

本文整理了Java中java.net.ServerSocket.setSoTimeout()方法的一些代码示例,展示了ServerSocket.setSoTimeout()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ServerSocket.setSoTimeout()方法的具体详情如下:
包路径:java.net.ServerSocket
类名称:ServerSocket
方法名:setSoTimeout

ServerSocket.setSoTimeout介绍

[英]Sets the SocketOptions#SO_TIMEOUT in milliseconds for this socket. This accept timeout defines the period the socket will block waiting to accept a connection before throwing an InterruptedIOException. The value 0 (default) is used to set an infinite timeout. To have effect this option must be set before the blocking method was called.
[中]设置此套接字的SocketOptions#SO_超时(以毫秒为单位)。此接受超时定义套接字在抛出InterruptedIOException之前阻止等待接受连接的时间。值0(默认值)用于设置无限超时。要生效,必须在调用阻塞方法之前设置此选项。

代码示例

代码示例来源:origin: netty/netty

@Override
public OioServerSocketChannelConfig setSoTimeout(int timeout) {
  try {
    javaSocket.setSoTimeout(timeout);
  } catch (IOException e) {
    throw new ChannelException(e);
  }
  return this;
}

代码示例来源:origin: redisson/redisson

@Override
public OioServerSocketChannelConfig setSoTimeout(int timeout) {
  try {
    javaSocket.setSoTimeout(timeout);
  } catch (IOException e) {
    throw new ChannelException(e);
  }
  return this;
}

代码示例来源:origin: apache/flink

/**
 * Starts the python script.
 *
 * @throws IOException
 */
public void open() throws IOException {
  server = new ServerSocket(0);
  server.setSoTimeout(50);
  startPython();
}

代码示例来源:origin: aa112901/remusic

/**
 * 创建ServerSocket,使用自动分配端口
 */
public void init() {
  try {
    socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[]{127, 0, 0, 1}));
    socket.setSoTimeout(5000);
    port = socket.getLocalPort();
    Log.d(LOG_TAG, "port " + port + " obtained");
  } catch (UnknownHostException e) {
    Log.e(LOG_TAG, "Error initializing server", e);
  } catch (IOException e) {
    Log.e(LOG_TAG, "Error initializing server", e);
  }
}

代码示例来源:origin: wildfly/wildfly

@Override
public OioServerSocketChannelConfig setSoTimeout(int timeout) {
  try {
    javaSocket.setSoTimeout(timeout);
  } catch (IOException e) {
    throw new ChannelException(e);
  }
  return this;
}

代码示例来源:origin: apache/flink

private void startPython(String tmpPath, String args) throws IOException {
  String pythonBinaryPath = config.getString(PythonOptions.PYTHON_BINARY_PATH);
  try {
    Runtime.getRuntime().exec(pythonBinaryPath);
  } catch (IOException ignored) {
    throw new RuntimeException(pythonBinaryPath + " does not point to a valid python binary.");
  }
  process = Runtime.getRuntime().exec(pythonBinaryPath + " -B " + tmpPath + FLINK_PYTHON_PLAN_NAME + args);
  new Thread(new StreamPrinter(process.getInputStream())).start();
  new Thread(new StreamPrinter(process.getErrorStream())).start();
  server = new ServerSocket(0);
  server.setSoTimeout(50);
  process.getOutputStream().write("plan\n".getBytes(ConfigConstants.DEFAULT_CHARSET));
  process.getOutputStream().flush();
}

代码示例来源:origin: libgdx/libgdx

public NetJavaServerSocketImpl (Protocol protocol, String hostname, int port, ServerSocketHints hints) {
  this.protocol = protocol;
  // create the server socket
  try {
    // initialize
    server = new java.net.ServerSocket();
    if (hints != null) {
      server.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency,
        hints.performancePrefBandwidth);
      server.setReuseAddress(hints.reuseAddress);
      server.setSoTimeout(hints.acceptTimeout);
      server.setReceiveBufferSize(hints.receiveBufferSize);
    }
    // and bind the server...
    InetSocketAddress address;
    if( hostname != null ) {
      address = new InetSocketAddress(hostname, port); 
    } else {
      address = new InetSocketAddress(port);
    }
    
    if (hints != null) {
      server.bind(address, hints.backlog);
    } else {
      server.bind(address);
    }
  } catch (Exception e) {
    throw new GdxRuntimeException("Cannot create a server socket at port " + port + ".", e);
  }
}

代码示例来源:origin: libgdx/libgdx

public NetJavaServerSocketImpl (Protocol protocol, String hostname, int port, ServerSocketHints hints) {
  this.protocol = protocol;
  // create the server socket
  try {
    // initialize
    server = new java.net.ServerSocket();
    if (hints != null) {
      server.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency,
        hints.performancePrefBandwidth);
      server.setReuseAddress(hints.reuseAddress);
      server.setSoTimeout(hints.acceptTimeout);
      server.setReceiveBufferSize(hints.receiveBufferSize);
    }
    // and bind the server...
    InetSocketAddress address;
    if( hostname != null ) {
      address = new InetSocketAddress(hostname, port); 
    } else {
      address = new InetSocketAddress(port);
    }
    
    if (hints != null) {
      server.bind(address, hints.backlog);
    } else {
      server.bind(address);
    }
  } catch (Exception e) {
    throw new GdxRuntimeException("Cannot create a server socket at port " + port + ".", e);
  }
}

代码示例来源:origin: apache/nifi

public void start() throws IOException {
  logger.debug("Starting Bootstrap Listener to communicate with Bootstrap Port {}", bootstrapPort);
  serverSocket = new ServerSocket();
  serverSocket.bind(new InetSocketAddress("localhost", 0));
  serverSocket.setSoTimeout(2000);
  final int localPort = serverSocket.getLocalPort();
  logger.info("Started Bootstrap Listener, Listening for incoming requests on port {}", localPort);
  listener = new Listener(serverSocket);
  final Thread listenThread = new Thread(listener);
  listenThread.setDaemon(true);
  listenThread.setName("Listen to Bootstrap");
  listenThread.start();
  logger.debug("Notifying Bootstrap that local port is {}", localPort);
  sendCommand("PORT", new String[] { String.valueOf(localPort), secretKey});
}

代码示例来源:origin: jphp-group/jphp

@Signature(@Arg("timeout"))
public Memory setSoTimeout(Environment env, Memory... args) throws SocketException {
  socket.setSoTimeout(args[0].toInteger());
  return Memory.NULL;
}

代码示例来源:origin: apache/activemq

public Acceptor(ServerSocket serverSocket, URI uri) {
  socket = serverSocket;
  target = uri;
  pause.set(new CountDownLatch(0));
  try {
    socket.setSoTimeout(ACCEPT_TIMEOUT_MILLIS);
  } catch (SocketException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: apache/zookeeper

public PortForwarder(int from, int to) throws IOException {
  this.to = to;
  serverSocket = new ServerSocket(from);
  serverSocket.setSoTimeout(30000);
  this.start();
}

代码示例来源:origin: netty/netty

socket.setSoTimeout(SO_TIMEOUT);
  success = true;
} catch (IOException e) {

代码示例来源:origin: log4j/log4j

try {
 serverSocket = createServerSocket(port);
 serverSocket.setSoTimeout(1000);
   serverSocket.setSoTimeout(1000);

代码示例来源:origin: redisson/redisson

socket.setSoTimeout(SO_TIMEOUT);
  success = true;
} catch (IOException e) {

代码示例来源:origin: oldmanpushcart/greys-anatomy

/**
 * 启动Greys服务端
 *
 * @param configure 配置信息
 * @throws IOException 服务器启动失败
 */
public void bind(Configure configure) throws IOException {
  if (!isBindRef.compareAndSet(false, true)) {
    throw new IllegalStateException("already bind");
  }
  try {
    serverSocketChannel = ServerSocketChannel.open();
    selector = Selector.open();
    serverSocketChannel.configureBlocking(false);
    serverSocketChannel.socket().setSoTimeout(configure.getConnectTimeout());
    serverSocketChannel.socket().setReuseAddress(true);
    serverSocketChannel.register(selector, OP_ACCEPT);
    // 服务器挂载端口
    serverSocketChannel.socket().bind(getInetSocketAddress(configure.getTargetIp(), configure.getTargetPort()), 24);
    logger.info("ga-server listening on network={};port={};timeout={};", configure.getTargetIp(),
        configure.getTargetPort(),
        configure.getConnectTimeout());
    activeSelectorDaemon(selector, configure);
  } catch (IOException e) {
    unbind();
    throw e;
  }
}

代码示例来源:origin: apache/incubator-gobblin

public MockServer start() throws IOException {
 _server = new ServerSocket();
 _server.setSoTimeout(5000);
 _server.bind(new InetSocketAddress("localhost", 0));
 _serverSocketPort = _server.getLocalPort();
 _threads.add(new EasyThread() {
  @Override
  void runQuietly() throws Exception {
   runServer();
  }
 }.startThread());
 return this;
}

代码示例来源:origin: wildfly/wildfly

socket.setSoTimeout(SO_TIMEOUT);
  success = true;
} catch (IOException e) {

代码示例来源:origin: jenkinsci/jenkins

ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("localhost",0));
serverSocket.setSoTimeout(10*1000);

代码示例来源:origin: apache/nifi

public static ServerSocket createServerSocket(final int port, final ServerSocketConfiguration config)
    throws IOException, KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException {
  if (config == null) {
    throw new NullPointerException("Configuration may not be null.");
  }
  final SSLContext sslContext = config.createSSLContext();
  final ServerSocket serverSocket;
  if (sslContext == null) {
    serverSocket = new ServerSocket(port);
  } else {
    serverSocket = sslContext.getServerSocketFactory().createServerSocket(port);
    ((SSLServerSocket) serverSocket).setNeedClientAuth(config.getNeedClientAuth());
  }
  if (config.getSocketTimeout() != null) {
    serverSocket.setSoTimeout(config.getSocketTimeout());
  }
  if (config.getReuseAddress() != null) {
    serverSocket.setReuseAddress(config.getReuseAddress());
  }
  if (config.getReceiveBufferSize() != null) {
    serverSocket.setReceiveBufferSize(config.getReceiveBufferSize());
  }
  return serverSocket;
}

相关文章