forked from varunon9/Remote-Control-PC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetMyIpAddress.java
More file actions
83 lines (76 loc) · 2.6 KB
/
Copy pathGetMyIpAddress.java
File metadata and controls
83 lines (76 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ipaddress;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.regex.Pattern;
/**
*
* @author varun
*/
public class GetMyIpAddress {
//to validate ip address
private static final Pattern PATTERN = Pattern.compile(
"^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
String ipAddresses[] = new String[10], temp;
int j = 0;
public static boolean validateIP(final String ip) {
return PATTERN.matcher(ip).matches();
}
public boolean validatePort(String portNumber) {
if ((portNumber != null) && (portNumber.length() >= 4) && (portNumber.matches(".*\\d.*"))) {
if( (Integer.parseInt(portNumber) > 1023))
return true;
else
return false;
}
else
return false;
}
public String[] ipAddress() {
System.out.println("Printing only IPv4 Addresses");
Enumeration e = null;
String s = "";
try {
e = NetworkInterface.getNetworkInterfaces();
}
catch (Exception exception) {
exception.printStackTrace();
}
while (e.hasMoreElements()) {
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements()) {
InetAddress i = (InetAddress) ee.nextElement();
temp = i.getHostAddress();
//this temp contains IPv4 as well as IPv6 addresses
//System.out.println(temp);
if((temp.charAt(1) == '7' || temp.charAt(1) == '9') && (temp.charAt(2) == '2')) {
ipAddresses[j] = temp;
j++;
System.out.println(temp);
}
else if(temp.charAt(0) == '1' && temp.charAt(1) == '0') {
ipAddresses[j] = temp;
j++;
System.out.println(temp);
}
}
}
if (ipAddresses[0] == null) {
ipAddresses[0] = "127.0.0.1";
}
return ipAddresses;
}
public static void main(String args[]) {
new GetMyIpAddress().ipAddress();
}
//List of private IP Addresses
//10.0.0.1 to 10.255.255.254
//172.16.0.1 to 172.31.255.254
//192.168.0.1 to 192.168.255.254
}