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

Concurrence et multi-thread Java Discussion :

[Thread] (impl. runnable) dans DaemonThread


Sujet :

Concurrence et multi-thread Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    165
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2007
    Messages : 165
    Par défaut [Thread] (impl. runnable) dans DaemonThread
    Bonjour,

    Je fais un serveur multithread, je viens de lui apporter de grosse modification.
    Il y a un DaemonThread et des Thread (implements runnable) pour chaque clients. Mon problème c'est que depuis que j'ai fais les grosses modifications, je n'arrive plus à lancer les Threads pour chaque clients. A chaque fois que je le fait il relance le DaemonThread.
    Quand je fais :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    // create the thread
                    Thread t = new Thread(new ServerData(skt));
                    // give it a name
                    t.setName(skt.getInetAddress().getHostAddress());
                    // start the thread
                    t.start();
    Il ne va pas dans ServerData mais il recommence dans DaemonThread.

    Voici mon code :
    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
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    public class Server {
     
    // list of the Players connected
    protected final List <Player> connections = new ArrayList<Player>();
     
    // list of the IP connected and waiting for logins
    protected final List <Socket> ipWaiting = new ArrayList<Socket>();
     
    // port of the application
    protected int port = 1991;
     
    // objects connections
    protected final Connections con = new Connections();
     
        public Server() throws IOException{
     
            Thread t = new DaemonThread();
     
      }
     
        class DaemonThread extends Thread{
            public DaemonThread(){
                //start the main thread
            start();
            }
     
            @Override
            public void run(){
     
            Socket skt = null;
            ServerSocket srvr = null;
                try {
                    //open the port
                    srvr = new ServerSocket(port);
                } catch (IOException ex) {
                    Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                }
     
            while(true){
                int result = 0;
                 try{
                     //accept the connections
                     skt = srvr.accept();
                     System.out.print("New connection ! \n");
                     //check in address if the client is already connected
                     result = ipWaitingChanges(skt, "check");
                     //error
                }catch(IOException ioe){
                     System.out.print("It didn't work! - "+ioe.getMessage()+"\n");
                     ioe.printStackTrace();
                     ipWaitingChanges(skt, "kill");
                }
                 if(result == 1){
                 //if no error :
                     ipWaitingChanges(skt, "add");
                 }
            }
     
            }
        }
     
     
      class ServerData implements Runnable{
          private Socket skt;
          protected Player player = null;
          private Network network;
          private cmdToServer cts;
          public ServerData(Socket skt) throws IOException{
              //init
              this.skt = skt;
              network = new Network(this.skt);
              cts = new cmdToServer();
              // number of thread
              System.out.println("Number of thread : "+Thread.activeCount());
          }
     
          public void run(){
          try{
                    try {
                        try {
                            //analyse the packet juste receive
                            cts.analyse((Object) network.readObject(), network);
                        } catch (SQLException ex) {
                            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (NoSuchAlgorithmException ex) {
                            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                    }
             }catch(IOException ioe){
                 //if error :
                 //close the connection
                System.out.print("It didn't work! - "+ioe.getMessage()+"\n");
                ioe.printStackTrace();
                if(getPlayerFromSocket(skt) != null){
                    //if he is already login in
                    removePlayer(getPlayerFromSocket(skt), network);
                }else{
                    //if not
                }
             }
          }
      }
    Avez-vous une idée ?

    Cordialement,
    Dafflon Emmanuel

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

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

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Je ne vois pas ton bout de code du haut dans ton code global, donc difficile de savoir ce que tu fait pour lancer tes threads.

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    165
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2007
    Messages : 165
    Par défaut
    J'ai mis à jour le poste du dessus, et voici le code de la méthode ou j'appelle le thread :
    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
      public synchronized int ipWaitingChanges(Socket skt, String type){
          //-1 = error
          int result = -1;
        // create connection
        if(type.equals("add")){
                try {
                    synchronized (ipWaiting) {
                        // add the inetaddress to address
                        ipWaiting.add(skt);
                    }
                    // create the thread
                    Thread t = new Thread(new ServerData(skt));
                    // give it a name
                    t.setName(skt.getInetAddress().getHostAddress());
                    // start the thread
                    t.start();
                } catch (IOException ex) {
                    Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
     
        //check connection
        else if(type.equals("check")){
            //create an iterator
            Iterator it;
            result = 1;
            synchronized(ipWaiting){
            //init an iterator of address
            it = ipWaiting.iterator();
            }
            while(it.hasNext()){
                InetAddress add = null;
                         add = (InetAddress) it.next();
                         if(skt.getInetAddress().equals(add)){
                             System.out.print("Server has refused connection : "+add.getHostAddress()+" - Already connect \n");
                             //2 = already connected
                             result = 2;
                             break;
                         }else{
                             //1 = not connected
                             result = 1;
                         }
             }
        }
     
        return result;
      }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème pour stopper thread avec runnable
    Par fabou3377 dans le forum Concurrence et multi-thread
    Réponses: 3
    Dernier message: 13/03/2008, 13h43
  2. Thread -> Utiliser Synchronize() dans une fonction
    Par Futixu dans le forum C++Builder
    Réponses: 5
    Dernier message: 26/01/2007, 13h13
  3. [C#] Thread et events dans une form
    Par farfadet dans le forum Windows Forms
    Réponses: 3
    Dernier message: 21/12/2006, 18h50
  4. Thread pour afficher dans une CListCtrl
    Par gaudi dans le forum MFC
    Réponses: 14
    Dernier message: 10/08/2006, 12h25

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