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

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

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

ServerSocket.getInetAddress介绍

[英]Gets the local IP address of this server socket or null if the socket is unbound. This is useful for multihomed hosts.
[中]获取此服务器套接字的本地IP地址,如果套接字未绑定,则为null。这对于多宿主主机很有用。

代码示例

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

public InetAddress getBindAddress() {
  return socket.getInetAddress();
}

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

public InetAddress getServerInetAddr() {
 return this.serverSock.getInetAddress();
}

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

/** This method will close the socket upon first initialization. */
protected InetSocketAddress getWebAddressFromBindSocket() throws IOException {
 Preconditions.checkNotNull(mWebBindSocket, "mWebBindSocket");
 InetSocketAddress socketAddr = new InetSocketAddress(mWebBindSocket.getInetAddress(),
   mWebBindSocket.getLocalPort());
 mWebBindSocket.close();
 return socketAddr;
}

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

/**
 * Gets the local socket address of this server socket or {@code null} if
 * the socket is unbound. This is useful on multihomed hosts.
 *
 * @return the local socket address and port this socket is bound to.
 */
public SocketAddress getLocalSocketAddress() {
  if (!isBound()) {
    return null;
  }
  return new InetSocketAddress(getInetAddress(), getLocalPort());
}

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

public void run() {
 LOG.info("starting "+channel.socket().getInetAddress());
 try {
  while (true) {
   try {
    new Connection(channel.accept());
   } catch (ClosedChannelException e) {
    return;
   } catch (IOException e) {
    LOG.warn("unexpected error", e);
    throw new RuntimeException(e);
   }
  }
 } finally {
  LOG.info("stopping "+channel.socket().getInetAddress());
  try {
   channel.close();
  } catch (IOException e) {
  }
 }
}

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

/**
 * Returns a textual representation of this server socket including the
 * address, port and the state. The port field is set to {@code 0} if there
 * is no connection to the server socket.
 *
 * @return the textual socket representation.
 */
@Override
public String toString() {
  StringBuilder result = new StringBuilder(64);
  result.append("ServerSocket[");
  if (!isBound()) {
    return result.append("unbound]").toString();
  }
  return result.append("addr=")
      .append(getInetAddress().getHostName()).append("/")
      .append(getInetAddress().getHostAddress()).append(
          ",port=0,localport=")
      .append(getLocalPort()).append("]")
      .toString();
}

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

/**
 * @param socket
 * @param bindAddress
 * @return real hostName
 * @throws UnknownHostException
 */
protected String resolveHostName(ServerSocket socket, InetAddress bindAddress) throws UnknownHostException {
  String result = null;
  if (socket.isBound()) {
    if (socket.getInetAddress().isAnyLocalAddress()) {
      // make it more human readable and useful, an alternative to 0.0.0.0
      result = InetAddressUtil.getLocalHostName();
    } else {
      result = socket.getInetAddress().getCanonicalHostName();
    }
  } else {
    result = bindAddress.getCanonicalHostName();
  }
  return result;
}

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

protected void stopServing() throws Exception {
 if (isServing()) {
  LOG.info("Stopping RPC server on {} @ {}", this, mRpcBindSocket.getInetAddress());
  if (!mGrpcServer.shutdown()) {
   LOG.warn("RPC Server shutdown timed out.");
  }
 }
 if (mWebServer != null) {
  mWebServer.stop();
  mWebServer = null;
 }
}

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

@Override
public ServerSocket createServerSocket(int port) throws IOException {
 ServerSocket sock = null;
 if ("".equals(bindAddress)) {
  sock = socketCreator.createServerSocket(port, this.backlog);
 } else {
  sock = socketCreator.createServerSocket(port, this.backlog,
    InetAddressUtil.toInetAddress(this.bindAddress));
 }
 if (logger.isDebugEnabled()) {
  logger.debug(
    "MX4JServerSocketFactory RMIServerSocketFactory, INetAddress {}, LocalPort {}, LocalSocketAddress {}",
    sock.getInetAddress(), sock.getLocalPort(), sock.getLocalSocketAddress());
 }
 return sock;
}

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

private ExecutorService initializeHandshakerThreadPool() throws IOException {
 String threadName =
   "Handshaker " + serverSock.getInetAddress() + ":" + this.localPort + " Thread ";
 try {
  logger.warn("Handshaker max Pool size: " + HANDSHAKE_POOL_SIZE);
  return LoggingExecutors.newThreadPoolWithSynchronousFeedThatHandlesRejection(threadName,
    thread -> getStats().incAcceptThreadsCreated(), null, 1, HANDSHAKE_POOL_SIZE, 60);
 } catch (IllegalArgumentException poolInitException) {
  this.stats.close();
  this.serverSock.close();
  this.pool.shutdown();
  throw poolInitException;
 }
}

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

/**
 * binds the server socket and gets threads going
 */
private void startAcceptor() throws ConnectionException {
 int localPort;
 int p = this.port;
 createServerSocket();
 try {
  localPort = socket.getLocalPort();
  id = new InetSocketAddress(socket.getInetAddress(), localPort);
  stopped = false;
  thread = new LoggingThread("P2P Listener Thread " + id, this);
  try {
   thread.setPriority(Thread.MAX_PRIORITY);
  } catch (Exception e) {
   logger.info("unable to set listener priority: {}", e.getMessage());
  }
  if (!Boolean.getBoolean("p2p.test.inhibitAcceptor")) {
   thread.start();
  } else {
   logger.fatal(
     "p2p.test.inhibitAcceptor was found to be set, inhibiting incoming tcp/ip connections");
   socket.close();
  }
 } catch (IOException io) {
  String s = "While creating ServerSocket on port " + p;
  throw new ConnectionException(s, io);
 }
 this.port = localPort;
}

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

private void initializeServerSocket() throws IOException {
 if (srv_sock == null || srv_sock.isClosed()) {
  if (bind_address == null) {
   srv_sock = getSocketCreator().createServerSocket(port, BACKLOG);
   bind_address = srv_sock.getInetAddress();
  } else {
   srv_sock = getSocketCreator().createServerSocket(port, BACKLOG, bind_address);
  }
  // GEODE-4176 - set the port from a wild-card bind so that handlers know the correct value
  if (this.port <= 0) {
   this.port = srv_sock.getLocalPort();
  }
  if (log.isInfoEnabled()) {
   log.info("Locator was created at " + new Date());
   log.info("Listening on port " + getPort() + " bound on address " + bind_address);
  }
  srv_sock.setReuseAddress(true);
 }
}

代码示例来源:origin: igniterealtime/Smack

/**
 * Initialize fields used in the tests.
 *
 * @throws Exception should not happen
 */
@Before
public void setup() throws Exception {
  // create SOCKS5 proxy server socket
  serverSocket = NetworkUtil.getSocketOnLoopback();
  serverAddress = serverSocket.getInetAddress().getHostAddress();
  serverPort = serverSocket.getLocalPort();
}

代码示例来源:origin: square/okio

peer.start();
Socket socket = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);

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

new LoggingThread("Cache Server Acceptor " + this.serverSock.getInetAddress() + ":"
    + this.localPort + " local port: " + this.serverSock.getLocalPort(), false, this);
this.acceptorId = thread.getId();
   new LoggingThread("Cache Server Selector " + this.serverSock.getInetAddress() + ":"
     + this.localPort + " local port: " + this.serverSock.getLocalPort(),
     false,

代码示例来源:origin: k9mail/k-9

@Override
public void run() {
  String hostAddress = serverSocket.getInetAddress().getHostAddress();
  int port = serverSocket.getLocalPort();
  logger.log("Listening on %s:%d", hostAddress, port);

代码示例来源:origin: k9mail/k-9

@Override
public void run() {
  String hostAddress = serverSocket.getInetAddress().getHostAddress();
  int port = serverSocket.getLocalPort();
  logger.log("Listening on %s:%d", hostAddress, port);

代码示例来源:origin: k9mail/k-9

@Override
public void run() {
  String hostAddress = serverSocket.getInetAddress().getHostAddress();
  int port = serverSocket.getLocalPort();
  logger.log("Listening on %s:%d", hostAddress, port);

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

ListenSocketAddress unContestedAddress = new ListenSocketAddress( httpsSocket.getInetAddress().getHostName(), 0 );
ListenSocketAddress httpsAddress = new ListenSocketAddress( httpsSocket.getInetAddress().getHostName(), httpsSocket.getLocalPort() );
AssertableLogProvider logProvider = new AssertableLogProvider();
CommunityNeoServer server = CommunityServerBuilder.server( logProvider )

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

@Test
public void shouldComplainIfServerPortIsAlreadyTaken() throws IOException
{
  try ( ServerSocket socket = new ServerSocket( 0, 0, InetAddress.getLocalHost() ) )
  {
    ListenSocketAddress contestedAddress = new ListenSocketAddress( socket.getInetAddress().getHostName(), socket.getLocalPort() );
    AssertableLogProvider logProvider = new AssertableLogProvider();
    CommunityNeoServer server = CommunityServerBuilder.server( logProvider )
        .onAddress( contestedAddress )
        .usingDataDir( folder.directory( name.getMethodName() ).getAbsolutePath() )
        .build();
    try
    {
      server.start();
      fail( "Should have reported failure to start" );
    }
    catch ( ServerStartupException e )
    {
      assertThat( e.getMessage(), containsString( "Starting Neo4j failed" ) );
    }
    logProvider.assertAtLeastOnce(
        AssertableLogProvider.inLog( containsString( "CommunityNeoServer" ) ).error(
            "Failed to start Neo4j on %s: %s",
            contestedAddress,
            format( "Address %s is already in use, cannot bind to it.", contestedAddress )
        )
    );
    server.stop();
  }
}

相关文章