PDA

View Full Version : [Java] Risolvere l'hostname da un IP


Finalfire
21-07-2006, 10:46
// 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 );
}
}

}

Ho creato questa piccola applicazione che, dato l'hostname risale all'IP e viceversa.
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 :doh: ).

andbin
21-07-2006, 11:09
Puoi usare lo stesso InetAddress.getByName ("xxx.xxx.xxx.xxx")

VICIUS
21-07-2006, 11:34
Finalfire l'immagine nella tua firma è decisamente troppo grande. Editala.

ciao ;)

Finalfire
21-07-2006, 12:06
Finalfire l'immagine nella tua firma è decisamente troppo grande. Editala.

ciao ;)
@andbin: grazie, funziona :) Ma, che differenza c'e' nell'usare getByName al posto di getByAddress?

@VICIUS: grazie per l'informazione. provvedo subito :)

andbin
21-07-2006, 12:52
Ma, che differenza c'e' nell'usare getByName al posto di getByAddress?getByAddress riceve l'indirizzo IP specificato in "network byte order" come array di byte .... avevo fatto l'esempio nell'altro tuo thread. ;)