Java to VB.Net Conversion [Small Snippet] - java

I have this snippet, it's in Java:
final InetAddress address = InetAddress.getLocalHost();
final NetworkInterface ni = NetworkInterface.getByInetAddress(address);
key = new String(ni.getHardwareAddress());
Example of key output: ▲╔UiÎ
What is the equivalent in VB.Net? I understand the first line gets Local Host, what about the rest? Thanks in advance.

This iterates over all local interfaces:
Dim theNetworkInterfaces() as System.Net.NetworkInformation.NetworkInterface = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
for each curInterface as System.Net.NetworkInformation.NetworkInterface in theNetworkInterfaces
MessageBox.Show(curInterface.GetPhysicalAddress().ToString())
The physical address is what you want.
The line
final NetworkInterface ni = NetworkInterface.getByInetAddress(address);
just grabs the specific network interface by the inetaddress
Say you store your localhost address in a variable called localIa and then you can use it:
NetworkInterface ni = NetworkInterface.getByInetAddress(localIa)
ni.GetPhysicalAddress().ToString()

Related

Getting the ip address of eth0 interface in java only return IPv6 address and not IPv4

I wrote the following code to get the IPv4 address of the eth0 interface I am using on a machine. However the code only finds fe80:x:x:x:xxx:xxxx:xxxx:xxxx which is not returned since I am looking for the IPv4 address.
Here is the code.
interfaceName = "eth0";
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName);
Enumeration<InetAddress> inetAddress = networkInterface.getInetAddresses();
InetAddress currentAddress;
currentAddress = inetAddress.nextElement();
while(inetAddress.hasMoreElements())
{
System.out.println(currentAddress);
if(currentAddress instanceof Inet4Address && !currentAddress.isLoopbackAddress())
{
ip = currentAddress.toString();
break;
}
currentAddress = inetAddress.nextElement();
}
It was messing with the logic where it gets the next element. I had the inetAddress next element being gotten before the while compare was ran. Thus making there be no more elements.
The following code has then fixed the logic
interfaceName = "eth0";
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName);
Enumeration<InetAddress> inetAddress = networkInterface.getInetAddresses();
InetAddress currentAddress;
currentAddress = inetAddress.nextElement();
while(inetAddress.hasMoreElements())
{
currentAddress = inetAddress.nextElement();
if(currentAddress instanceof Inet4Address && !currentAddress.isLoopbackAddress())
{
ip = currentAddress.toString();
break;
}
}

Get non-local IP Address Java [duplicate]

This question already has answers here:
Getting the IP address of the current machine using Java
(19 answers)
Closed 9 years ago.
I have a program which requires it to know it's IP Address. However when I use
InetAddress current_addr = addresses.nextElement();
It returns
127.0.1.1
Which isn't very helpful. How can I get my non-local IP Address from java?
What do you get when you use:
InetAddress IP = InetAddress.getLocalHost();
String ipAddress = IP.getHostAddress();
it should ideally give you the ip address if you don't have more than one network interfaces.
I tested it locally and it gives me proper ip address of my machine i.e.
192.168.2.10
If you have more than one network interface then you can try to use the NetworkInterface class, here is the sample:
Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
for (; n.hasMoreElements();)
{
NetworkInterface e = n.nextElement();
System.out.println("Interface: " + e.getName());
Enumeration<InetAddress> a = e.getInetAddresses();
for (; a.hasMoreElements();)
{
InetAddress addr = a.nextElement();
System.out.println(" " + addr.getHostAddress());
}
}
Source taken from a related post: java InetAddress.getLocalHost(); returns 127.0.0.1 ... how to get REAL IP?

How to get the IP address for remote host using Java

I need to get the IP address for remote hosts. I tried the following and works fine:
socket = factory.createSocket(hostName, port);
InetAddress remoteIP = socket.getInetAddress();
String[] remoteIPOnly = remoteIP.toString().split("\\/");
System.out.println("Remote IP is: "+remoteIPOnly[1]);
But, I need a way where I don't have to specify a port number. I.e, I need the IP for a remote host despite the port number. Is this possible ? Is it possible to get the IP without creating socket from the first place ?
Try this:
InetAddress inetAddress = InetAddress.getByName("www.google.com");
byte[] raw = inetAddress.getAddress();
The byte array now contains the IP addresses bytes.
Use getHostAddress() as below:
InetAddress inetAddress = InetAddress.getByName("www.google.com");
String ipAddress = inetAddress.getHostAddress();
System.out.println(ipAddress );//prints 66.152.109.61

