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

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

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

ServerSocket.setReuseAddress介绍

[英]Sets the value for the socket option SocketOptions.SO_REUSEADDR.
[中]设置套接字选项SocketOptions的值。所以!。

代码示例

代码示例来源:origin: Alluxio/alluxio

private static boolean isLocalPortAvailable(int port) {
  try (ServerSocket socket = new ServerSocket(port)) {
   socket.setReuseAddress(true);
   return true;
  } catch (IOException e) {
   return false;
  }
 }
}

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

/**
 * Starts listening to slave connections.
 *
 * @throws Exception If fails.
 */
public void beginAccept() throws Exception {
  this.serverSocketChannel = ServerSocketChannel.open();
  this.selector = RemotingUtil.openSelector();
  this.serverSocketChannel.socket().setReuseAddress(true);
  this.serverSocketChannel.socket().bind(this.socketAddressListen);
  this.serverSocketChannel.configureBlocking(false);
  this.serverSocketChannel.register(this.selector, SelectionKey.OP_ACCEPT);
}

代码示例来源: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: go-lang-plugin-org/go-lang-idea-plugin

private static int findFreePort() {
  try(ServerSocket socket = new ServerSocket(0)) {
   socket.setReuseAddress(true);
   return socket.getLocalPort();
  }
  catch (Exception ignore) {
  }
  throw new IllegalStateException("Could not find a free TCP/IP port to start dlv");
 }
}

代码示例来源: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: square/okhttp

serverSocket = serverSocketFactory.createServerSocket();
serverSocket.setReuseAddress(inetSocketAddress.getPort() != 0);
serverSocket.bind(inetSocketAddress, 50);

代码示例来源:origin: ballerina-platform/ballerina-lang

private static int findFreePort() {
    try (ServerSocket socket = new ServerSocket(0)) {
      socket.setReuseAddress(true);
      return socket.getLocalPort();
    } catch (Exception ignore) {
    }
    throw new IllegalStateException("Could not find a free TCP/IP port to start debugging");
  }
}

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

