{"id":835,"date":"2010-03-29T09:59:28","date_gmt":"2010-03-29T08:59:28","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=835"},"modified":"2010-03-29T09:59:28","modified_gmt":"2010-03-29T08:59:28","slug":"internet-address-ip","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/29\/internet-address-ip\/","title":{"rendered":"Internet Address IP"},"content":{"rendered":"<p>With Java you can use the java.net package to find out details of your PC address or somewhere else (like <a href=\"http:\/\/www.google.com\">www.google.com<\/a>) with using the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/net\/InetAddress.html\">InetAddress<\/a> class.  The InetAddress is a implementation of the <a href=\"http:\/\/en.wikipedia.org\/wiki\/IP_address\">Internet Protocol<\/a> basically.<\/p>\n<p>To gain the local pc address you can use<\/p>\n<pre lang=\"java\">\r\nInetAddress theHost = InetAddress.getLocalHost();\r\nSystem.out.println(\"Local host address: \"+theHost);\r\n<\/pre>\n<p>which would get the local PC address and display it to the screen, for me it would something like <\/p>\n<pre lang=\"bash\">\r\nLocal host address: genux-laptop\/127.0.1.1\r\n<\/pre>\n<p>you can get the individual parts of the above address, e.g. genux-laptop and 127.0.0.1 <\/p>\n<pre lang=\"java\">\r\nSystem.out.println(\"Local host name : \" +theHost.getHostName());\r\nSystem.out.println(\"Local address : \"+theHost.getHostAddress());\r\n<\/pre>\n<p>and the output would be <\/p>\n<pre lang=\"bash\">\r\nLocal host name :  genux-laptop\r\nLocal address : 127.0.1.1\r\n<\/pre>\n<p>you can also use the InetAddress to obtain remote addresses for example www.google.com so to pull details back from google you could use the getByName function that is within the InetAddress class as.<\/p>\n<pre lang=\"java\">\r\nSystem.out.println(\"Host address of google.com : \"+ InetAddress.getByName(\"www.google.com\"));\r\n<\/pre>\n<p>Here is a full code that does the above in full<\/p>\n<pre lang=\"java\">\r\nimport java.io.*;\r\nimport java.net.*;\r\n\r\npublic class address {\r\n\t\tpublic static void main(String[] args)\r\n\t\t{\r\n\t\t\t\/\/ a inetaddress class holds details of internet protocol (IP) address\r\n\t\t\t\r\n\t\t\tInetAddress theHost = null;\r\n\t\t\t\/\/ you can get the local host (your PC) addresss\r\n\t\t\ttry {\r\n\t\t\t\ttheHost = InetAddress.getLocalHost();\r\n\t\t\t\tSystem.out.println(\"Local host address: \"+theHost);\r\n\t\t\t}\r\n\t\t\tcatch (UnknownHostException unknow)\r\n\t\t\t{\r\n\t\t\t\tSystem.out.println(\"Error : \"+ unknow.toString());\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t\t\/\/ if there is no local host then, oppss!!.. you should really have a local host name\r\n\t\t\t\/\/ also you get can the local host name and address individual \r\n\t\t\tString theHostName = theHost.getHostName();\r\n\t\t\tString theHostAddress = theHost.getHostAddress();\r\n\t\t\t\r\n\t\t\tSystem.out.println(\"The host name \"+ theHostName);\r\n\t\t\tSystem.out.println(\"The host address \" + theHostAddress);\r\n\t\t\t\r\n\t\t\ttry {\r\n\t\t\t\t\/\/ you can a reverse look up as such by passing in the local name to get the IP address\r\n\t\t\t\tSystem.out.println(\"Host address by host name : \"+ InetAddress.getByName(theHostName));\r\n\t\t\t\t\r\n\t\t\t\t\/\/ if you wanted to get a google host name then just pass in www.google.com\r\n\t\t\t\tSystem.out.println(\"Host address of google.com : \"+ InetAddress.getByName(\"www.google.com\"));\r\n\t\t\t}\r\n\t\t\tcatch (UnknownHostException unknow)\r\n\t\t\t{\r\n\t\t\t\tSystem.out.println(\"Error : \"+ unknow.toString());\r\n\t\t\t}\r\n\t\t}\r\n}\r\n<\/pre>\n<p>and the output would be <\/p>\n<pre lang=\"bash\">\r\nLocal host address: genux-laptop\/127.0.1.1\r\nThe host name genux-laptop\r\nThe host address 127.0.1.1\r\nHost address by host name : genux-laptop\/127.0.1.1\r\nHost address of google.com : www.google.com\/216.239.59.105\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>With Java you can use the java.net package to find out details of your PC address or somewhere else (like www.google.com) with using the InetAddress class. The InetAddress is a implementation of the Internet Protocol basically. To gain the local pc address you can use InetAddress theHost = InetAddress.getLocalHost(); System.out.println(&#8220;Local host address: &#8220;+theHost); which would &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/29\/internet-address-ip\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Internet Address IP<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[157],"class_list":["post-835","post","type-post","status-publish","format-standard","hentry","category-java","tag-inetaddress"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=835"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/835\/revisions"}],"predecessor-version":[{"id":836,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/835\/revisions\/836"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}