Converting string to IP Address in java(android)

When i convert string ("192.168.0.105") to InetAddress in java (android). I am getting "/192.168.0.105". An extra "/" is coming in InetAddress, which causes the socket not to be created.
How do i get rid of "/".
Regards,
Syed Mustehsan Ikram
You can use getHostAddress() method of InetAddress to get host address without /.
And if you are using InetSocketAddress then use getAddress().getHostAddress() to get host ip without /.
InetAddress inetAddress = InetAddress.getByName("192.168.0.105");
System.out.println(inetAddress.getHostAddress());
InetSocketAddress address = new InetSocketAddress("192.168.0.105", 5555);
System.out.println(address.getAddress().getHostAddress());
myString = myString.replace("/", "");

Creating InetAddress object in Java

I am trying to convert an address specified by an IP number or a name, both in String (i.e. localhost or 127.0.0.1), into an InetAdress object. There's no constructor but rather static methods that return an InetAddress. So if I get a host name it's not a problem, but what if I get the IP number? There's one method that gets byte[] but I'm not sure how that can help me. All other methods gets the host name.
InetAddress API documentation
You should be able to use getByName or getByAddress.
The host name can either be a machine
name, such as "java.sun.com", or a
textual representation of its IP
address
InetAddress addr = InetAddress.getByName("127.0.0.1");
The method that takes a byte array can be used like this:
byte[] ipAddr = new byte[]{127, 0, 0, 1};
InetAddress addr = InetAddress.getByAddress(ipAddr);
From the API for InetAddress
The host name can either be a machine
name, such as "java.sun.com", or a
textual representation of its IP
address. If a literal IP address is
supplied, only the validity of the
address format is checked.
ip = InetAddress.getByAddress(new byte[] {
(byte)192, (byte)168, (byte)0, (byte)102}
);
InetAddress.getByName also works for ip address.
From the JavaDoc
The host name can either be a machine
name, such as "java.sun.com", or a
textual representation of its IP
address. If a literal IP address is
supplied, only the validity of the
address format is checked.
The api is fairly easy to use.
// Lookup the dns, if the ip exists.
if (!ip.isEmpty()) {
InetAddress inetAddress = InetAddress.getByName(ip);
dns = inetAddress.getCanonicalHostName();
}
InetAddress class can be used to store IP addresses in IPv4 as well as IPv6 formats. You can store the IP address to the object using either InetAddress.getByName() or InetAddress.getByAddress() methods.
In the following code snippet, I am using InetAddress.getByName() method to store IPv4 and IPv6 addresses.
InetAddress IPv4 = InetAddress.getByName("127.0.0.1");
InetAddress IPv6 = InetAddress.getByName("2001:db8:3333:4444:5555:6666:1.2.3.4");
You can also use InetAddress.getByAddress() to create object by providing the byte array.
InetAddress addr = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
Furthermore, you can use InetAddress.getLoopbackAddress() to get the local address and InetAddress.getLocalHost() to get the address registered with the machine name.
InetAddress loopback = InetAddress.getLoopbackAddress(); // output: localhost/127.0.0.1
InetAddress local = InetAddress.getLocalHost(); // output: <machine-name>/<ip address on network>
Note- make sure to surround your code by try/catch because InetAddress methods return java.net.UnknownHostException
This is a project for getting IP address of any website , it's usefull and so easy to make.
import java.net.InetAddress;
import java.net.UnkownHostExceptiin;
public class Main{
public static void main(String[]args){
try{
InetAddress addr = InetAddresd.getByName("www.yahoo.com");
System.out.println(addr.getHostAddress());
}catch(UnknownHostException e){
e.printStrackTrace();
}
}
}

Categories

Resources