JavaBeanBlog
                JavaBeanBlog
                BlogJava | 首頁 | 發新隨筆 | 發新文章 | 聯系 | 聚合 | 管理

                get IP and Mac with java


                 1import java.net.InetAddress;
                 2import java.net.NetworkInterface;
                 3import java.net.SocketException;
                 4import java.net.UnknownHostException;
                 5
                 6public class getAddress {
                 7    public static void main(String[] args) throws UnknownHostException {
                            
                            //get HOSTAddress
                 8
                        InetAddress thisIp = InetAddress.getLocalHost();
                 9        try {
                10            System.out.println("IP:" + thisIp.getHostAddress());
                11        }
                 catch (Exception e) {
                12            e.printStackTrace();
                13        }

                14
                15        try {
                                //get  MAC By IP
                17            NetworkInterface ni = NetworkInterface.getByInetAddress(thisIp);
                18            if (ni != null) {
                19                byte[] mac = ni.getHardwareAddress();
                20                if (mac != null) {
                21                    for (int k = 0; k < mac.length; k++) {
                22                        System.out.format("%02X%s", mac[k],
                23                                (k < mac.length - 1) ? "-" : "");
                24                    }

                25                }
                 else {
                26                    System.out.println("Address doesn't exist ");
                27                }

                28            }
                 else {
                29                System.out.println("address is not found.");
                30            }

                31        }
                 catch (SocketException e) {
                32            e.printStackTrace();
                33        }

                34    }

                35}

                36
                發表于 2012-05-22 17:52 javaBean 閱讀(181) 評論(0)  編輯  收藏
                 
                新用戶注冊  刷新評論列表  

                只有注冊用戶登錄后才能發表評論。


                網站導航:
                博客園   IT新聞   Chat2DB   C++博客   博問   管理
                 
                隨筆:0 文章:4 評論:0 引用:0
                <2025年6月>
                日一二三四五六
                25262728293031
                1234567
                891011121314
                15161718192021
                22232425262728
                293012345

                留言簿

                • 給我留言
                • 查看公開留言
                • 查看私人留言

                文章檔案

                • 2012年6月 (1)
                • 2012年5月 (3)

                搜索

                •  

                最新評論


                Powered by: 博客園
                模板提供:滬江博客
                Copyright ©2025 javaBean

                狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频
                主站蜘蛛池模板: 太仓市| 普格县| 自贡市| 麻栗坡县| 阿城市| 武清区| 海兴县| 澄迈县| 章丘市| 横山县| 博兴县| 镇坪县| 酒泉市| 长治市| 乌审旗| 江西省| 漠河县| 汕头市| 郯城县| 寿宁县| 东至县| 奈曼旗| 吴桥县| 吐鲁番市| 东乡族自治县| 临漳县| 朝阳区| 蒲江县| 通辽市| 贡觉县| 遂昌县| 榆林市| 黄冈市| 贵南县| 阜宁县| 临澧县| 喜德县| 岑巩县| 山东| 光泽县| 开江县|