Get the IP address of the Host Computer
in
Programming Questions
•
2 years ago
I've read the solution to 'Get the Host Name of the Computer' but it refers to an outside site you have to register to. I came up with the following code by converting the java code at
http://www.exampledepot.com/egs/java.net/Local.html but I'm still looking for a way to get the Host Name and Host IP as separate values in a single line or declaration without having to make the list and strings as here:
I haven't used the try method before. Does that work in Setup() or can it be called as a function from setup()? Can you explain try?
- /**
- * HostName: Get Host Name and Ip Address
- * PROCESSING EXAMPLE (UNBSD 2011) (BSD)
- * Click Display to show ip and name in console.
- */
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- public String HostAddress;
- public String HostName;
- void setup(){
- size(50,50);
- }
- void draw(){
- // println(HostAddress + ":"+HostName);
- }
- void mouseClicked(){
- try {
- InetAddress addr = InetAddress.getLocalHost();
- // Get IP Address
- byte[] ipAddr = addr.getAddress();
- // Extract Just the IP. Vanilla addr returns name and address separated by a '/' character
- String raw_addr = addr.toString();
- String[] list = split(raw_addr,'/');
- HostAddress = list[1];
- // Get hostname
- HostName = addr.getHostName();
- } catch (UnknownHostException e) {
- }
- println(HostAddress + ":"+HostName);
- }
I haven't used the try method before. Does that work in Setup() or can it be called as a function from setup()? Can you explain try?
1