Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1439 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  problème client-serveur (socket)

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

problème client-serveur (socket)

n°2171936
druduss2
Posté le 20-01-2013 à 16:21:22  profilanswer
 

Bonjour,
 
J'aurai aimé avoir un bout de code à pointer et dire "ici ça ne marche pas et je sais pas pourquoi", comme indiqué dans le topic des bonnes pratiques, mais malheureusement ce n'est pas le cas.
 
Je suis confronté à un comportement bizarre de mon projet de client-server java par socket, que je n'arrive pas à comprendre ou à en isoler les causes.
 
Je vous explique :
Voulant développer un petit programme perso, et ayant déjà été confronté à la difficulté de faire communiquer plusieurs personnes utilisant le même programme, je me suis dit : Tiens, je vais développer une petite librairie client-server en java, utilisant les sockets. Je tiens à dire que j'avais réussi à créer une application client-server en java par socket il y a peu, mais que le code du serveur et du client était trop lié à l'application pour être réutilisé en l'état. J'ai donc créé un nouveau projet, que j'ai nommé LibraryNetworkMango, dont le but était de proposer un petit serveur java pouvant envoyer des Object java par sérialization.
Donc j'ai développé ce projet, qui marche bien, sauf un comportement que je ne comprend pas du tout.
 
Donc le problème :
- Lorsqu'un client (MangoClient) se connecte au serveur (MangoServer), le serveur l'accepte et lui donne un ID unique (PersonnalID), du côté client, lorsque je reçois cette ID unique, je considère que c'est interne, et je n’envoie pas de notification à l'application.
- Là, c'est l'application qui gère, dans mon cas j'envoie un objet donnant l'état de l'application client au serveur après avoir lancé le client.
- Le serveur reçoit un event (MangoServerEvent), qui contient un type (EventTypeServer) état OBJECTRECEIVED.
- A ce moment-là, le serveur (de l'application) décide de faire une MAJ avec les informations reçues du client, puis de tout ré-envoyer à tous les clients (note: le serveur est aussi une instance de mon application, et donc je rafraîchis le GUI avec les nouvelles informations du client, et ça s'affiche donc c'est bon).
- Le client reçoit la MAJ et rafraîchit son GUI, ça marche.
- A partir de là, je me retrouve confronté à un cas de figure bien particulier :
  _ Toute modification du client est envoyé au serveur et affiché (la connexion dans ce sens marche).
  _ Toute modification du serveur ne s'affiche pas côté client. J'ai mis des System.out.println(); côté client et serveur afin de lire ce que j'envoie et reçoit, et il s'avère que l'objet envoyé correspond, mais que l'objet reçu est toujours la première version de l'objet reçu (le premier objet envoyé par le serveur).
 
Voici une image représentant l'arborescence de mon projet sur Netbeans :
http://data.imagup.com/12/1173359517.jpg
 
Il faut que je m'excuse pour la quantité de code qui va suivre, ne sachant pas où se trouve l'erreur...
Note : j'ai enlevé tous les commentaires afin de diminuer la quantité de code J'ai aussi enlevé la partie envoyer des String (je voulais que Mango offre la possibilité de choisir le mode de communication). J'ai aussi enlevé la partie affichant l'état au fur et à mesure.
 
Merci d'avance si vous prenez le temps de vous pencher sur mon problème, je suis bloqué dessus depuis trois jours...
 
MangoClient
Code complet (commentaire, affiche de l'état, possibilité d'envoyer des String) : 694 lignes.
Version allégé : 245 lignes.

Code :
  1. package newpackage;
  2. import CommonClass.*;
  3. import java.io.*;
  4. import java.net.InetAddress;
  5. import java.net.Socket;
  6. import java.net.SocketException;
  7. import java.net.UnknownHostException;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. public class MangoClient implements Runnable {
  11.     private String password;
  12.     private CommunicationType communicationType;
  13.     private ObjectInputStream readerObjet;
  14.     private ObjectOutputStream writerObjet;
  15.     private BufferedReader readerString;
  16.     private BufferedWriter writerString;
  17.     public MangoClientListener mangoListener;
  18.     private Socket serverClient;
  19.     private Thread network;
  20.     private String ip;
  21.     private int port;
  22.     private static boolean afficherLogTest = true;
  23.     private int personalID;
  24.     public MangoClient(String ip, int port, String password, CommunicationType communicationType, MangoClientListener mangoListener) {
  25.         this.personalID = -1;
  26.         this.ip = ip;
  27.         this.port = port;
  28.         this.communicationType = communicationType;
  29.         this.password = password;
  30.         this.mangoListener = mangoListener;
  31.     }
  32.     public MangoClient(String ip, int port, CommunicationType communicationType, MangoClientListener mangoListener) {
  33.         this(ip, port, "", communicationType, mangoListener);
  34.     }
  35.     @Override
  36.     public void run() {
  37.         if (this.communicationType == CommunicationType.OBJECT) {
  38.             while (this.network != null && this.readerObjet != null) {
  39.                 Capsule capsule;
  40.                 try {
  41.                     if ((capsule = (Capsule) this.readerObjet.readObject()) != null) {
  42.                         if (capsule.getMangoMessage() == MangoMessage.INTERN || capsule.getMangoMessage() == MangoMessage.INTERN_PERSONALID) {
  43.                             if (capsule.getMangoMessage() == MangoMessage.INTERN) {
  44.                                 String temp = (String) capsule.getObjet();
  45.                                 this.responseIntern(temp);
  46.                             } else { // INTERN_PERSONALID
  47.                                 this.personalID = (int) capsule.getObjet();
  48.                             }
  49.                         } else {
  50.                             if (this.mangoListener != null) {
  51.                                 this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(capsule.getObjet(), this.communicationType));
  52.                             }
  53.                         }
  54.                     }
  55.                 } catch (IOException ex) {
  56.                     Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  57.                     this.stop();
  58.                     this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  59.                 } catch (ClassNotFoundException ex) {
  60.                     Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  61.                 }
  62.             }
  63.         }
  64.     }
  65.     private void sendINTERN(Object objet) {
  66.         switch (this.communicationType) {
  67.             case OBJECT:
  68.                 if (this.writerObjet != null) {
  69.                     Capsule capsule = new Capsule(objet, MangoMessage.INTERN);
  70.                     try {
  71.                         this.writerObjet.writeObject(capsule);
  72.                         this.writerObjet.flush();
  73.                     } catch (IOException ex) {
  74.                         Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  75.                         this.stop();
  76.                         this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  77.                     }
  78.                 }
  79.                 break;
  80.         }
  81.     }
  82.     public void send(Object objet) {
  83.         switch (this.communicationType) {
  84.             case OBJECT:
  85.                 if (this.writerObjet != null) {
  86.                     Capsule capsule = new Capsule(objet);
  87.                     try {
  88.                         this.writerObjet.writeObject(capsule);
  89.                         this.writerObjet.flush();
  90.                     } catch (IOException ex) {
  91.                         Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  92.                         this.stop();
  93.                         this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  94.                     }
  95.                 }
  96.                 break;
  97.         }
  98.     }
  99.     public void stop() {
  100.         this.network = null;
  101.         switch (this.communicationType) {
  102.             case OBJECT:
  103.                 this.readerObjet = null;
  104.                 this.writerObjet = null;
  105.                 break;
  106.         }
  107.         try {
  108.             this.serverClient.close();
  109.         } catch (IOException ex) {
  110.             Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  111.             this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  112.         }
  113.     }
  114.     public void launchMangoClient() {
  115.         boolean err = false;
  116.         try {
  117.             this.serverClient = new Socket(this.ip, this.port);
  118.             this.serverClient.setSoTimeout(2000); // we wait max 2 seconds before timeout error
  119.             if (!this.serverClient.isConnected()) {
  120.                 err = true;
  121.                 this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  122.             }
  123.         } catch (UnknownHostException ex) {
  124.             Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  125.             this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  126.         } catch (IOException ex) {
  127.             Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  128.             this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  129.         }
  130.         if (err == false) {
  131.             InputStream inputStream = null;
  132.             OutputStream outputStream = null;
  133.             try {
  134.                 inputStream = this.serverClient.getInputStream();
  135.             } catch (IOException ex) {
  136.                 err = true;
  137.                 Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  138.                 this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  139.             }
  140.             if (err == false) {
  141.                 switch (this.communicationType) {
  142.                     case OBJECT:
  143.                         try {
  144.                             this.readerObjet = new ObjectInputStream(new BufferedInputStream(inputStream));
  145.                         } catch (IOException ex) {
  146.                             err = true;
  147.                             Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  148.                             this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  149.                         }
  150.                         break;
  151.                 }
  152.                 try {
  153.                     outputStream = this.serverClient.getOutputStream();
  154.                 } catch (IOException ex) {
  155.                     err = true;
  156.                     Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  157.                     this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  158.                 }
  159.                 switch (this.communicationType) {
  160.                     case OBJECT:
  161.                         try {
  162.                             this.writerObjet = new ObjectOutputStream(outputStream);
  163.                         } catch (IOException ex) {
  164.                             err = true;
  165.                             Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  166.                             this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  167.                         }
  168.                         break;
  169.                 }
  170.                 InetAddress url = this.serverClient.getInetAddress();
  171.                 this.personalID = this.waitingForPersonalID();
  172.                 if (this.personalID == -1) {
  173.                     this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  174.                 }
  175.                 this.network = new Thread(this);
  176.                 this.network.setName("RestoClient: Logged on: " + url.getHostAddress());
  177.                 this.network.setPriority(Thread.NORM_PRIORITY);
  178.                 this.network.start();
  179.                 try {
  180.                     this.serverClient.setSoTimeout(0);
  181.                 } catch (SocketException ex) {
  182.                     err = true;
  183.                     Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  184.                     this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  185.                 }
  186.             } else {
  187.                 this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  188.             }
  189.         }
  190.     }
  191.     private void responseIntern(String temp) {
  192.         String stringResponse = null;
  193.         switch (temp) {
  194.             case "INTERN/askpassword":
  195.                 stringResponse = this.password;
  196.                 break;
  197.             default: // on reçoit la personalID sous la forme "INTERN/xxx"
  198.                 String id_string = temp.substring(7);
  199.                 this.personalID = Integer.parseInt(id_string);
  200.                 break;
  201.         }
  202.         if (stringResponse != null) {
  203.             if (this.communicationType == CommunicationType.OBJECT) {
  204.                 this.sendINTERN(stringResponse);
  205.             }
  206.         }
  207.     }
  208.     public int getPersonalID() {
  209.         return this.personalID;
  210.     }
  211.     private int waitingForPersonalID() {
  212.         int personalID_local = -1;
  213.         switch (this.communicationType) {
  214.             case OBJECT:
  215.                 Capsule capsule;
  216.                 try {
  217.                     if ((capsule = (Capsule) this.readerObjet.readObject()) != null) {
  218.                         personalID_local = (int) capsule.getObjet();
  219.                     }
  220.                 } catch (IOException ex) {
  221.                     Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  222.                     this.stop();
  223.                     this.mangoListener.receiveMangoClientEvent(new MangoClientEvent(EventTypeClient.CLIENTCRASH));
  224.                 } catch (ClassNotFoundException ex) {
  225.                     Logger.getLogger(MangoClient.class.getName()).log(Level.SEVERE, null, ex);
  226.                 }
  227.                 break;
  228.         }
  229.         return personalID_local;
  230.     }
  231. }


 
