// Achtung! Dieses Programm nutzt Methoden die erst ab JDK 1.4 vorhanden sind. // Compilierung mit früheren Versionen führt zu Fehlermdg! import java.net.*; import java.awt.*; import java.awt.event.*; class INetTest extends Frame{ // Version & Autor // Vorläufer-Version VersionsDatum = "02.07.2001"; "07.08.2003"; private static final String VersionsDatum = "29.09.2004"; private static final String Autor = "Manfred Sommer"; private static String Titel = "IP Adressen Test"; // IP Felder private static InetAddress MeineIPadr; // Fonts private static final Font FontMono = new Font("Monospaced", Font.PLAIN, 18); private static final Font FontSansSerif = new Font("SansSerif", Font.PLAIN, 14); private static final Font BigItalicSansSerif = new Font("SansSerif", Font.ITALIC, 16); // Haupt - Fenstergrösse private static int XMax = 460; private static int YMax = 250; // Fenster für mdg private static TextArea mdg = new TextArea(5,38); // Label und Fenster für Eingaben für mdg private static Label EinLabel = new Label("Eingabe einer IPAdr oder eines Namens", Label.LEFT); private static TextField IPString = new TextField(38); // Beginn Menü private static MenuBar MainMenuBar = new MenuBar(); private static Menu DateiMenu = new Menu("Datei"); private static MenuItem DateiOpen = new MenuItem("Öffnen..."); private static Menu BearbeitenMenu = new Menu("Bearbeiten"); private static MenuItem MeineIPAdr = new MenuItem("Meine IP Adresse"); private static MenuItem LoeMenu = new MenuItem("Fenster löschen"); private static MenuItem Beenden = new MenuItem("Exit"); private static Menu HelpMenu = new Menu("Hilfe"); private static MenuItem AboutBox = new MenuItem("About"); // End Menü //// Konstruktor. INetTest () { super(Titel); // Fonts für Menüs setFont(FontMono); DateiMenu.setFont(FontSansSerif); DateiOpen.setFont(FontSansSerif); BearbeitenMenu.setFont(FontSansSerif); MeineIPAdr.setFont(FontSansSerif); LoeMenu.setFont(FontSansSerif); Beenden.setFont(FontSansSerif); HelpMenu.setFont(FontSansSerif); AboutBox.setFont(FontSansSerif); // Font für Text Bereich setFont(new Font("Monospaced", Font.PLAIN, 18)); setLayout(new FlowLayout(FlowLayout.LEADING)); setMenuBar(MainMenuBar); MainMenuBar.add(DateiMenu); DateiMenu.add(DateiOpen); DateiMenu.add(Beenden); MainMenuBar.add(BearbeitenMenu); BearbeitenMenu.add(MeineIPAdr); BearbeitenMenu.add(LoeMenu); MainMenuBar.add(HelpMenu); HelpMenu.add(AboutBox); add(EinLabel); add(IPString); mdg.setEditable(false); add(mdg); DateiOpen.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ doOpenFile();} }); MeineIPAdr.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ doMeineIPAdr();} }); IPString.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { doLookUpIPAdr(IPString.getText());} }); LoeMenu.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ doLoe();} }); Beenden.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ FensterBeenden(); } }); AboutBox.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ showAboutBox(); } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { FensterBeenden(); } }); setBackground(Color.lightGray); setSize(XMax, YMax); setVisible(true); } void FensterBeenden() { dispose(); System.exit(0); } // show about box void showAboutBox(){ final Frame f = new Frame("About Box"); final Button Ok = new Button("Ok"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { f.dispose(); } }); Ok.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { f.dispose(); } }); f.setFont (BigItalicSansSerif); f.setLayout (new GridLayout(0, 1)); f.add(new Label(" ", Label.CENTER)); f.add(new Label(Titel, Label.CENTER)); f.add(new Label("Datum der letzten Änderung: " + VersionsDatum, Label.CENTER)); f.add(new Label("Autor: Manfred Sommer", Label.CENTER)); f.add(new Label(" ", Label.CENTER)); f.add(Ok); f.pack(); f.setVisible(true); } void doOpenFile(){ mdg.append("Dummy für: Datei - öffnen.\n"); } static int ByteToInt(byte b){ return b & 255; } boolean keineZahl(String z){ for (int i=0; i< z.length(); i++) if (!Character.isDigit(z.charAt(i))) return true; return false; } byte[] scanIP(String ia){ int P1 = ia.indexOf('.'); int P2 = ia.indexOf('.', P1+1); int P3 = ia.indexOf('.', P2+1); if ((P1 < 0) || (P2 < 0) || (P3 < 0)) return null; String s1 = ia.substring(0, P1); if (keineZahl(s1)) return null; String s2 = ia.substring(P1+1, P2); if (keineZahl(s2)) return null; String s3 = ia.substring(P2+1, P3); if (keineZahl(s3)) return null; String s4 = ia.substring(P3+1); if (keineZahl(s4)) return null; byte[] res = new byte[4]; short sh = Short.parseShort(s1); res[0] = (byte) sh; sh = Short.parseShort(s2); res[1] = (byte) sh; sh = Short.parseShort(s3); res[2] = (byte) sh; sh = Short.parseShort(s4); res[3] = (byte) sh; return res; } void doLoe(){ mdg.setText(""); } void doMeineIPAdr(){ mdg.append("Meine IP Adresse ermitteln...\n"); MeineIPadr = null; try { MeineIPadr = InetAddress.getLocalHost();} catch (UnknownHostException e) { mdg.append("Dieser Computer kennt seine IP-Adresse nicht.\n"); return; } mdg.append("Meine Host-Addr: " + MeineIPadr.getHostAddress() + "\n"); mdg.append("Mein Host-Name: " + MeineIPadr.getHostName() + "\n"); } void doLookUpIPAdr(String einString){ mdg.append("IP Adresse von " + einString + " ermitteln...\n"); byte[] eineIPNummer = scanIP(einString); InetAddress eineAdresse = null; InetAddress[] vieleAdressen = null; if (eineIPNummer == null) // by Name try { eineAdresse = InetAddress.getByName(einString); vieleAdressen = InetAddress.getAllByName(einString); } catch (UnknownHostException e) { mdg.append("getByName: UnknownHostException.\n"); return; } else // by Addr. Achtung Diese Methode gibt es erst ab JDK 1.4 try { eineAdresse = InetAddress.getByAddress(eineIPNummer); } catch (UnknownHostException e) { mdg.append("getByAddress: UnknownHostException.\n"); return; } mdg.append("Host-Addr: " + eineAdresse.getHostAddress() + "\n"); mdg.append("Host-Name: " + eineAdresse.getHostName() + "\n"); if (vieleAdressen != null) for (int i=0; i < vieleAdressen.length; i++){ mdg.append("Host-Addr: " + vieleAdressen[i].getHostAddress() + "\n"); mdg.append("Host-Name: " + vieleAdressen[i].getHostName() + "\n"); } } public static void main(String args[]) { new INetTest(); } }