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 閱讀(182) 評論(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_国产精品亚洲第五区在线_日本免费网站视频
              主站蜘蛛池模板: 土默特右旗| 文昌市| 灌云县| 玉门市| 长武县| 腾冲县| 依安县| 永德县| 宜兰市| 吉林省| 炉霍县| 镇沅| 勃利县| 新郑市| 汉源县| 邻水| 炉霍县| 东莞市| 宜州市| 佛冈县| 临颍县| 田东县| 东山县| 无极县| 曲阳县| 延川县| 镇平县| 梅河口市| 台北县| 永仁县| 黔江区| 长垣县| 玛纳斯县| 大关县| 宁城县| 开封市| 天峻县| 四子王旗| 左贡县| 通榆县| 庆云县|