|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Jan 2006
Messaggi: 64
|
[Java] Risolvere l'hostname da un IP
Codice:
// IpToDns.java // Semplice utility, permette di risalire al DNS o all'IP servendosi di uno dei due import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; public class IpToDns extends JFrame { private JLabel oneLabel, twoLabel; // label per indicare i form private JTextField fieldIP, fieldDNS; // form private JButton scanButton; // button per lo scan private String stringIP, stringDNS; // stringhe da inserire nei form // costruttore della classe public IpToDns() { super( "IpToDNS and Reverse" ); // ottiene il riquadro dei contenuti e ne imposta il layout Container layout = getContentPane(); layout.setLayout( new FlowLayout() ); // crea gli oggetti grafici oneLabel = new JLabel("IP: "); layout.add( oneLabel ); fieldIP = new JTextField( stringIP, 30 ); layout.add( fieldIP ); twoLabel = new JLabel("DNS: "); layout.add( twoLabel ); fieldDNS = new JTextField( stringDNS, 30 ); layout.add( fieldDNS ); scanButton = new JButton("Scan"); layout.add( scanButton ); // aggiungiamo un ascoltatore di eventi a scanButton GestioneEventi event = new GestioneEventi(); scanButton.addActionListener( event ); setVisible( true ); setSize( 390, 130 ); } // classe main public static void main( String args[] ) { IpToDns startNdRun = new IpToDns(); startNdRun.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // classe per la gestione degli eventi private class GestioneEventi implements ActionListener { // gestisce eventi del button public void actionPerformed( ActionEvent evento ) { if (fieldIP.getText().length() == 0 && fieldDNS.getText().length() != 0) dnsIP(); if (fieldDNS.getText().length() == 0 && fieldIP.getText().length() != 0) ipDNS(); // se tutti e due i form sono vuoti restituisce un JOptionPane informando l'utente // di riempire almeno uno dei due campi if (fieldIP.getText().length() == 0 && fieldDNS.getText().length() == 0) JOptionPane.showMessageDialog(null, "Fill the IP and DNS form", "IP and DNS empty!", JOptionPane.ERROR_MESSAGE); } } // dato l'IP risale all'hostname private void ipDNS() { try { InetAddress ipdns = InetAddress.getByAddress( fieldIP.getText(), new byte[]{} ); fieldDNS.setText( ipdns.getHostName() ); } catch ( UnknownHostException exception ) { JOptionPane.showMessageDialog( null, "Error", "Error", JOptionPane.ERROR_MESSAGE ); } } // dato l'hostname risale all'IP private void dnsIP() { try { InetAddress dnsip = InetAddress.getByName( fieldDNS.getText() ); fieldIP.setText( dnsip.getHostAddress() ); } catch ( UnknownHostException exception ) { JOptionPane.showMessageDialog( null, "Error", "Error", JOptionPane.ERROR_MESSAGE ); } } } Nella prima funzione (dato l'hostname risale all'IP) e' tutto ok. Ma, se provo a risalire ad un hostname dando l'IP, il metodo ipDNS ritorna sempre UnknownHostException. Provate a compilarlo e guardate voi stessi. Per me, il problema sta' qui: InetAddress ipdns = InetAddress.getByAddress( fieldIP.getText(), new byte[]{} ); , ma sinceramente non riesco a capire come risolvere (sempre quel maledetto secondo argomento ![]() |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Puoi usare lo stesso InetAddress.getByName ("xxx.xxx.xxx.xxx")
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Oct 2001
Messaggi: 11471
|
Finalfire l'immagine nella tua firma è decisamente troppo grande. Editala.
ciao ![]() |
![]() |
![]() |
![]() |
#4 | |
Member
Iscritto dal: Jan 2006
Messaggi: 64
|
Quote:
![]() @VICIUS: grazie per l'informazione. provvedo subito ![]() |
|
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Quote:
![]()
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 04:35.