Java getting my IP address

String ip; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration<InetAddress> addresses = iface.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); ip = addr.getHostAddress(); System.out.println(iface.getDisplayName() + ” ” + ip); } } } catch (SocketException e) { throw new … Read more

How to check if an IP address is from a particular network/netmask in Java?

Option 1: Use spring-security-web‘s IpAddressMatcher. Unlike Apache Commons Net, it supports both ipv4 and ipv6. import org.springframework.security.web.util.matcher.IpAddressMatcher; … private void checkIpMatch() { matches(“192.168.2.1”, “192.168.2.1”); // true matches(“192.168.2.1”, “192.168.2.0/32”); // false matches(“192.168.2.5”, “192.168.2.0/24”); // true matches(“92.168.2.1”, “fe80:0:0:0:0:0:c0a8:1/120”); // false matches(“fe80:0:0:0:0:0:c0a8:11”, “fe80:0:0:0:0:0:c0a8:1/120”); // true matches(“fe80:0:0:0:0:0:c0a8:11”, “fe80:0:0:0:0:0:c0a8:1/128”); // false matches(“fe80:0:0:0:0:0:c0a8:11”, “192.168.2.0/32”); // false } private boolean matches(String ip, … Read more

How do I check whether a value in a string is an IP address

Ruby has already the needed Regex in the standard library. Checkout resolv. require “resolv” “192.168.1.1” =~ Resolv::IPv4::Regex ? true : false #=> true “192.168.1.500” =~ Resolv::IPv4::Regex ? true : false #=> false “ff02::1” =~ Resolv::IPv6::Regex ? true : false #=> true “ff02::1::1” =~ Resolv::IPv6::Regex ? true : false #=> false If you like it the … Read more

json-server cannot access via local IP

I found the solution for this issue: json-server –host 192.168.1.XXX my_file.json Using this command, server is deployed on my local IP, and Windows asks for a firewall exception. Another solution is to switch to .NET server – another free simple fake server where I can setup local IP as endpoint. All it needs to: install … Read more

IP-address ending with zero? [closed]

An IP address ending in .0 is perfectly legal these days. However, some devices (and firewall policies) believe that it isn’t. In the old “classfull” addressing scheme, IPs from 192.0.0.0 to 223.255.255.255 were considered “class C” space, i.e. they had an implicit subnet mask of 255.255.255.0. So, back then, you couldn’t actually have a .0 … Read more

Get local IP address in Qt

Use QNetworkInterface::allAddresses() const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost); for (const QHostAddress &address: QNetworkInterface::allAddresses()) { if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost) qDebug() << address.toString(); }