DMI's Blog sur les technos .NET(dotnet) et J2EE

DNS -> @IP avec Java (class InetAddress)

InetAddress

 

InetAddress est une classe java fort intéressant car elle permet de retrouver une adresse IP d’un poste à partir de son nom de domaine (DNS). Pour cela java passe par la couche réseau de l’OS pour que celui-ci interroge ces serveurs pour obtenir, soit le nom, soit l’IP.

 

Voici un exemple qui permet d’avoir une adresse IP à partir d’un nom.

 

package dotnet.mabulle.java;

import java.net.* ;

 

public class NomToIp {

public static void main(String[] args){

String m_Name = args[0];

if(m_Name.length()==0){

System.out.println("Saisissez un nom");

}

try{

InetAddress address = InetAddress.getByName(m_Name);

System.out.println("Name: "+ address.getHostName());

System.out.println("Address: "+ address.getHostAddress());

 

}catch(UnknownHostException Unex){

Unex.printStackTrace();

}

}//fin main

}//fin class

 

Si vous donnez google.fr en entrée, vous obtenez:

 

Name: google.fr

Address: 216.239.57.104

 

Si vous donnez dotnet.mabulle.com en entrée, vous obtenez:

 

Name: dotnet.mabulle.com

Address: 213.251.133.165

 

Dans le prochain article je vous expliquerai comment faire un pseudo ping avec java.

A+

 

 

 

aucun commentaire - aucun rétrolien

Serialisation avec java

package blog.tecnoJava.serialization;

 

import java.beans.XMLDecoder;

import java.beans.XMLEncoder;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

 

public class Serialize {

 

            //Cette méthode permet d’écrire (sérialiser) un objet java dans un fichier

            //Binaire:*.dat,*.bin,...

            public void WriteInBinaryFormat(Object oToSerialize,String strDestinationPath) throws IOException{

                        //Create output streams.

                        FileOutputStream fos = new FileOutputStream(strDestinationPath);

                        ObjectOutputStream oos = new ObjectOutputStream(fos);

                        //Write object

                        oos.writeObject(oToSerialize);

            }

//          Cette Methode Permet de lire un objet sérialiser à partir d’un fichier binaire

            public Object ReadFromBinaryFile(String strSourceFilePath) throws IOException, ClassNotFoundException{

                        //Create input streams.

                        FileInputStream fis = new FileInputStream(strSourceFilePath);

                        ObjectInputStream ois = new ObjectInputStream(fis);

                        //Read object

                        return ois.readObject();

            }

           

            //Cette méthode permet d’écrire (archiver) un objet java dans une archive

            //XML comme ci-dessous

            public void WriteInXMLArchiveFormat(Object oToSerialize,String strDestinationPath) throws FileNotFoundException{

//                      Create output stream.

                        FileOutputStream fos = new FileOutputStream(strDestinationPath);

                        //Create XML encoder

                        XMLEncoder Xmlenc = new XMLEncoder(fos);

                        //Write object.

                        Xmlenc.writeObject(oToSerialize);

            }

            public Object ReadFromXMLArchive(String strSourceFilePath) throws FileNotFoundException{

//                      Create input stream.

                        FileInputStream fis = new FileInputStream(strSourceFilePath);

                        //Create XML decoder

                        XMLDecoder Xmldec = new XMLDecoder(fis);

                        //Read object.

                        return Xmldec.readObject();

            }

}

aucun commentaire - aucun rétrolien