zoukankan      html  css  js  c++  java
  • inet address example(socket)

    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(" Address "
                                + ((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(" " + address.getHostName() + "/" + address.getHostAddress());
                    }
                }
                catch (UnknownHostException e)
                {
                    System.out.println("UnknownHostException:" + e);
                }
            }
        }
    }

  • 相关阅读:
    BZOJ 3529 [Sdoi2014]数表
    bzoj 3195 [Jxoi2012]奇怪的道路
    BZOJ 4720 [Noip2016]换教室
    BZOJ 2160 拉拉队排练
    BZOJ 1031 [JSOI2007]字符加密Cipher 后缀数组教程
    BZOJ 1002 [FJOI2007]轮状病毒
    欧拉定理、拓展欧拉定理及其应用(欧拉降幂法)
    算术基本定理解析及其应用
    The 15th Zhejiang Provincial Collegiate Programming Contest(部分题解)
    网络流解析及其应用
  • 原文地址:https://www.cnblogs.com/james1207/p/3285528.html
Copyright © 2011-2022 走看看