Salve ragazzi,
stò progettando (già ribadito per due volte) un piccolo FireWall (questioni didattiche) in Java.
Ho un problema con la classe InetAdress.
Infatti avrei bisogno di creare un InetAddress identificato da un indirizzo ipv4.
Ho compreso che debbo usare il metodo getByAddress, che ha bisogno in input di un byte[].
Bene, ho scritto un metodo che crea l'array dalla stringa, tuttavia, ovviamente, poichè nel byte il numero massimo è 127, se metto ad esempio l'indirizzo 192.168.2.1, mi parte un eccezione:
Dove sbaglio???codice:Value out of range. Value:"192" Radix:10
Per semplificare vi posto il codice in una main class.
codice:public class MainClass2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String ipAdressString = "192.168.2.1"; String part1 = ""; String part2 = ""; String part3 = ""; String part4 = ""; char charByteStop = '.'; boolean firstPoint = false; boolean secondPoint = false; boolean thirdPoint = false; boolean fourthPoint = false; int k = 0; int j = 0; int h = 0; int l = 0; int length = ipAdressString.length() - 1; if(!firstPoint && !secondPoint && !thirdPoint && !fourthPoint) { for (k = 0; k <= length && k <= 3 && !firstPoint; k++) { char charRead = ipAdressString.charAt(k); if (charRead != charByteStop) { String concString = Character.toString(charRead); part1 = part1.concat(concString); } else { firstPoint = true; } } for (j = k; j <= length && j <= k + 3 && firstPoint && !secondPoint; j++) { char charRead = ipAdressString.charAt(j); if (charRead != charByteStop) { String concString = Character.toString(charRead); part2 = part2.concat(concString); } else { secondPoint = true; } } for (h = j; h <= length && h <= j + 3 && firstPoint && secondPoint && !thirdPoint; h++) { char charRead = ipAdressString.charAt(h); if (charRead != charByteStop) { String concString = Character.toString(charRead); part3 = part3.concat(concString); } else { thirdPoint = true; } } for (l = h; l <= length && l <= h + 3 && firstPoint && secondPoint && thirdPoint && !fourthPoint; l++) { char charRead = ipAdressString.charAt(l); if (charRead != charByteStop) { String concString = Character.toString(charRead); part4 = part4.concat(concString); } else { fourthPoint = true; } } } Byte[] ipAdress = { new Byte(part1), new Byte(part2), new Byte(part3), new Byte(part4) }; for (int s = 0; s <= ipAdress.length - 1; s++) { System.out.print(ipAdress[h]); } } }

) un piccolo FireWall (questioni didattiche) in Java.
Rispondi quotando


