Bonjour,

J'ai réalisé un code qui permet de désérialiser un objet de type bool puis de l'envoyer via un socket. Mon programme génère une exception :

à System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
à System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
à System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
à System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)


En fait le serveur, sérialise un objet booléen puis l'envoie au client. Ce dernier va désérialiser la valeur. Côté serveur je n'ai pas de problème.

Voilà le code de la partie clilent :
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
 try
          {
            client = new TcpClient(serveur, port);
            IdentificationClient.LoginClient = txtLogin.Text.ToString();
            IdentificationClient.PasswordClient = txtPassword.Text.ToString();
 
            NetworkStream fluxClient = client.GetStream(); // the stream
            #region Envoyer le login et le mot de passe au serveur
            if (fluxClient.CanWrite)
            {
                MemoryStream ms = new MemoryStream();
                IFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, IdentificationClient);
                byte[] writerbuffer = ms.GetBuffer(); // we have the byte[] array!
                //byte[] writerbuffer = Encoding.UTF8.GetBytes(txtLogin.Text.ToString());
                fluxClient.Write(writerbuffer, 0, writerbuffer.Length);
                //MessageBox.Show(writerbuffer.Length.ToString());
                fluxClient.Close();
                ms.Close();
            } 
            #endregion
 
            #region Désérialisation et récupération de la réponse du serveur
            //NetworkStream fluxClient1 = client.GetStream();
            bool p;
            if (fluxClient.CanRead)
            {
                byte[] data = new byte[client.ReceiveBufferSize];
                MemoryStream ms2 = new MemoryStream(data);
                fluxClient.Read(data, 0, (int)client.ReceiveBufferSize);
                IFormatter formatter = new BinaryFormatter();
                p = (bool)formatter.Deserialize(ms2); // you have to cast the deserialized object
 
                MessageBox.Show("yes" + p.ToString());
                fluxClient.Close();
                ms2.Close();
            }
            else
            {
                MessageBox.Show("Impossible de lire");
            }
            #endregion
 
              //if (p==true)
                 // MessageBox.Show("Bienvenue "+IdentificationClient.LoginClient);
              //else
                 // MessageBox.Show("Erreur d'identification, veuillez vérifier vos paramètres");
          } 
          catch (Exception E) 
          {
              MessageBox.Show(E.StackTrace);
          }