inet address example(socket)
2013-08-27 18:03
316 查看
package com.opensource.socket;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class InetAddressExample
{
/**
* @param args
*/
public static void main(String[] args)
{
try
{
Enumeration<NetworkInterface> interfaceList = NetworkInterface.getNetworkInterfaces();
if (interfaceList == null)
{
System.out.println("no interfaces found.");
}
else
{
while (interfaceList.hasMoreElements())
{
NetworkInterface iface = interfaceList.nextElement();
System.out.println("Interface:" + iface.getName());
Enumeration<InetAddress> addrList = iface.getInetAddresses();
if (!addrList.hasMoreElements())
{
System.out.println("no address for this interface.");
}
while (addrList.hasMoreElements())
{
InetAddress address = addrList.nextElement();
System.out.print("\tAddress "
+ ((address instanceof Inet4Address ? "(v4)" : (address instanceof Inet6Address ? "(v6)"
: "(?)"))));
System.out.println(": " + address.getHostAddress());
}
}
}
}
catch (SocketException e)
{
System.out.println("SocketException:" + e);
}
for (String host : args)
{
try
{
System.out.println(host + ":");
InetAddress[] addressList = InetAddress.getAllByName(host);
for (InetAddress address : addressList)
{
System.out.println("\t" + address.getHostName() + "/" + address.getHostAddress());
}
}
catch (UnknownHostException e)
{
System.out.println("UnknownHostException:" + e);
}
}
}
}
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class InetAddressExample
{
/**
* @param args
*/
public static void main(String[] args)
{
try
{
Enumeration<NetworkInterface> interfaceList = NetworkInterface.getNetworkInterfaces();
if (interfaceList == null)
{
System.out.println("no interfaces found.");
}
else
{
while (interfaceList.hasMoreElements())
{
NetworkInterface iface = interfaceList.nextElement();
System.out.println("Interface:" + iface.getName());
Enumeration<InetAddress> addrList = iface.getInetAddresses();
if (!addrList.hasMoreElements())
{
System.out.println("no address for this interface.");
}
while (addrList.hasMoreElements())
{
InetAddress address = addrList.nextElement();
System.out.print("\tAddress "
+ ((address instanceof Inet4Address ? "(v4)" : (address instanceof Inet6Address ? "(v6)"
: "(?)"))));
System.out.println(": " + address.getHostAddress());
}
}
}
}
catch (SocketException e)
{
System.out.println("SocketException:" + e);
}
for (String host : args)
{
try
{
System.out.println(host + ":");
InetAddress[] addressList = InetAddress.getAllByName(host);
for (InetAddress address : addressList)
{
System.out.println("\t" + address.getHostName() + "/" + address.getHostAddress());
}
}
catch (UnknownHostException e)
{
System.out.println("UnknownHostException:" + e);
}
}
}
}
相关文章推荐
- inet address example(socket)
- InetAddressSocket使用总结
- Java学习-InetAddress和InetSocketAddress
- java点滴之InetAddress类和InetSocketAddress的使用
- InetAddress和InetSocketAddress
- 网路编程(概述、InetAddress、Socket服务、URL)
- InetAddress类和InetSocketAddress的使用
- Java中InetAddress和InetSocketAddress的区别
- 网络编程_InetAddress_InetSocketAddress_URL_爬虫原理JAVA185-186
- java:网络编程(InetAddress,InetSocketAddress,URL,TCP(Socket与SeverSocket),TCP与UDP的区别)
- Java.UDP-TCP-DatagramSocket-DatagramPacket-InetAddress-ServerSocket-Socket
- [网络编程]——InetAddress_InetSocketAddress
- java之InetAddress类和InetSocketAddress的使用
- SocketAddress与 InetSocketAddress
- java点滴之InetAddress类和InetSocketAddress的使用
- Java笔记(9)-网络编程、URL、InetAddress、套接字、Socket、ServerSocket、多线程、UDP、广播数据包、远程调用
- 1.InetAddress和InetSocketAddress
- host、InetAddress和InetSocketAddress的区别
- InetAddress类和InetSocketAddress
- 01网络编程_网络编程_InetAddress_InetSocketAddress