本文整理汇总了Java中java.net.InetAddress.getHostAddress方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.getHostAddress方法的具体用法?Java InetAddress.getHostAddress怎么用?Java InetAddress.getHostAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.getHostAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resolveAndShuffle
import java.net.InetAddress; //导入方法依赖的package包/类
private List<InetSocketAddress> resolveAndShuffle(Collection<InetSocketAddress> serverAddresses) {
List<InetSocketAddress> tmpList = new ArrayList<InetSocketAddress>(serverAddresses.size());
for (InetSocketAddress address : serverAddresses) {
try {
InetAddress ia = address.getAddress();
String addr = (ia != null) ? ia.getHostAddress() : address.getHostString();
InetAddress resolvedAddresses[] = InetAddress.getAllByName(addr);
for (InetAddress resolvedAddress : resolvedAddresses) {
InetAddress taddr = InetAddress.getByAddress(address.getHostString(), resolvedAddress.getAddress());
tmpList.add(new InetSocketAddress(taddr, address.getPort()));
}
} catch (UnknownHostException ex) {
LOG.warn("No IP address found for server: {}", address, ex);
}
}
Collections.shuffle(tmpList, sourceOfRandomness);
return tmpList;
}
示例2: checkAcceptPermission
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Verify that the given AccessControlContext has permission to
* accept this connection.
*/
void checkAcceptPermission(SecurityManager sm,
AccessControlContext acc)
{
/*
* Note: no need to synchronize on cache-related fields, since this
* method only gets called from the ConnectionHandler's thread.
*/
if (sm != cacheSecurityManager) {
okContext = null;
authCache = new WeakHashMap<AccessControlContext,
Reference<AccessControlContext>>();
cacheSecurityManager = sm;
}
if (acc.equals(okContext) || authCache.containsKey(acc)) {
return;
}
InetAddress addr = socket.getInetAddress();
String host = (addr != null) ? addr.getHostAddress() : "*";
sm.checkAccept(host, socket.getPort());
authCache.put(acc, new SoftReference<AccessControlContext>(acc));
okContext = acc;
}
示例3: ConnectionBean
import java.net.InetAddress; //导入方法依赖的package包/类
public ConnectionBean(ServerCnxn connection,ZooKeeperServer zk){
this.connection = connection;
this.stats = connection;
this.zk = zk;
InetSocketAddress sockAddr = connection.getRemoteSocketAddress();
if (sockAddr == null) {
remoteIP = "Unknown";
} else {
InetAddress addr = sockAddr.getAddress();
if (addr instanceof Inet6Address) {
remoteIP = ObjectName.quote(addr.getHostAddress());
} else {
remoteIP = addr.getHostAddress();
}
}
sessionId = connection.getSessionId();
}
示例4: getIpAddress
import java.net.InetAddress; //导入方法依赖的package包/类
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
示例5: getRemoteIPv4Address
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getRemoteIPv4Address() {
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ni : interfaces) {
List<InetAddress> addresses = Collections.list(ni.getInetAddresses());
for (InetAddress addr : addresses) {
if (!addr.isLoopbackAddress()) {
String strAddr = addr.getHostAddress();
if(!strAddr.contains(":")) {
return strAddr.substring(0, strAddr.lastIndexOf(".") + 1) + "255";
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return "255.255.255.255";
}
示例6: getRemoteIp
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getRemoteIp(Channel channel) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) channel.remoteAddress();
if (inetSocketAddress == null) {
return "";
}
final InetAddress inetAddr = inetSocketAddress.getAddress();
return (inetAddr != null ? inetAddr.getHostAddress() : inetSocketAddress.getHostName());
}
示例7: main
import java.net.InetAddress; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
InetAddress localAddress = InetAddress.getLocalHost();
String[] hostlist = new String[] {
localAddress.getHostAddress(), localAddress.getHostName() };
for (int i = 0; i < hostlist.length; i++) {
System.setProperty("java.rmi.server.hostname", hostlist[i]);
Remote impl = new ChangeHostName();
System.err.println("\ncreated impl extending URO: " + impl);
Receiver stub = (Receiver) RemoteObject.toStub(impl);
System.err.println("stub for impl: " + stub);
System.err.println("invoking method on stub");
stub.receive(stub);
UnicastRemoteObject.unexportObject(impl, true);
System.err.println("unexported impl");
if (stub.toString().indexOf(hostlist[i]) >= 0) {
System.err.println("stub's ref contains hostname: " +
hostlist[i]);
} else {
throw new RuntimeException(
"TEST FAILED: stub's ref doesn't contain hostname: " +
hostlist[i]);
}
}
System.err.println("TEST PASSED");
}
示例8: isValidHostAddress
import java.net.InetAddress; //导入方法依赖的package包/类
private static boolean isValidHostAddress(InetAddress address) {
if (address == null || address.isLoopbackAddress()) {
return false;
}
String name = address.getHostAddress();
return (name != null && !EMPTY_IP.equals(name) && !LOCALHOST_IP.equals(name) && IP_PATTERN.matcher(name)
.matches());
}
示例9: updateIP
import java.net.InetAddress; //导入方法依赖的package包/类
protected void
updateIP()
{
InetAddress latest_ip = buddy.getAdjustedIP();
if ( latest_ip != null ){
current_ip = latest_ip.getHostAddress();
log( "IP set to " + current_ip );
}
}
示例10: getLocalAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Returns the IP address that this HTTP server instance is bound to.
* @return String representation of the IP address or <code>null</code> if not running
*/
public String getLocalAddress() {
InetAddress address = listener.getInetAddress();
// Ugly work-around for older JDKs
byte[] octets = address.getAddress();
if ((octets[0] == 0)
&& (octets[1] == 0)
&& (octets[2] == 0)
&& (octets[3] == 0)) {
return "localhost";
} else {
return address.getHostAddress();
}
}
示例11: addServer
import java.net.InetAddress; //导入方法依赖的package包/类
public synchronized void addServer(String pingResponse, InetAddress ipAddress)
{
String s = ThreadLanServerPing.getMotdFromPingResponse(pingResponse);
String s1 = ThreadLanServerPing.getAdFromPingResponse(pingResponse);
if (s1 != null)
{
s1 = ipAddress.getHostAddress() + ":" + s1;
boolean flag = false;
for (LanServerInfo lanserverinfo : this.listOfLanServers)
{
if (lanserverinfo.getServerIpPort().equals(s1))
{
lanserverinfo.updateLastSeen();
flag = true;
break;
}
}
if (!flag)
{
this.listOfLanServers.add(new LanServerInfo(s, s1));
this.wasUpdated = true;
}
}
}
示例12: toString
import java.net.InetAddress; //导入方法依赖的package包/类
@Override
public String toString()
{
final InetAddress address = getConnection().getInetAddress();
if (getState() == LoginClientState.AUTHED_LOGIN)
{
return "[" + getAccount() + " (" + (address == null ? "disconnected" : address.getHostAddress()) + ")]";
}
return "[" + (address == null ? "disconnected" : address.getHostAddress()) + "]";
}
示例13: ClientHandler
import java.net.InetAddress; //导入方法依赖的package包/类
public ClientHandler(Socket socket) {
this.socket = socket;
/*
* 通过Socket获取远程计算机地址信息 对于服务端而言,远程计算机就是客户端了
*/
InetAddress address = socket.getInetAddress();
// 获取IP地址
host = address.getHostAddress();
}
示例14: getIpAddr
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* 获取计算机的ip地址
* @return
*/
public static String getIpAddr() {
String ipAddress = null;
try {
InetAddress address = InetAddress.getLocalHost();
ipAddress = address.getHostAddress();
} catch (UnknownHostException e) {
logger.error(e.getMessage(), e);
}
return ipAddress;
}
示例15: doReverseLookup
import java.net.InetAddress; //导入方法依赖的package包/类
static void doReverseLookup(NetworkInterface netIf) throws SocketException, UnknownHostException {
for (NetworkInterface subIf : Collections.list(netIf.getSubInterfaces())) {
Enumeration<InetAddress> subInetAddresses = subIf.getInetAddresses();
while (subInetAddresses != null && subInetAddresses.hasMoreElements()) {
InetAddress inetAddress = subInetAddresses.nextElement();
String reversalString = inetAddress.getHostAddress();
//should not throw UHE in case of virtual sub interface
InetAddress.getByName(reversalString);
}
}
}