try {
 server = new ServerSocket();
 server.setReuseAddress(true);
 if (addr != null) {
  server.bind(new InetSocketAddress(addr, port));
 } else {
  server.bind(new InetSocketAddress(port));

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

ListenTask(int port) throws IOException {
  this.port = port;
  this.ssc = ServerSocketChannel.open();
  ssc.socket().setReuseAddress(true);
  ssc.socket().bind(new InetSocketAddress(port), LISTEN_BACKLOG);
  ssc.configureBlocking(false);
  endpoint = new EndPoint(NioSelectorScheduler.this,ssc);

  // if port is automatically assigned then retrieve actual value
  if(port == 0) {
    this.port = ssc.socket().getLocalPort();
  };    
}

代码示例来源:origin: alibaba/Sentinel

/**
 * Get a server socket from an available port from a base port.<br>
 * Increasing on port number will occur when the port has already been used.
 *
 * @param basePort base port to start
 * @return new socket with available port
 */
private static ServerSocket getServerSocketFromBasePort(int basePort) {
  int tryCount = 0;
  while (true) {
    try {
      ServerSocket server = new ServerSocket(basePort + tryCount / 3, 100);
      server.setReuseAddress(true);
      return server;
    } catch (IOException e) {
      tryCount++;
      try {
        TimeUnit.MILLISECONDS.sleep(30);
      } catch (InterruptedException e1) {
        break;
      }
    }
  }
  return null;
}

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

ss = new ServerSocket();
ss.setReuseAddress( true );
ss.bind(new InetSocketAddress((InetAddress) null, port), 0);

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

@Override
public void configure(InetSocketAddress addr, int maxcc) throws IOException {
  configureSaslLogin();
  thread = new ZooKeeperThread(this, "NIOServerCxn.Factory:" + addr);
  thread.setDaemon(true);
  maxClientCnxns = maxcc;
  this.ss = ServerSocketChannel.open();
  ss.socket().setReuseAddress(true);
  LOG.info("binding to port " + addr);
  ss.socket().bind(addr);
  ss.configureBlocking(false);
  ss.register(selector, SelectionKey.OP_ACCEPT);
}

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

protected static int getNextTCPPort(InetAddress bind_addr, int start_port) throws Exception {
  try(ServerSocket sock=new ServerSocket()) {
    sock.setReuseAddress(false);
    Util.bind(sock, bind_addr, start_port, start_port+100);
    return sock.getLocalPort();
  }
}

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

while((!shutdown) && (numRetries < 3)){
  try {
    ss = new ServerSocket();
    ss.setReuseAddress(true);
    if (listenOnAllIPs) {
      int port = view.get(QuorumCnxManager.this.mySid)
    setName(view.get(QuorumCnxManager.this.mySid)
        .electionAddr.toString());
    ss.bind(addr);
    while (!shutdown) {
      Socket client = ss.accept();

代码示例来源:origin: peter-lawrey/Java-Chronicle

public InProcessChronicleSource(@NotNull Chronicle chronicle, int port) throws IOException {
  this.chronicle = chronicle;
  server = ServerSocketChannel.open();
  server.socket().setReuseAddress(true);
  server.socket().bind(new InetSocketAddress(port));
  name = chronicle.name() + "@" + port;
  logger = Logger.getLogger(getClass().getName() + "." + name);
  service = Executors.newCachedThreadPool(new NamedThreadFactory(name));
  service.execute(new Acceptor());
}

代码示例来源:origin: oblac/jodd

/**
 * Starts HTTP tunnel. Method ends when the tunnel is stopped.
 */
public void start() throws IOException {
  serverSocket = new ServerSocket(listenPort, socketBacklog);
  serverSocket.setReuseAddress(true);
  executorService = Executors.newFixedThreadPool(threadPoolSize);
  running = true;
  while (running) {
    Socket socket = serverSocket.accept();
    socket.setKeepAlive(false);
    executorService.execute(onSocketConnection(socket));
  }
  executorService.shutdown();
}

代码示例来源:origin: pentaho/pentaho-kettle

private ServerSocket createServerSocket( int port ) throws IOException {
 ServerSocket serverSocket = new ServerSocket();
 serverSocket.setPerformancePreferences( 1, 2, 3 ); // order of importance: bandwidth, latency, connection time
 serverSocket.setReuseAddress( true );
  serverSocket.bind( new InetSocketAddress( port ) );
 } catch ( BindException e ) {
  long totalWait = 0L;
     + port + " after a " + ( totalWait / 1000 ) + " seconds wait..." );
    Thread.sleep( 10000 ); // wait 10 seconds, try again...
    serverSocket.bind( new InetSocketAddress( port ), 100 );
   } catch ( IOException ioe ) {
    ioException = ioe;

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

@Override
public void reconfigure(InetSocketAddress addr) {
  ServerSocketChannel oldSS = ss;        
  try {
    acceptThread.setReconfiguring();
    tryClose(oldSS);
    acceptThread.wakeupSelector();
    try {
      acceptThread.join();
    } catch (InterruptedException e) {
      LOG.error("Error joining old acceptThread when reconfiguring client port {}",
            e.getMessage());
      Thread.currentThread().interrupt();
    }
    this.ss = ServerSocketChannel.open();
    ss.socket().setReuseAddress(true);
    LOG.info("binding to port " + addr);
    ss.socket().bind(addr);
    ss.configureBlocking(false);
    acceptThread = new AcceptThread(ss, addr, selectorThreads);
    acceptThread.start();
  } catch(IOException e) {
    LOG.error("Error reconfiguring client port to {} {}", addr, e.getMessage());
    tryClose(oldSS);
  }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

private ServerSocket getServerSocket() throws IOException {
  ServerSocket serverSocket = null;
  try {
    serverSocket = new ServerSocket(port, 100);
    serverSocket.setReuseAddress(true);
  } catch (BindException e) {
    port = port + 1;
    serverSocket = getServerSocket();
    if (portFindTimes++ > 50) {
      throw e;
    }
  }
  return serverSocket;
}

代码示例来源:origin: org.apache.lucene/lucene-core

final int maxClients = Integer.parseInt(args[arg++]);
try (final ServerSocket s = new ServerSocket()) {
 s.setReuseAddress(true);
 s.setSoTimeout(30000); // initially 30 secs to give clients enough time to startup
 s.bind(new InetSocketAddress(hostname, 0));
 final InetSocketAddress localAddr = (InetSocketAddress) s.getLocalSocketAddress();
 System.out.println("Listening on " + localAddr + "...");

相关文章