MangoServer
Code complet (commentaire, affiche de l'état, possibilité d'envoyer des String) : 407 lignes.
Version allégé : 215 lignes.

Code :
  1. package newpackage;
  2. import CommonClass.*;
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.util.ArrayList;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. public class MangoServer implements Runnable, MangoServerListener {
  10.     private boolean acceptingClient;
  11.     private int serverPort;
  12.     private int maxClient;
  13.     private String password;
  14.     private CommunicationType communicationType;
  15.     private boolean isPassword;
  16.     private ArrayList<MangoSocket> clients;
  17.     private ServerSocket server;
  18.     private Thread seeker;
  19.     private MangoServerListener mangoListener;
  20.     private boolean isWaitingInternalCommand;
  21.     private static boolean afficherLogTest = false;
  22.     private boolean error;
  23.     public MangoServer(int serverPort, int maxClient, String password, CommunicationType serverType, MangoServerListener mangoListener) {
  24.         this.error = false;
  25.         this.mangoListener = mangoListener;
  26.         this.acceptingClient = true;
  27.         this.serverPort = serverPort;
  28.         this.maxClient = maxClient;
  29.         this.password = password;
  30.         this.communicationType = serverType;
  31.         this.isWaitingInternalCommand = false;
  32.         if (this.password == null) {
  33.             this.isPassword = false;
  34.         } else {
  35.             if (this.password.equals("" )) {
  36.                 this.isPassword = false;
  37.             } else {
  38.                 this.isPassword = true;
  39.             }
  40.         }
  41.         this.clients = new ArrayList<>();
  42.         try {
  43.             this.server = new ServerSocket(this.serverPort);
  44.         } catch (IOException ex) {
  45.             this.closeMangoServer();
  46.             this.mangoListener.receiveMangoServerEvent(new MangoServerEvent(EventTypeServer.SERVERCRASH));
  47.             Logger.getLogger(MangoServer.class.getName()).log(Level.SEVERE, null, ex);
  48.         }
  49.     }
  50.     public MangoServer(int serverPort, int maxClient, CommunicationType serverType, MangoServerListener mangoListener) {
  51.         this(serverPort, maxClient, "", serverType, mangoListener);
  52.     }
  53.     public void stopAcceptingClient() {
  54.         this.acceptingClient = false;
  55.         this.seeker = null;
  56.     }
  57.     public void startMangoServer() {
  58.         this.seeker = new Thread(this);
  59.         this.seeker.setName(" MangoServer: Client Seeker" );
  60.         this.seeker.setPriority(Thread.MIN_PRIORITY);
  61.         this.seeker.start();
  62.     }
  63.     public void closeMangoServer() {
  64.         this.seeker.interrupt();
  65.         this.seeker = null;
  66.         try {
  67.             this.server.close();
  68.         } catch (IOException ex) {
  69.             Logger.getLogger(MangoServer.class.getName()).log(Level.SEVERE, null, ex);
  70.         }
  71.         this.killAndRemoveAllClient();
  72.     }
  73.     @Override
  74.     public void run() {
  75.         boolean accept_new_client = true;
  76.         while (this.acceptingClient == true && this.seeker != null && accept_new_client == true && this.error == false) {
  77.             Socket client = null;
  78.             if (this.server != null) {
  79.                 try {
  80.                     client = this.server.accept();
  81.                 } catch (IOException ex) {
  82.                     this.error = true;
  83.                     Logger.getLogger(MangoServer.class.getName()).log(Level.SEVERE, null, ex);
  84.                 }
  85.                 if (this.error == false) {
  86.                     if (this.isPassword) {
  87.                         this.askPasswordFromNewClient(new MangoSocket(client, this.communicationType, this));
  88.                     } else {
  89.                         this.acceptNewClient(new MangoSocket(client, this.communicationType, this));
  90.                     }
  91.                 }
  92.             }
  93.             if (this.maxClient > 0) {
  94.                 if (this.maxClient > this.countClient()) {
  95.                     accept_new_client = true;
  96.                 } else {
  97.                     accept_new_client = false;
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     private void acceptNewClient(MangoSocket mangoSocket) {
  103.         this.clients.add(mangoSocket);
  104.         this.mangoListener.receiveMangoServerEvent(new MangoServerEvent(EventTypeServer.NEWCLIENT));
  105.     }
  106.     private void askPasswordFromNewClient(MangoSocket mangoSocket) {
  107.         this.isWaitingInternalCommand = true;
  108.         switch (this.communicationType) {
  109.             case OBJECT:
  110.                 Capsule capsule = new Capsule("INTERN/askpassword", MangoMessage.INTERN);
  111.                 mangoSocket.send(capsule);
  112.                 break;
  113.         }
  114.     }
  115.     private void sendNotificationWrongPasswordToNewClient(MangoSocket mangoSocket) {
  116.         switch (this.communicationType) {
  117.             case OBJECT:
  118.                 Capsule capsule = new Capsule("/wrongpassword", MangoMessage.INTERN);
  119.                 mangoSocket.send(capsule);
  120.                 break;
  121.         }
  122.     }
  123.     public boolean respondToClient(Object objet, int personnalID) {
  124.         boolean retour = false;
  125.         MangoSocket mangoSocket = this.getMangoSocket(personnalID);
  126.         if (mangoSocket != null) {
  127.             retour = mangoSocket.send(objet);
  128.         }
  129.         return retour;
  130.     }
  131.     public boolean respondToAllClients(Object objet) {
  132.         boolean zeroErreur = true;
  133.         for (MangoSocket mangoSocket : this.clients) {
  134.             if (mangoSocket != null) {
  135.                 boolean retour = mangoSocket.send(objet);
  136.                 if (retour == false) {
  137.                     zeroErreur = false;
  138.                 }
  139.             }
  140.         }
  141.         return zeroErreur;
  142.     }
  143.     private MangoSocket getMangoSocket(int personnalID) {
  144.         MangoSocket mangoSocket = null;
  145.         for (MangoSocket r : this.clients) {
  146.             if (r.getPersonalID() == personnalID) {
  147.                 mangoSocket = r;
  148.             }
  149.         }
  150.         return mangoSocket;
  151.     }
  152.     private void killAndRemoveAllClient() {
  153.         for (MangoSocket r : this.clients) {
  154.             r.addMangoListener(null);
  155.             r.kill();
  156.         }
  157.     }
  158.     public int countClient() {
  159.         return this.clients.size();
  160.     }
  161.     @Override
  162.     public void receiveMangoServerEvent(MangoServerEvent mangoEvent) {
  163.         if (this.isWaitingInternalCommand) {
  164.             MangoSocket mangoClient;
  165.             mangoClient = this.getMangoSocket(mangoEvent.getPersonnalID());
  166.             String temp = "";
  167.             if (mangoClient != null) {
  168.                 if (temp.equals(this.password)) {
  169.                     this.acceptNewClient(mangoClient);
  170.                 } else {
  171.                     this.sendNotificationWrongPasswordToNewClient(mangoClient);
  172.                 }
  173.             }
  174.         } else {
  175.             switch (mangoEvent.getEventType()) {
  176.                 case LOSTCLIENT:
  177.                     this.removeAndKillClientByPersonnalID(mangoEvent.getPersonnalID());
  178.                     break;
  179.                 case OBJECTRECEIVED:
  180.                     mangoEvent = new MangoServerEvent(mangoEvent.getObjet(), this.communicationType);
  181.                     break;
  182.                 case STRINGRECEIVED:
  183.                     mangoEvent = new MangoServerEvent(mangoEvent.getString(), this.communicationType);
  184.                     break;
  185.             }
  186.             this.mangoListener.receiveMangoServerEvent(mangoEvent);
  187.         }
  188.     }
  189.     private void removeAndKillClientByPersonnalID(int personnalID) {
  190.         MangoSocket mangoSocketToDelete = this.getMangoSocket(personnalID);
  191.         if (mangoSocketToDelete != null) {
  192.             this.clients.remove(mangoSocketToDelete);
  193.             mangoSocketToDelete.kill();
  194.         }
  195.     }
  196. }


 
MangoSocket
Code complet (commentaire, affiche de l'état, possibilité d'envoyer des String) : 397 lignes.
Version allégé : 153 lignes.

Code :
  1. package newpackage;
  2. import CommonClass.*;
  3. import java.io.*;
  4. import java.net.Socket;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. public class MangoSocket implements Runnable {
  8.     private static int countOfID = 1;
  9.     private int personalID;
  10.     private CommunicationType communicationType;
  11.     private Socket socketClient;
  12.     private Thread thread;
  13.     private ObjectInputStream readerObject;
  14.     private ObjectOutputStream writerObject;
  15.     private BufferedReader readerString;
  16.     private BufferedWriter writerString;
  17.     public MangoServerListener mangoListener;
  18.     private static boolean afficherLogTest = true;
  19.     public MangoSocket(Socket socketClient, CommunicationType serverType, MangoServerListener mangoListener) {
  20.         this.mangoListener = mangoListener;
  21.         this.personalID = MangoSocket.getID();
  22.         System.out.println("personalID: " + this.personalID);
  23.         this.communicationType = serverType;
  24.         this.socketClient = socketClient;
  25.         OutputStream outputStream = null;
  26.         InputStream inputStream = null;
  27.         try {
  28.             outputStream = this.socketClient.getOutputStream();
  29.         } catch (IOException ex) {
  30.             Logger.getLogger(MangoSocket.class.getName()).log(Level.SEVERE, null, ex);
  31.         }
  32.         switch (this.communicationType) {
  33.             case OBJECT:
  34.                 try {
  35.                     this.writerObject = new ObjectOutputStream(outputStream);
  36.                 } catch (IOException ex) {
  37.                     Logger.getLogger(MangoSocket.class.getName()).log(Level.SEVERE, null, ex);
  38.                 }
  39.                 break;
  40.         }
  41.         try {
  42.             inputStream = this.socketClient.getInputStream();
  43.         } catch (IOException ex) {
  44.             Logger.getLogger(MangoSocket.class.getName()).log(Level.SEVERE, null, ex);
  45.         }
  46.         switch (this.communicationType) {
  47.             case OBJECT:
  48.                 try {
  49.                     this.readerObject = new ObjectInputStream(new BufferedInputStream(inputStream));
  50.                 } catch (IOException ex) {
  51.                     Logger.getLogger(MangoSocket.class.getName()).log(Level.SEVERE, null, ex);
  52.                 }
  53.                 break;
  54.         }
  55.         if (this.communicationType == CommunicationType.OBJECT) {
  56.             this.sendCapsule(new Capsule(this.personalID, MangoMessage.INTERN_PERSONALID));
  57.         }
  58.         this.thread = new Thread(this);
  59.         this.thread.setName("MangoSocket " + this.personalID);
  60.         this.thread.setPriority(Thread.MIN_PRIORITY);
  61.         this.thread.start();
  62.     }
  63.     private static int getID() {
  64.         return MangoSocket.countOfID++;
  65.     }
  66.     public int getPersonalID() {
  67.         return personalID;
  68.     }
  69.     public void kill() {
  70.         try {
  71.             this.socketClient.close();
  72.         } catch (IOException ex) {
  73.             Logger.getLogger(MangoSocket.class.getName()).log(Level.SEVERE, null, ex);
  74.         }
  75.         this.thread = null;
  76.         this.readerObject = null;
  77.         this.writerObject = null;
  78.         this.readerString = null;
  79.         this.writerString = null;
  80.         if (this.mangoListener != null) {
  81.             this.mangoListener.receiveMangoServerEvent(new MangoServerEvent(EventTypeServer.LOSTCLIENT, this.personalID));
  82.         }
  83.     }
  84.     public boolean send(Object objet) {
  85.         boolean result = false;
  86.         switch (this.communicationType) {
  87.             case OBJECT:
  88.                 result = this.sendObject(objet);
  89.                 break;
  90.         }
  91.         return result;
  92.     }
  93.     private boolean sendObject(Object objet) {
  94.         Capsule capsule = new Capsule(objet);
  95.         return this.sendCapsule(capsule);
  96.     }
  97.     private boolean sendCapsule(Capsule capsule) {
  98.         boolean result;
  99.         if (this.communicationType == CommunicationType.OBJECT) {
  100.             if (this.writerObject != null) {
  101.                 result = true;
  102.                 try {
  103.                     this.writerObject.writeObject(capsule);
  104.                     this.writerObject.flush();
  105.                 } catch (IOException ex) {
  106.                     Logger.getLogger(MangoSocket.class.getName()).log(Level.SEVERE, null, ex);
  107.                 }
  108.             } else {
  109.                 result = false;
  110.             }
  111.         } else {
  112.             result = false;
  113.         }
  114.         return result;
  115.     }
  116.     @Override
  117.     public void run() {
  118.         switch (this.communicationType) {
  119.             case OBJECT:
  120.                 while (this.readerObject != null) {
  121.                     Capsule capsule;
  122.                     try {
  123.                         if ((capsule = (Capsule) this.readerObject.readObject()) != null) {
  124.                             if (this.personalID != -1) {
  125.                                 this.mangoListener.receiveMangoServerEvent(new MangoServerEvent(capsule.getObjet(), this.communicationType));
  126.                             }
  127.                         }
  128.                     } catch (IOException ex) {
  129.                         this.kill();
  130.                         Logger.getLogger(MangoSocket.class.getName()).log(Level.SEVERE, null, ex);
  131.                     } catch (ClassNotFoundException ex) {
  132.                         Logger.getLogger(MangoSocket.class.getName()).log(Level.SEVERE, null, ex);
  133.                     }
  134.                 }
  135.                 break;
  136.         }
  137.     }
  138.     public void addMangoListener(MangoServerListener mangoListener) {
  139.         this.mangoListener = mangoListener;
  140.     }
  141. }

mood
Publicité
Posté le 20-01-2013 à 16:21:22  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  problème client-serveur (socket)

 

Sujets relatifs
Problème avec mon code.... HELPProblème de merge de session Hibernate
Problème d'encodage.Probleme-à-la-con avec un script VBS
Problème de menuProblème script php tables croisées
Url rewriting problèmeAjax : problème (niveau Iut/Bts)
Reseau Socket - Problème de bind() dans un client/serveur localprobleme de socket (communication client/serveur)
Plus de sujets relatifs à : problème client-serveur (socket)


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR