Bonjour,
Dans la cadre de la réalisation d'un système client/serveur, je rencontre un problème lors de la désérialisation d'un classe maison.
general :
la classe Maison
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 byte[] readbuf = new byte[1024*1024] byte[] sendbuf;
La sérialisation et l'envoi (coté client):
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 [Serializable] public class NetworkPacket { private object cmd; private ArrayList args; public object Command { get { return cmd; } } public ArrayList Arguments { get { return args; } } public NetworkPacket(object command) { cmd = command; args = new ArrayList(); } public void Write(object obj) { args.Add(obj); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 NetworkPacket np = new NetworkPacket("TEST"); np.Write("toto"); BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, np); this.sendbuf = ms.ToArray(); // on envois le tout. this.sock.BeginSend(this.sendbuf, 0, this.sendbuf.Length, SocketFlags.None, new AsyncCallback(SendCallback), this.sock);
La désérialisation et la récéption (coté serveur) - readbuf étant le buffer de reception sur beginreceive:
Avec ce système aucun problème quand un seul client est connecté je récupère bien mes networkpacket.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 MemoryStream memStream = new MemoryStream(this.readbuf); memStream.Seek(0, SeekOrigin.Begin); BinaryFormatter binForm = new BinaryFormatter(); NetworkPacket np = binForm.Deserialize(memStream) as NetworkPacket; memStream.Close();
Par contre dés qu'un deuxème client arrive j'obtiens systématiquement ceci :
Je précise que le serveur traite les clients et les receptions de façon complètement asynchrone.Exception: System.Runtime.Serialization.SerializationException
Le flux binaire '0' ne contient pas de BinaryHeader valide. Les raisons possibles sont un flux non valide ou une modification de la version de l'objet entre la sérialisation et la désérialisation.
à 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)
à NetAccess.Server.ReceiveCallback(IAsyncResult ar) dans E:\NetAccess\NetAccess\Server.cs:ligne 125
Partager