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

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

本文整理了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

  1. @Override
  2. public OioServerSocketChannelConfig setSoTimeout(int timeout) {
  3. try {
  4. javaSocket.setSoTimeout(timeout);
  5. } catch (IOException e) {
  6. throw new ChannelException(e);
  7. }
  8. return this;
  9. }

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

  1. @Override
  2. public OioServerSocketChannelConfig setSoTimeout(int timeout) {
  3. try {
  4. javaSocket.setSoTimeout(timeout);
  5. } catch (IOException e) {
  6. throw new ChannelException(e);
  7. }
  8. return this;
  9. }

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

  1. /**
  2. * Starts the python script.
  3. *
  4. * @throws IOException
  5. */
  6. public void open() throws IOException {
  7. server = new ServerSocket(0);
  8. server.setSoTimeout(50);
  9. startPython();
  10. }

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

  1. /**
  2. * 创建ServerSocket,使用自动分配端口
  3. */
  4. public void init() {
  5. try {
  6. socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[]{127, 0, 0, 1}));
  7. socket.setSoTimeout(5000);
  8. port = socket.getLocalPort();
  9. Log.d(LOG_TAG, "port " + port + " obtained");
  10. } catch (UnknownHostException e) {
  11. Log.e(LOG_TAG, "Error initializing server", e);
  12. } catch (IOException e) {
  13. Log.e(LOG_TAG, "Error initializing server", e);
  14. }
  15. }

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

  1. @Override
  2. public OioServerSocketChannelConfig setSoTimeout(int timeout) {
  3. try {
  4. javaSocket.setSoTimeout(timeout);
  5. } catch (IOException e) {
  6. throw new ChannelException(e);
  7. }
  8. return this;
  9. }

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

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

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

  1. public NetJavaServerSocketImpl (Protocol protocol, String hostname, int port, ServerSocketHints hints) {
  2. this.protocol = protocol;
  3. // create the server socket
  4. try {
  5. // initialize
  6. server = new java.net.ServerSocket();
  7. if (hints != null) {
  8. server.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency,
  9. hints.performancePrefBandwidth);
  10. server.setReuseAddress(hints.reuseAddress);
  11. server.setSoTimeout(hints.acceptTimeout);
  12. server.setReceiveBufferSize(hints.receiveBufferSize);
  13. }
  14. // and bind the server...
  15. InetSocketAddress address;
  16. if( hostname != null ) {
  17. address = new InetSocketAddress(hostname, port);
  18. } else {
  19. address = new InetSocketAddress(port);
  20. }
  21. if (hints != null) {
  22. server.bind(address, hints.backlog);
  23. } else {
  24. server.bind(address);
  25. }
  26. } catch (Exception e) {
  27. throw new GdxRuntimeException("Cannot create a server socket at port " + port + ".", e);
  28. }
  29. }

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

  1. public NetJavaServerSocketImpl (Protocol protocol, String hostname, int port, ServerSocketHints hints) {
  2. this.protocol = protocol;
  3. // create the server socket
  4. try {
  5. // initialize
  6. server = new java.net.ServerSocket();
  7. if (hints != null) {
  8. server.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency,
  9. hints.performancePrefBandwidth);
  10. server.setReuseAddress(hints.reuseAddress);
  11. server.setSoTimeout(hints.acceptTimeout);
  12. server.setReceiveBufferSize(hints.receiveBufferSize);
  13. }
  14. // and bind the server...
  15. InetSocketAddress address;
  16. if( hostname != null ) {
  17. address = new InetSocketAddress(hostname, port);
  18. } else {
  19. address = new InetSocketAddress(port);
  20. }
  21. if (hints != null) {
  22. server.bind(address, hints.backlog);
  23. } else {
  24. server.bind(address);
  25. }
  26. } catch (Exception e) {
  27. throw new GdxRuntimeException("Cannot create a server socket at port " + port + ".", e);
  28. }
  29. }

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

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

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

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

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

  1. public Acceptor(ServerSocket serverSocket, URI uri) {
  2. socket = serverSocket;
  3. target = uri;
  4. pause.set(new CountDownLatch(0));
  5. try {
  6. socket.setSoTimeout(ACCEPT_TIMEOUT_MILLIS);
  7. } catch (SocketException e) {
  8. e.printStackTrace();
  9. }
  10. }

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

  1. public PortForwarder(int from, int to) throws IOException {
  2. this.to = to;
  3. serverSocket = new ServerSocket(from);
  4. serverSocket.setSoTimeout(30000);
  5. this.start();
  6. }

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

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

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

  1. try {
  2. serverSocket = createServerSocket(port);
  3. serverSocket.setSoTimeout(1000);
  4. serverSocket.setSoTimeout(1000);

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

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

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

  1. /**
  2. * 启动Greys服务端
  3. *
  4. * @param configure 配置信息
  5. * @throws IOException 服务器启动失败
  6. */
  7. public void bind(Configure configure) throws IOException {
  8. if (!isBindRef.compareAndSet(false, true)) {
  9. throw new IllegalStateException("already bind");
  10. }
  11. try {
  12. serverSocketChannel = ServerSocketChannel.open();
  13. selector = Selector.open();
  14. serverSocketChannel.configureBlocking(false);
  15. serverSocketChannel.socket().setSoTimeout(configure.getConnectTimeout());
  16. serverSocketChannel.socket().setReuseAddress(true);
  17. serverSocketChannel.register(selector, OP_ACCEPT);
  18. // 服务器挂载端口
  19. serverSocketChannel.socket().bind(getInetSocketAddress(configure.getTargetIp(), configure.getTargetPort()), 24);
  20. logger.info("ga-server listening on network={};port={};timeout={};", configure.getTargetIp(),
  21. configure.getTargetPort(),
  22. configure.getConnectTimeout());
  23. activeSelectorDaemon(selector, configure);
  24. } catch (IOException e) {
  25. unbind();
  26. throw e;
  27. }
  28. }

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

  1. public MockServer start() throws IOException {
  2. _server = new ServerSocket();
  3. _server.setSoTimeout(5000);
  4. _server.bind(new InetSocketAddress("localhost", 0));
  5. _serverSocketPort = _server.getLocalPort();
  6. _threads.add(new EasyThread() {
  7. @Override
  8. void runQuietly() throws Exception {
  9. runServer();
  10. }
  11. }.startThread());
  12. return this;
  13. }

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

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

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

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

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

  1. public static ServerSocket createServerSocket(final int port, final ServerSocketConfiguration config)
  2. throws IOException, KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException {
  3. if (config == null) {
  4. throw new NullPointerException("Configuration may not be null.");
  5. }
  6. final SSLContext sslContext = config.createSSLContext();
  7. final ServerSocket serverSocket;
  8. if (sslContext == null) {
  9. serverSocket = new ServerSocket(port);
  10. } else {
  11. serverSocket = sslContext.getServerSocketFactory().createServerSocket(port);
  12. ((SSLServerSocket) serverSocket).setNeedClientAuth(config.getNeedClientAuth());
  13. }
  14. if (config.getSocketTimeout() != null) {
  15. serverSocket.setSoTimeout(config.getSocketTimeout());
  16. }
  17. if (config.getReuseAddress() != null) {
  18. serverSocket.setReuseAddress(config.getReuseAddress());
  19. }
  20. if (config.getReceiveBufferSize() != null) {
  21. serverSocket.setReceiveBufferSize(config.getReceiveBufferSize());
  22. }
  23. return serverSocket;
  24. }

相关文章