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

Entrée/Sortie Java Discussion :

Problème java.io.StreamCorruptedException lors d'echange d'objet client-serveur


Sujet :

Entrée/Sortie Java

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    42
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2009
    Messages : 42
    Points : 27
    Points
    27
    Par défaut Problème java.io.StreamCorruptedException lors d'echange d'objet client-serveur
    Bonjour,

    Voilà j'ai un petit problème, je suis entrain de créer un programme client-serveur fonctionnement avec les sockets.

    Pour pouvoir échanger des objet entre eux, j'utilise les class :
    • ObjectInputStream
    • ObjectOutputStream


    Mon serveur reçoit sans problème les objets du client (objets que j'ai séréalisés) tant dis que le client me renvoie une exception à chaque fois que je fait un readObject().

    Voici mon code qui s'exécute quand je clique sur un bouton:

    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
    public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		if(e.getSource()==connexion){
    			try {
    				client=new Socket(saiServer.getText(), 5000);
     
    				System.out.println("connexion réussie: "+client.getInetAddress()); 
     
    				ObjectInputStream reader=new ObjectInputStream(client.getInputStream());
    				ObjectOutputStream writer=new ObjectOutputStream(client.getOutputStream());
     
    				writer.writeObject(monObjet);
    				writer.flush();
    				reader.readObject(); // le programme se plante ici
     
    			} catch (UnknownHostException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			} catch (ClassNotFoundException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    		}		
    	}
    voici l'exception:
    java.io.StreamCorruptedException: invalid type code: AC
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at EcranConnexion.actionPerformed(EcranConnexion.java:134)

  2. #2
    Membre chevronné
    Avatar de professeur shadoko
    Homme Profil pro
    retraité nostalgique Java SE
    Inscrit en
    Juillet 2006
    Messages
    1 257
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 75
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : retraité nostalgique Java SE

    Informations forums :
    Inscription : Juillet 2006
    Messages : 1 257
    Points : 1 855
    Points
    1 855
    Par défaut
    sans le code du serveur on ne peut rien savoir .... sauf qu'il n'envoie pas un objet serialisé (sans doute).

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    42
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2009
    Messages : 42
    Points : 27
    Points
    27
    Par défaut
    Dsl, j'ai pas pu revenir sur le poste avant aujourd'hui.

    Mon serveur lance un thread (AttenteConnexion) qui permet de recevoir les clients. Une fois que le client est connecter, je crée un thread (LectureMsgDuClient) pour chacun d'eux afin de pouvoir lire les informations que le clients m'envoie. l'information reçue passe par la classe (GestionMsgDuClient) qui permet de lancer différente méthode en fonction du type d'objet reçu.
    La class (InfoConnexion) permet de créer les buffer pour l'échange des données.

    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
    package newObjet;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
     
     
    public class AttenteConnexion extends Thread{
     
    	private ServerSocket server;
    	private Socket client;
     
    	public boolean demarrerAttente(){
    		try {
    			server=new ServerSocket(5000,100);
    			this.start();
    			return true;
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			return false;
    		}
    	}
     
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		while(true){
    			try {
    				client=server.accept();
    				System.out.println("nouveau client:"+client.getInetAddress());
     
    				new LectureMsgDuClient(client);
     
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
     
    }
    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
    package newObjet;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Serializable;
    import java.net.Socket;
     
     
    public class LectureMsgDuClient extends Thread{
     
    	private Socket client;
    	private InfoConnexion connexion;
    	private GestionMsgDuClient gestionMsg;
    	private Boolean erreur;
     
    	public LectureMsgDuClient(Socket client){
    		this.client=client;
    		try {
    			connexion=new InfoConnexion(this.client);
    			gestionMsg=new GestionMsgDuClient(connexion);
    			erreur=false;
    			this.start();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    	}
     
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
     
    		while(!erreur){
    			try {
    				gestionMsg.reception();
    				System.out.println("objet ressu");
     
    			} catch (ClassNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    				erreur=true;
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    				erreur=true;
    			}
     
    		}
    	}
    }
    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
    package newObjet;
     
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
     
    public class GestionMsgDuClient {
     
    	InfoConnexion connexion;
     
    	public GestionMsgDuClient(InfoConnexion connexion) throws IOException{
    		this.connexion=connexion;
    	}
     
    	public void reception() throws IOException, ClassNotFoundException{
    		Object objet=connexion.recevoir();
    		if (objet instanceof MonObjet)
    			checkSession((MonObjet)objet);
    	}
     
    	private void checkSession(MonObjet objet){
     
    		System.out.println(objet); //vérifie qu'il n'y a pas de problème en affichant l'objet reçu
    		try {
     
    			connexion.envoyer(objet);// renvoie l'objet reçu. normalement l'objet envoyer par le client devrait pouvoir être reconnu sans problème par celui-ci.
     
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    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
    package newObjet;
     
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
     
    public class InfoConnexion {
     
    	private Socket client;
    	private ObjectInputStream reader;
    	private ObjectOutputStream writer;
     
    	public InfoConnexion(Socket client) throws IOException{
    		this.client=client;
    		writer=new ObjectOutputStream(this.client.getOutputStream());
    		reader=new ObjectInputStream(this.client.getInputStream());
    	}
     
    	public void envoyer(Object obj) throws IOException{
    		writer.writeObject(obj);
    		writer.flush();
    	}
     
    	public Object recevoir() throws IOException, ClassNotFoundException{
    		return reader.readObject();
    	}
    }
    C'est toujours un programme de teste, je modifierai bien entendu le code quand les échanges se feront correctement.

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    42
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2009
    Messages : 42
    Points : 27
    Points
    27
    Par défaut
    Bon apparemment, le client ne plante pas quand il attend un objet avec le readObjet(). C'est seulement quand il reçoit un objet venant du serveur que le client plante. Le pire c'est que j'ai différents message d'exceptions différente. De temps en temps (rarement...) aucunes exceptions s'affiche, apparemment le client reste sur le readObjet() et ne reçois rien venant du serveur même si j'ai bien envoyer un objet.

    Je n'arrive toujours pas a comprendre pourquoi

    Voici les nouvelles exceptions:

    Exception in thread "Thread-3" java.lang.InternalError
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at newObjet.InfoConnexion.readObject(InfoConnexion.java:26)
    at newObjet.GestionMsgDuServer.reception(GestionMsgDuServer.java:16)
    at newObjet.LectureMsgDuServer.run(LectureMsgDuServer.java:32)

    Exception in thread "AWT-EventQueue-0" java.lang.InternalError
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at newObjet.InfoConnexion.readObject(InfoConnexion.java:26)
    at EcranConnexion.actionPerformed(EcranConnexion.java:129)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.readByte(Unknown Source)
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at newObjet.InfoConnexion.readObject(InfoConnexion.java:26)
    at EcranConnexion.actionPerformed(EcranConnexion.java:129)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Java.io.StreamCorruptedException: invalid handle value: 007E0003
    at java.io.ObjectInputStream.readHandle(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at newObjet.InfoConnexion.readObject(InfoConnexion.java:26)
    at newObjet.GestionMsgDuServer.reception(GestionMsgDuServer.java:16)
    at newObjet.LectureMsgDuServer.run(LectureMsgDuServer.java:32)

  5. #5
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    42
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2009
    Messages : 42
    Points : 27
    Points
    27
    Par défaut
    Ouf j'ai enfin trouver lol

    Il y avait un petit blem avec deux threads. Ils accédaient a un même objet, qui gérait mais stream, en même temps.

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

Discussions similaires

  1. Echange Certificat SSL client/serveur
    Par gigi206 dans le forum Général Python
    Réponses: 3
    Dernier message: 16/02/2014, 21h53
  2. Echange des données client/serveur, comment procéder ?
    Par shark59 dans le forum Réseau et multijoueurs
    Réponses: 7
    Dernier message: 17/05/2011, 20h26
  3. [TOMCAT 4.1.24] java.io.StreamCorruptedException
    Par meufeu dans le forum Tomcat et TomEE
    Réponses: 4
    Dernier message: 19/10/2009, 10h25
  4. Réponses: 10
    Dernier message: 17/06/2009, 17h05
  5. Meilleur protocole pour echange de données client/serveur
    Par melcom_dev dans le forum Développement
    Réponses: 2
    Dernier message: 23/03/2005, 18h28

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