java.net.BindException.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(105)

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

BindException.<init>介绍

[英]Constructs a new instance.
[中]构造一个新实例。

代码示例

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

public static void bind(Socket sock, InetAddress bind_addr, int start_port, int end_port) throws Exception {
  int original_start_port=start_port;
  while(true) {
    try {
      InetSocketAddress sock_addr=new InetSocketAddress(bind_addr, start_port);
      sock.bind(sock_addr);
    }
    catch(SocketException bind_ex) {
      if(start_port == end_port)
        throw new BindException("No available port to bind to in range [" + original_start_port + " .. " + end_port + "]");
      if(bind_addr != null && !bind_addr.isLoopbackAddress()) {
        NetworkInterface nic=NetworkInterface.getByInetAddress(bind_addr);
        if(nic == null)
          throw new BindException("bind_addr " + bind_addr + " is not a valid interface: " + bind_ex);
      }
      start_port++;
      continue;
    }
    break;
  }
}

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

public static void bind(ServerSocket srv_sock, InetAddress bind_addr,
            int start_port, int end_port, int backlog) throws Exception {
  int original_start_port=start_port;
  while(true) {
    try {
      InetSocketAddress sock_addr=new InetSocketAddress(bind_addr, start_port);
      srv_sock.bind(sock_addr, backlog);
    }
    catch(SocketException bind_ex) {
      if(start_port == end_port)
        throw new BindException("No available port to bind to in range [" + original_start_port + " .. " + end_port + "]");
      if(bind_addr != null && !bind_addr.isLoopbackAddress()) {
        NetworkInterface nic=NetworkInterface.getByInetAddress(bind_addr);
        if(nic == null)
          throw new BindException("bind_addr " + bind_addr + " is not a valid interface: " + bind_ex);
      }
      start_port++;
      continue;
    }
    break;
  }
}

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

public static void bind(final ServerSocketChannel ch, InetAddress bind_addr, int start_port, int end_port, int backlog) throws Exception {
  int original_start_port=start_port;
  while(true) {
    try {
      ch.bind(new InetSocketAddress(bind_addr, start_port), backlog);
    }
    catch(SocketException bind_ex) {
      if(start_port == end_port)
        throw new BindException("No available port to bind to in range [" + original_start_port + " .. " + end_port + "]");
      if(bind_addr != null && !bind_addr.isLoopbackAddress()) {
        NetworkInterface nic=NetworkInterface.getByInetAddress(bind_addr);
        if(nic == null)
          throw new BindException("bind_addr " + bind_addr + " is not a valid interface: " + bind_ex);
      }
      start_port++;
      continue;
    }
    break;
  }
}

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

public static void checkIfValidAddress(InetAddress bind_addr,String prot_name) throws Exception {
  // N.B. bind_addr.isAnyLocalAddress() is not OK
  if (bind_addr.isLoopbackAddress())
    return;
  Collection<InetAddress> addrs=getAllAvailableAddresses();
  for(InetAddress addr : addrs) {
    if(addr.equals(bind_addr))
      return;
  }
  StringBuilder sb=new StringBuilder();
  if(prot_name != null)
    sb.append('[').append(prot_name).append("] ");
  sb.append(bind_addr).append(" is not a valid address on any local network interface");
  throw new BindException(sb.toString());
}

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

/**
 * Asserts that the specified port is available on the specified network interface, indicated by
 * it's assigned IP address, on this local system.
 *
 * @param bindAddress an InetAddress indicating the bounded network interface to determine whether
 *        the service port is available or not.
 * @param port an integer indicating the network port to listen for client network requests.
 * @throws BindException if the network address and port are not available. Address defaults to
 *         localhost (or all network interfaces on the local system) if null.
 * @see org.apache.geode.internal.AvailablePort
 */
protected static void assertPortAvailable(final InetAddress bindAddress, final int port)
  throws BindException {
 if (!AvailablePort.isPortAvailable(port, AvailablePort.SOCKET, bindAddress)) {
  throw new BindException(
    String.format("Network is unreachable; port (%1$d) is not available on %2$s.", port,
      bindAddress != null ? bindAddress.getCanonicalHostName() : "localhost"));
 }
}

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

public static ServerSocket createServerSocket(SocketFactory factory,String service_name,InetAddress bind_addr,
                       int start_port,int end_port) throws Exception {
  int original_start_port=start_port;
  ServerSocket srv_sock=null;
  while(true) {
    try {
      if(srv_sock != null);
        Util.close(srv_sock);
      srv_sock=factory.createServerSocket(service_name);
      InetSocketAddress sock_addr=new InetSocketAddress(bind_addr, start_port);
      srv_sock.bind(sock_addr);
      return srv_sock;
    }
    catch(SocketException bind_ex) {
      if(start_port == end_port)
        throw new BindException(String.format("no port available in range [%d .. %d] (bind_addr=%s)",
                           original_start_port, end_port, bind_addr));
      if(bind_addr != null && !(bind_addr.isLoopbackAddress() || bind_addr.isAnyLocalAddress())) {
        NetworkInterface nic=NetworkInterface.getByInetAddress(bind_addr);
        if(nic == null)
          throw new BindException("bind_addr " + bind_addr + " is not a valid interface: " + bind_ex);
      }
      start_port++;
    }
  }
}

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

/**
 * @param port
 *      Use 0 to choose a random port.
 */
public TcpSlaveAgentListener(int port) throws IOException {
  super("TCP agent listener port="+port);
  try {
    serverSocket = ServerSocketChannel.open();
    serverSocket.socket().bind(new InetSocketAddress(port));
  } catch (BindException e) {
    throw (BindException)new BindException("Failed to listen on port "+port+" because it's already in use.").initCause(e);
  }
  this.configuredPort = port;
  setUncaughtExceptionHandler((t, e) -> {
    LOGGER.log(Level.SEVERE, "Uncaught exception in TcpSlaveAgentListener " + t + ", attempting to reschedule thread", e);
    shutdown();
    TcpSlaveAgentListenerRescheduler.schedule(t, e);
  });
  LOGGER.log(Level.FINE, "TCP agent listener started on port {0}", getPort());
  start();
}

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

public static ServerSocketChannel createServerSocketChannel(SocketFactory factory,String service_name, InetAddress bind_addr,
                              int start_port, int end_port) throws Exception {
  int original_start_port=start_port;
  ServerSocketChannel ch=null;
  while(true) {
    try {
      if(ch != null)
        Util.close(ch);
      ch=factory.createServerSocketChannel(service_name);
      ch.bind(new InetSocketAddress(bind_addr, start_port), 50);
      return ch;
    }
    catch(SocketException bind_ex) {
      if(start_port == end_port)
        throw new BindException("No available port to bind to in range [" + original_start_port + " .. " + end_port + "]");
      if(bind_addr != null && !bind_addr.isLoopbackAddress()) {
        NetworkInterface nic=NetworkInterface.getByInetAddress(bind_addr);
        if(nic == null)
          throw new BindException("bind_addr " + bind_addr + " is not a valid interface: " + bind_ex);
      }
      start_port++;
    }
  }
}

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

/**
 * A convenience method to bind to a given address and report
 * better exceptions if the address is not a valid host.
 * @param socket the socket to bind
 * @param address the address to bind to
 * @param backlog the number of connections allowed in the queue
 * @throws BindException if the address can't be bound
 * @throws UnknownHostException if the address isn't a valid host name
 * @throws IOException other random errors from bind
 */
public static void bind(ServerSocket socket, InetSocketAddress address,
            int backlog) throws IOException {
 try {
  socket.bind(address, backlog);
 } catch (BindException e) {
  BindException bindException =
   new BindException("Problem binding to " + address + " : " +
     e.getMessage());
  bindException.initCause(e);
  throw bindException;
 } catch (SocketException e) {
  // If they try to bind to a different host's address, give a better
  // error message.
  if ("Unresolved address".equals(e.getMessage())) {
   throw new UnknownHostException("Invalid hostname for server: " +
                   address.getHostName());
  }
  throw e;
 }
}

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

public static void bind(FileDescriptor fd, InetAddress address, int port) throws SocketException {
  if (address instanceof Inet6Address && ((Inet6Address) address).getScopeId() == 0) {
    // Linux won't let you bind a link-local address without a scope id. Find one.
    NetworkInterface nif = NetworkInterface.getByInetAddress(address);
    if (nif == null) {
      throw new SocketException("Can't bind to a link-local address without a scope id: " + address);
    }
    try {
      address = Inet6Address.getByAddress(address.getHostName(), address.getAddress(), nif.getIndex());
    } catch (UnknownHostException ex) {
      throw new AssertionError(ex); // Can't happen.
    }
  }
  try {
    Libcore.os.bind(fd, address, port);
  } catch (ErrnoException errnoException) {
    throw new BindException(errnoException.getMessage(), errnoException);
  }
}

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

public ServerSocket createServerSocket(int nport, int backlog, InetAddress bindAddr,
  List<GatewayTransportFilter> transportFilters, int socketBufferSize) throws IOException {
 if (transportFilters.isEmpty()) {
  return createServerSocket(nport, backlog, bindAddr, socketBufferSize);
 } else {
  printConfig();
  ServerSocket result = new TransportFilterServerSocket(transportFilters);
  result.setReuseAddress(true);
  // Set the receive buffer size before binding the socket so
  // that large buffers will be allocated on accepted sockets (see
  // java.net.ServerSocket.setReceiverBufferSize javadocs)
  result.setReceiveBufferSize(socketBufferSize);
  try {
   result.bind(new InetSocketAddress(bindAddr, nport), backlog);
  } catch (BindException e) {
   BindException throwMe =
     new BindException(String.format("Failed to create server socket on %s[%s]",
       bindAddr, Integer.valueOf(nport)));
   throwMe.initCause(e);
   throw throwMe;
  }
  return result;
 }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void resolveMethodExceptionSubType() {
  IOException ioException = new FileNotFoundException();
  assertEquals("handleIOException", this.resolver.resolveMethod(ioException).getName());
  SocketException bindException = new BindException();
  assertEquals("handleSocketException", this.resolver.resolveMethod(bindException).getName());
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

throw new BindException("Could not find a free port in "+range);

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

checkOpen();
if (isBound()) {
  throw new BindException("Socket is already bound");

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * Create bind exception by wrapping the bind exception thrown.
 * @param listener
 * @param ex
 * @return
 */
private static BindException constructBindException(ServerConnector listener,
  BindException ex) {
 BindException be = new BindException("Port in use: "
   + listener.getHost() + ":" + listener.getPort());
 if (ex != null) {
  be.initCause(ex);
 }
 return be;
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void resolveMethodExceptionSubType() {
  ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class);
  IOException ioException = new FileNotFoundException();
  assertEquals("handleIOException", resolver.resolveMethod(ioException).getName());
  SocketException bindException = new BindException();
  assertEquals("handleSocketException", resolver.resolveMethod(bindException).getName());
}

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

checkOpenAndCreate(true);
if (isBound()) {
  throw new BindException("Socket is already bound");

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

} catch (BindException e) {
 BindException throwMe =
   new BindException(String.format("Failed to create server socket on %s[%s]",
     bindAddr == null ? InetAddress.getLocalHost() : bindAddr,
     String.valueOf(nport)));

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

} catch (BindException ex) {
 if (port == 0 || !findPort) {
  BindException be = new BindException("Port in use: "
    + listener.getHost() + ":" + listener.getPort());
  be.initCause(ex);

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

throw new BindException(lastVE.getMessage());

相关文章