• 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);
                }
            }
        }
    }

  • 相关阅读:
    【网络文摘】一位36岁程序员的困惑
    【网络文摘】大龄程序员怎样渡过中年危机?
    【问题与解决】showModalDialog is not defined 的解决方案
    【ASP.NET 问题】ASP.NET 网站404页面返回200,或者302的解决办法
    IIS 网站 HTTP 转 HTTPS
    ECharts JS应用:图表页面实现
    【问题与解决】怎么删除TFS云端上的项目
    【问题与解决】Github 上传代码报错(error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version)
    【问题与解决】Mac OS通过 npm 安装 React Native 报错(checkPermissions Missing write access to /usr/local/lib/node_modules)
    【JavaScript 插件】图片展示插件 PhotoSwipe 初识
  • 原文地址:https://www.cnblogs.com/james1207/p/3285528.html
Copyright © 2020-2023  润新知