java.net.Socket.getLocalAddress()方法的使用及代码示例

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

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

Socket.getLocalAddress介绍

[英]Returns the local IP address this socket is bound to, or InetAddress.ANY if the socket is unbound.
[中]返回此套接字绑定到的本地IP地址或InetAddress。如果套接字未绑定,则为任意。

代码示例

代码示例来源:origin: aws/aws-sdk-java

@Override
public InetAddress getLocalAddress() {
  return sock.getLocalAddress();
}

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

private String buildHostnameToReport() {
  InetAddress localAddress = socket.getLocalAddress();
  // we use local ip statically for privacy reasons, see https://github.com/k9mail/k-9/pull/3798
  if (localAddress instanceof Inet6Address) {
    return "[IPv6:::1]";
  } else {
    return "[127.0.0.1]";
  }
}

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

public InetAddress getLocalAddress() {
  if (this.socket != null) {
    return this.socket.getLocalAddress();
  } else {
    return null;
  }
}

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

public InetAddress getLocalAddress() {
  if (this.socket != null) {
    return this.socket.getLocalAddress();
  } else {
    return null;
  }
}

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

public String socketDescription() {
  Socket socket = transportLayer.socketChannel().socket();
  if (socket.getInetAddress() == null)
    return socket.getLocalAddress().toString();
  return socket.getInetAddress().toString();
}

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

private InetAddress serverAddress() {
  return transportLayer.socketChannel().socket().getLocalAddress();
}

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

/**
 * See {@link Socket#getLocalAddress()}. Calling this method does not trigger mode detection.
 */
@Override
public InetAddress getLocalAddress() {
  return getSocketAllowUnknownMode().getLocalAddress();
}

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

protected String getSockAddress() {
  StringBuilder sb=new StringBuilder();
  if(sock != null) {
    sb.append(sock.getLocalAddress().getHostAddress()).append(':').append(sock.getLocalPort());
    sb.append(" - ").append(sock.getInetAddress().getHostAddress()).append(':').append(sock.getPort());
  }
  return sb.toString();
}

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

InetAddress inetAddress = socket.getLocalAddress();

代码示例来源:origin: stackoverflow.com

// output on my machine: "192.168.1.102"
Socket s = new Socket("192.168.1.1", 80);
System.out.println(s.getLocalAddress().getHostAddress());
s.close();

// output on my machine: "127.0.1.1"
System.out.println(InetAddress.getLocalHost().getHostAddress());

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

public void go() throws IOException {
  final ServerSocket s = new ServerSocket(listenPort);
  final ExecutorService exe = Executors.newCachedThreadPool();
  while (true) {
    final Socket incoming = s.accept();
    ip = incoming.getLocalAddress().getHostAddress();
    System.out.println("New Client Connected from " + incoming.getInetAddress().getHostName() + "... ");
    exe.submit(new FtpLoop(incoming, this));
  }
}

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

public SocketAddress getLocalAddress() {
  final Socket socket = conduit.getSocketChannel().socket();
  return new InetSocketAddress(socket.getLocalAddress(), socket.getLocalPort());
}

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

/**
 * Returns the local address and port of this socket as a SocketAddress or
 * null if the socket is unbound. This is useful on multihomed
 * hosts.
 */
public SocketAddress getLocalSocketAddress() {
  if (!isBound()) {
    return null;
  }
  return new InetSocketAddress(getLocalAddress(), getLocalPort());
}

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

public static String getClientIp(String serviceUrl) {
    try {
      URL url = new URL(serviceUrl);
      int port = url.getPort();
      if (port == -1) {
        port = url.getDefaultPort();
      }
      try (Socket socket = new Socket(url.getHost(), port)) {
        return socket.getLocalAddress().getHostAddress();
      }
    } catch (Exception e) {
      return SystemUtil.getFirstLocalNonLoopbackIpAddress();
    }
  }
}

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

o.write("Jenkins-Session: " + Jenkins.SESSION_HASH + "\r\n");
o.write("Client: " + s.getInetAddress().getHostAddress() + "\r\n");
o.write("Server: " + s.getLocalAddress().getHostAddress() + "\r\n");
o.write("Remoting-Minimum-Version: " + RemotingVersionInfo.getMinimumSupportedVersion() + "\r\n");
o.flush();

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

private void port() throws IOException {
  write("PORT "
      + controlSocket.getLocalAddress().getHostAddress().replace('.',
          ',') + ',' + (dataPort >> 8) + ','
      + (dataPort & 255)
      + "\r\n");
  if (getReply() != FTP_OK) {
    throw new IOException("Unable to configure data port");
  }
}

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

private String id(SocketChannel channel) {
  return channel.socket().getLocalAddress().getHostAddress() + ":" + channel.socket().getLocalPort() + "-" +
      channel.socket().getInetAddress().getHostAddress() + ":" + channel.socket().getPort();
}

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

@Signature
public Memory getLocalAddress(Environment env, Memory... args) {
  return new StringMemory(socket.getLocalAddress().toString());
}

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

public String toString() {
  Socket tmp_sock=sock;
  if(tmp_sock == null)
    return "<null socket>";
  InetAddress local=tmp_sock.getLocalAddress(), remote=tmp_sock.getInetAddress();
  String local_str=local != null? Util.shortName(local) : "<null>";
  String remote_str=remote != null? Util.shortName(remote) : "<null>";
  return String.format("%s:%s --> %s:%s (%d secs old) [%s] [recv_buf=%d]",
             local_str, tmp_sock.getLocalPort(), remote_str, tmp_sock.getPort(),
             TimeUnit.SECONDS.convert(getTimestamp() - last_access, TimeUnit.NANOSECONDS),
             status(), receiver != null? receiver.bufferSize() : 0);
}

代码示例来源:origin: ethereum/ethereumj

public static String bindIp() {
    String bindIp;
      try (Socket s = new Socket("www.google.com", 80)) {
        bindIp = s.getLocalAddress().getHostAddress();
        System.out.printf("UDP local bound to: %s%n", bindIp);
      } catch (IOException e) {
        System.out.printf("Can't get bind IP. Fall back to 0.0.0.0: " + e);
        bindIp = "0.0.0.0";
      }
    return bindIp;
  }
}

相关文章

微信公众号

最新文章

更多