IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Servlets/JSP Java Discussion :

Scanner les ports d'une machine distante


Sujet :

Servlets/JSP Java

  1. #1
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut Scanner les ports d'une machine distante
    J'ai un programme Java qui permet de scanner un port d'une machine mais le problème que ce programme n'est fonctionnel que sur la machine locale je ne sais pas pourquoi si je change l'adresse IP par une autre distante connectée à mon PC ne veut pas s'exécuter
    voila mon programme qui contient une classe Pscanner el main le 2 sous un package fabrekscan:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
    package fabrekscan;
     
    import java.net.*;
    import java.io.IOException;
    public class PScanner {
     
     
     
            public static void scan(final InetAddress remote)
            {
     
            int port1=80;
            String hostname = remote.getHostName();
            try {
                     Socket s1 = new Socket(remote,port1);
     
                     System.out.println("Server is listening on port " + port1+ " of " + hostname + "\n");
     
                     s1.close();
               }
                     catch (IOException ex)
                     {
     
                     System.out.println("Server is not listening on port " + port1+ " of " + hostname);
     
                     }
               }
    }
    le main
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package fabrekscan;
     
    import java.net.InetAddress;
    import java.net.UnknownHostException;
     
    /**
     *
     * @author AMINE
     */
    public class main {
     
     
         public static void main(String[] args) 
             {
                //PScanner ps=new PScanner();
                InetAddress ia=null;
                String host="192.168.1.2";
                 try {
                        if(host!=null)
                        {
                            ia = InetAddress.getByName(host);
                           // for(;;)//boucle infinie
                            PScanner.scan(ia);
                        }
                     }
                 catch (UnknownHostException e) 
                 {
                 System.err.println(e );
                 }
                System.out.println("Bye from NFS");
     
           }
    }
    Merci de me corriger les fautes afin que mon programme sera exécutables même avec des machines distantes.

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    peux tu afficher toute tes exceptions (y compris la ioexception) et nous faire un copier / coller de celle qui se déclenche avec tout son stacktrace.

  3. #3
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    C'est ce que j'obtiens après l'exécution toujours:

    init:
    deps-jar:
    compile:
    run:
    Server is not listening on port 80 of 192.168.1.2
    Bye from NFS
    BUILD SUCCESSFUL (total time: 14 seconds)


  4. #4
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    affiche la IOException s'il te plait, qu'on aie le message exacte.

  5. #5
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    Mais comment je peux afficher l'exception. Rien ne s'affiche!!!

  6. #6
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    ex.printStackTrace() dans ton catch (IOException ex)

  7. #7
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    Voilà donc l'exception:

    run:
    Server is not listening on port 80 of 192.168.1.2
    java.net.SocketException: Network is unreachable: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    Bye from NFS
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:516)
    at java.net.Socket.connect(Socket.java:466)
    at java.net.Socket.<init>(Socket.java:366)
    at java.net.Socket.<init>(Socket.java:179)
    at fabrekscan.PScanner.scan(PScanner.java:15)
    at fabrekscan.main.main(main.java:28)
    BUILD SUCCESSFUL (total time: 11 seconds)

  8. #8
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    Network is unreachable: connect
    Le réseau que tu vise (192.168.1.xxx) n'est pas accessible depuis ta machine où tu lance le programme -> vérifie ta configuration réseau, ce n'est pas un problème du à java.

  9. #9
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    Ok Merci pour votre aide

  10. #10
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    Est ce que je peux minimiser de code dans JSP et le mettre dans la classe PScanner car l'exécution et très longue même le rafraîchissement?

  11. #11
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    la classe PScanner:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    package fabrekscan;
     
    import java.net.*;
    import java.io.IOException;
     
     public class PScanner {
            public static int scan( InetAddress remote,int port1)
            {       
            int b=1;
            String hostname = remote.getHostName();
            try {
                     Socket s1 = new Socket(hostname,port1);
                     b=1;
                     s1.close();
                }
                     catch (IOException ex)
                     {
                     b=0;
                     }
            return b;
               }
    }







    La page JSP:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <%@page import="java.util.*"%> 
    <%@page import="java.io.*"%>
    <%@page import="fabrekscan.PScanner"%>
    <%@ page import="java.net.InetAddress"%>
    <%@ page import="java.net.UnknownHostException"%>
    <META http-equiv="Refresh" content="4">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
     
        <center> <h1>Service</h1</center><br>
        <%
        PScanner p=new PScanner();
         InetAddress ia=null;
                 int port1=3306;
                String host="localhost";
                 try {
                        if(host!=null)
                        {
                            ia = InetAddress.getByName(host);
                            if(p.scan(ia,port1)==1)
                                {
                                %>
                                <center><img src="up.jpg" width="100" height="200" alt="up"/></center>
     
                                <%}else
                                   {  
                                    %>
     
                                    <center><img src="down.jpg" width="100" height="200" alt="down"/></center>
                                    <%
                                   }      
                        }
                     }
                   
                 catch (UnknownHostException e) 
                 {
                 System.err.println(e );
                 }      
        %>
        </body>
    </html>

  12. #12
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    Si le port est en marche on aura une flèche verte up.
    Sinon une flèche rouge Down.

    Sur localhost l'exécution est rapide.

    Sur une machine distante, elle très longue > 30 secondes.

  13. #13
    Expert éminent
    Avatar de _skip
    Homme Profil pro
    Développeur d'applications
    Inscrit en
    Novembre 2005
    Messages
    2 898
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur d'applications
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Novembre 2005
    Messages : 2 898
    Points : 7 752
    Points
    7 752
    Par défaut
    Si la machine répond pas, c'est long avant d'atteindre le timeout.
    Utilise un timeout plus court.

  14. #14
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    C'est pas ça.
    Lorsque j'actualise la page j'attends une longue durée pour qu'elle s'affiche.

  15. #15
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    Comment changer le timeout ???

  16. #16
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    pour spécifier les timeout, il faut passer par ce genre de code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    Socket sock = new Socket(); // crée une socket quelconque, non connectée
    sock.bind(null);// Prend un port local temporaire pour la réception
    sock.connect(new InetSocketAddress(host,port), 500); // se connecte à host:port avec un timeout de 500 ms

  17. #17
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    J'ai mis ce code dans la classe PScanner mais il n'a rien changé.

  18. #18
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    c'est que c'est pas le scan mais la machine distante qui met du temps à répondre.

    Si tu change ton jsp comme ceci, ca donne quoi comem affichage?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    if(host!=null)
                        {
                            Date start = new Date();
                            ia = InetAddress.getByName(host);
                            Date end = new Date();
                            out.println("DNS lookup réalisé en "+(end.getTime()-start.getTime())+" millisecondes");
                            start=end;
                            if(p.scan(ia,port1)==1)
                                {
                                %>
                                <center><img src="up.jpg" width="100" height="200" alt="up"/></center>
     
                                <%}else
                                   {  
                                    %>
     
                                    <center><img src="down.jpg" width="100" height="200" alt="down"/></center>
                                    <%
                                   }      
                            end = new Date();
                            out.println("scan réalisé en "+(end.getTime()-start.getTime())+" millisecondes");
                                 
                        }

  19. #19
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    Il m'a donné ça avec les images des flèches up et down.

    DNS lookup réalisé en 0 millisecondes
    up
    scan réalisé en 16 millisecondes DNS lookup réalisé en 0 millisecondes
    up
    scan réalisé en 16 millisecondes DNS lookup réalisé en 0 millisecondes
    down
    scan réalisé en 500 millisecondes DNS lookup réalisé en 0 millisecondes
    down
    scan réalisé en 11046 millisecondes DNS lookup réalisé en 0 millisecondes
    down
    scan réalisé en 12782 millisecondes DNS lookup réalisé en 0 millisecondes
    down
    scan réalisé en 11031 millisecondes DNS lookup réalisé en 0 millisecondes
    up
    scan réalisé en 0 millisecondes DNS lookup réalisé en 0 millisecondes
    up
    scan réalisé en 16 millisecondes DNS lookup réalisé en 0 millisecondes
    down
    scan réalisé en 515 millisecondes DNS lookup réalisé en 0 millisecondes
    down
    scan réalisé en 21453 millisecondes DNS lookup réalisé en 0 millisecondes
    down
    scan réalisé en 15657 millisecondes

  20. #20
    Membre à l'essai
    Inscrit en
    Avril 2009
    Messages
    42
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 42
    Points : 17
    Points
    17
    Par défaut re
    Le retard ne se fait que si le service est down ou la machine est déconnectée.

Discussions similaires

  1. Lister les fichiers d'une machine distante
    Par essadiki_ista dans le forum Général Java
    Réponses: 4
    Dernier message: 05/11/2013, 20h29
  2. Réponses: 0
    Dernier message: 06/06/2012, 10h44
  3. Bloquer un service (port) d'une machine distante en Java
    Par mbollo2005 dans le forum Général Java
    Réponses: 1
    Dernier message: 23/12/2010, 13h50
  4. Réponses: 0
    Dernier message: 14/11/2010, 16h49
  5. Réponses: 4
    Dernier message: 10/09/2002, 17h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo